post forms in loops

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
thrice
Forum Newbie
Posts: 4
Joined: Fri Oct 24, 2003 4:30 pm

post forms in loops

Post by thrice »

i'm very new to php and i'm having trouble with getting a form to be made in a loop. i've played around with the code and so it may be in shambles right now, but i think you'll get the idea from this



while($obj=mysql_fetch_object($result))
{
//other code that's not part of the problem

echo "<td>
<font color=\"white\" size=\"3\">Rate this File</font>
<form action=\"<?php echo $PHP_SELF ?>\" method=\"post\">
<select name=\"arating\">
<option value=\"1\">1
<option value=\"2\">2
<option value=\"3\">3
<option value=\"4\">4
<option value=\"5\">5
</select>
<input type=\"submit\" name=\"Submit\" value=\"Submit\" >
</form>
</td></tr>

</table>";
if ( $REQUEST_METHOD == 'POST' )
{

InsertRecord($arating,$file);
}

$file=$obj->filename;
}


now when i do this the page comes up ok, but when i try to submit an entry from the drop down box it gives me a 404 error saying that http://www.ipwnj00.net/<?php%20echo%20/ ... ds.php3%20?> can't be found. i've taken it out of the loop, and it submits it to the database, but then the form obviously isn't included with all the rows that i need. it also can't refresh without resending the information. like i said i'm really really new to php so any help would be appreciated.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

<?php echo $PHP_SELF ?> .. you are already in 'PHP mode' when you do this, so you want something like.

Code: Select all

while($obj=mysql_fetch_object($result)) 
{
?>
<td>
<font color="white" size="3">Rate this File</font> 
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
<select name="arating"> 
<option value="1">1 
<option value="2">2 
<option value="3">3 
<option value="4">4 
<option value="5">5 
</select> 
<input type="submit" name="Submit" value="Submit" > 
</form> 
</td></tr> 
</table>
<?php 
if (isset($_POST['arating']))
{ 
    $file=$obj->filename; 
    InsertRecord($_POST['arating'],$file); 
} 
}
thrice
Forum Newbie
Posts: 4
Joined: Fri Oct 24, 2003 4:30 pm

Post by thrice »

great thanks that fixed the error 404 stuff, but i still can't press the refresh without it demanding that i resend the same information. how do i get rid of that? thanks again for the help.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could end your script with a header that sends the user to either a third page, or the same page once again.

Using [php_man]header[/php_man] you automaticly discard the $_POST'ed information, hence the "Resend info"-popup goes away. If you read up on header's I think you will understand.
thrice
Forum Newbie
Posts: 4
Joined: Fri Oct 24, 2003 4:30 pm

Post by thrice »

thanks a lot. i'll try the header() function out and see how it works. alright. last question, i promise =) in the code that markl999 suggested, i'm able to create a drop down box from each query from the database like i needed, but when i submit a rating for a file to the database with the InsertRecord() function, it inserts the rating along with the filename of every other row recieved in the while() loop. so if i have 3 rows queried on the screen with files called blah1.zip, blah2.zip, blah3.zip, and i try to rate blah1.zip, the other blah2.zip and blah3.zip also get that rating. what do i need to do to make each drop down list unique to the row it should be associated with? thanks again for all the help
Post Reply