1

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

3
  • Using the same names for all the hidden fields?? And you are having multiple forms. Hence only those record of the form you click would get submitted. Commented Mar 10, 2014 at 4:10
  • Yes Yes ;) You are right. Same name and id for all the records in a hidden field Commented Mar 10, 2014 at 4:11
  • FYI - even if i change the form name for both the values, I'm not able to get the values in the next page. Commented Mar 10, 2014 at 4:13

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.