Jump to content

Form Variables


lhbdan

Recommended Posts

I wrote this while loop to cycle through all the rows in my database, and for each row, to create two things, first to echo the name of the book, and the second was to create a button next to it that when clicked, sends the BookID of that row via the post method to the the page at /scas/moreinfo.php . The first echo statement works, and states every book name on the page one after another. For some reason however the form Posts the same book id in every form i think, the value of it being the value in BookID in the last row of the database, in this case 16, so no matter what button is pressed, it sends the value 16. What have I done wrong?

 

while($row = mysql_fetch_array($result))

{

      echo  $row['BookName'] ;

echo "<form name='moreinfo' action='/scas/moreinfo.php' method='post'>";

echo "<input type='hidden' name='BookID' value='" .$row['BookID']. "'>";

echo "<input type='submit' value='More Info' />";

        echo "<br />";

}

Link to comment
https://forums.phpfreaks.com/topic/230500-form-variables/
Share on other sites

1. every iteration of the while loop is changing the value, of ONE variable. as a result only the LAST value recovered from the db is being used.

 

2. every iteration of the while loop is starting a new form without closing the previous form.

 

Might I suggest you use a SELECT field for your form rather than individual forms

Link to comment
https://forums.phpfreaks.com/topic/230500-form-variables/#findComment-1186923
Share on other sites

Examples...

 

Passing via the form/post method

 

?>
<form name='moreinfo' action='/scas/moreinfo.php' method='post'>
<select name="BookIDd">
<?PHP
while($row = mysql_fetch_array($result)){
<option value="<?PHP echo $row['BookID']; ?>"><?PHP echo  $row['BookName'] ; ?></option>
}
?>
</select>
<input type='submit' value='More Info' /><br />
</form>
<?PHP

 

 

Passing via the GET method

 

while($row = mysql_fetch_array($result)){
?>
<a href="/scas/moreinfo.php?BookID=<?PHP echo $row['BookID']; ?>"><?PHP echo  $row['BookName'] ; ?></a><br/>
<?PHP
}

Link to comment
https://forums.phpfreaks.com/topic/230500-form-variables/#findComment-1186926
Share on other sites

Closing the form should do the trick

while($row = mysql_fetch_array($result))
{<?php
          echo  $row['BookName'] ;
   echo "<form name='moreinfo' action='/scas/moreinfo.php' method='post'>";
   echo "<input type='hidden' name='BookID' value='" .$row['BookID']. "'>";
   echo "<input type='submit' value='More Info' />";
   echo"</form>"; // close the form
        echo "<br />";
}?>

 

Link to comment
https://forums.phpfreaks.com/topic/230500-form-variables/#findComment-1186927
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.