Jump to content

Recommended Posts

Hey,

 

I'm trying to get a form to appear in a loop, but I'm having some issues with how to make it so that each individual submit button works for its individual form -- I can't seem to come up with a solution as to how to make the whole thing work :/

 

This is the relevant section of PHP:

// Get all the data from the dogs table
        $result2 = mysql_query("SELECT * FROM training
              WHERE type='$type' AND level='$level'") or die(mysql_error());                  

if($result2 == ''){ echo "There are no events matching these details.";}
else{

echo "<table border='0'>";
echo "<tr><th>Event Name</th> <th>Level</th> <th>Type</th> <th>Entry Fee</th> <th>Enter Dog</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result2 )) {

$eventno = $row['id'];
$creator = $row['runby'];
$fee = $row['fee'];
$submit = ($eventno + '1');

// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>";
echo $row['level'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>"; 
echo $row['fee'];
echo "</td><td>"; 
echo "<form action=arena.php method=post>
<input type=hidden name=eventno value=$eventno>
<input type=hidden name=entrantid value=$entrantid>";
echo "<input type=submit name=submit2 value='Enter Dog!'>";
echo "</td></tr>";

}
echo "</table></form>";
}}

if(isset($_POST['submit2']))
{ 
$eventid = $_POST['eventno'];
$entrantid = $_POST['entrantid'];
$entrantscore = '2';

// Get all the data from the dogs table
        $result3 = mysql_query("SELECT * FROM trainingentrants
              WHERE eventid='$eventid' AND entrantid='$entrantid'") or die(mysql_error()); 
              if($result3 != ''){ echo "Oops, this dog is already participating in this event!";}
              else {

echo "Success! Your Dog has been entered into this Event.";

$enterDog = @mysql_query("INSERT INTO trainingentrants (eventid, entrantid, entrantscore) VALUES ('$eventid', '$entrantid', '$entrantscore')") or die("Error: ".mysql_error());

// give entry fee to creator

$sql4=mysql_query("UPDATE players SET onhand = onhand + '$fee' WHERE id='$creator'");
$result4=mysql_query($sql4);

 

Basically the bit where the formaction=arena.php I want it so that every single Event (just to make this clear, the idea is that you are presented with a list of "Events" from the database to enter your dogs into!) has a submit button which will enter the dog into the event.

 

I'm not sure whether the error is in the submit button or what, but basically I really need help working out how to make the form function in the loop as well as just showing up in it! :S

 

All and any advice would be appreciated!

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/207989-putting-a-form-in-a-loop/
Share on other sites

The way you have your code now, you have nested <form> tags and only one </form>. This is not permitted and will cause all sorts of problems. Move this line

<?php
echo "<form action='arena.php' method='post'>";
?>

outside the loop.

 

Ken

Hey,

Thanks very much for the suggestion; I did what you said and moved the opening of the form outside the loop. Unfortunately it doesn't seem to have fixed the problem! The submit button still doesn't seem to work independently for further options after the first one :/

 

This is what it's been changed to:

if(isset($_POST['submit']))
{
$type = $_POST['type'];
$level = $_POST['level'];
$entrantid = $_POST['dogid'];

// Get all the data from the dogs table
        $result2 = mysql_query("SELECT * FROM training
              WHERE type='$type' AND level='$level'") or die(mysql_error());                  

if($result2 == ''){ echo "There are no events matching these details.";}
else{

echo "<form action=arena.php method=post>";

echo "<table border='0'>";
echo "<tr><th>Event Name</th> <th>Level</th> <th>Type</th> <th>Entry Fee</th> <th>Enter Dog</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result2 )) {

$eventno = $row['id'];
$creator = $row['runby'];
$fee = $row['fee'];
$submit = ($eventno + '1');

// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>";
echo $row['level'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>"; 
echo $row['fee'];
echo "</td><td>"; 
echo "
<input type=hidden name=eventno value=$eventno>
<input type=hidden name=entrantid value=$entrantid>";
echo "<input type=submit name=submit2 value='Enter Dog!'>";
echo "</td></tr>";

}
echo "</table></form>";
}}

if(isset($_POST['submit2']))
{ 
$eventid = $_POST['eventno'];
$entrantid = $_POST['entrantid'];
$entrantscore = '2';

// Get all the data from the dogs table
        $result3 = mysql_query("SELECT * FROM trainingentrants
              WHERE eventid='$eventid' AND entrantid='$entrantid'") or die(mysql_error()); 
              if($result3 != ''){ echo "Oops, this dog is already participating in this event!";}
              else {

echo "Success! Your Dog has been entered into this Event.";

$enterDog = @mysql_query("INSERT INTO trainingentrants (eventid, entrantid, entrantscore) VALUES ('$eventid', '$entrantid', '$entrantscore')") or die("Error: ".mysql_error());

 

Since you're allowing the user to select only one dog via the form, I would change all the submit buttons to radio buttons and have one submit button at the end.

<?php
while($row = mysql_fetch_assoc( $result2 )) {

$eventno = $row['id'];
$creator = $row['runby'];
$fee = $row['fee'];
$submit = ($eventno + '1');

// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>";
echo $row['level'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>"; 
echo $row['fee'];
echo "</td><td>"; 
echo "<input type='hidden' name='eventno' value='$eventno'>\n";
echo "<input type='hidden' name='entrantid' value='$entrantid'>\n";
echo "<input type='radio' name='enter_dog' value='yes'>";
echo "</td></tr>";

}
echo "<input type='submit' name='submit2' value='Enter Dog!'>";
echo "</table></form>";
?>

 

Ken

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.