Forloop and Javascript submit in PHP

I have a code like this.

As per my previous question, I DONT WANT to send any values in the URL and going with the hidden field.

In the below code, when I submit the form, i was able to get the post values in the next page for the first row value only (first while loop record).

Why? When I inspect it with firebug I was able to see the hidden values for all the records.

In other words, only the first record is getting posted to the next page and not the consecutive records.

If the below code looks crazy, could someone help me in such a way that I don't want to pass the values in URL or anywhere in the browser/webform

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Books</title>
    <script type="text/javascript">
            function test() {
                document.forms.testform.submit();
            }
    </script>
</head>

<body>

    <?php
         mysql_connect("localhost","root","password");
         mysql_select_db("books");
         $result = mysql_query("SELECT B_Code,Author_Name FROM books");  
         while($row = mysql_fetch_array( $result )) {  
    ?>

    <form action ="post.php" id="testform" method="post">
        <input type="hidden" name="name" value="<?php echo $row['B_Code']; ?>" />
        <input type="hidden" name="id" value="<?php echo $row['Author_Name']; ?>" />
    </form>

    <a href="#" onclick="test()"><?php echo $row['Author_Name']; ?></a>

    <?php } ?>

</body>
</html>

Thanks, Kimz

Answers


Try this solution...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Books</title>
        <script type="text/javascript">
        function test(name,id)
        {
        document.getElementById("name").value = name;
        document.getElementById("id").value = id;
        document.forms.testform.submit();
        }
        </script>
        </head>
        <body>
        <form action ="post.php" id="testform" method="post">
        <?php
        mysql_connect("localhost","root","password");
        mysql_select_db("books");
        $result = mysql_query("SELECT B_Code,Author_Name FROM books");  
        while($row = mysql_fetch_array( $result )) {  
        ?>
        <a href="#" onclick="test('<?php echo $row['B_Code']; ?','<?php echo $row['Author_Name']; ?>')"><?php echo $row['Author_Name']; ?></a>
        <?php } ?>
        <input type="hidden" name="name" value="" id="name"/>
        <input type="hidden" name="id" value="" id="id"/>
        </form>
        </body>
        </html>

why you need to submit form using javascript, you can submit form without javascript. use Please remove a tag and use button type submit.


Need Your Help

Rendering new component on-click in a parent component

reactjs

I have a button component called CreateTicket and a component I want to render on click called IndividualTicketInput. I have created a parent component where I currently have both of these. I need to

Would like to keep cache data when changing to other UIView

iphone xcode uiview

i would love to have some help or any keyword that i can use to search for.