Pain Posted November 8, 2011 Share Posted November 8, 2011 Hello everyone. I am wondering how can i give different id's to a html form? <?php ... if ($numrows!=0) { // fetching data from the database to variable while ($row = mysql_fetch_assoc($query)) { $view_jam = $row['jamname']; $view_recipe = $row['jamrecipe']; $view_rating = $row['jamrate']; $view_owner = $row['owner']; $view_comments = $row['comments']; $view_count = $row['count']; $view_id = $row['id']; $view_count2 = $row['count2']; echo "Jam name: <b>" . $view_jam . "</b><br />"; echo "Jam recipe: " . $view_recipe . "<br />"; echo "Owner: " . $view_owner . "<br />"; echo "Comments: " . $view_comments . "<br />"; echo "Jam Rating: " . $view_rating . "<br />"; [b]echo '<form action="main.php?id=jams" method="POST">[/b] Choose rating <input type="text" name="jamrate" /> <input type="submit" name="submit1" value="Rate"> </form><br /><br />'; ... ?> As you can see, the form is inside a loop. I am trying to build a rating system. When there is only one row in a database then everything goes smoothly, but if there are more, then the rating applies to all rows. I suppose i need to give different id's to the form. How? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/ Share on other sites More sharing options...
AyKay47 Posted November 8, 2011 Share Posted November 8, 2011 why do you have the entire form inside of the while loop? instead, if anything, have the form outside of the while loop and have the inputs generated in the while loop if the correct conditions are met. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286354 Share on other sites More sharing options...
Pain Posted November 8, 2011 Author Share Posted November 8, 2011 User can upload thing, so when they do that i need a rating form to appears instantly. That's why it is inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286357 Share on other sites More sharing options...
redarrow Posted November 8, 2011 Share Posted November 8, 2011 use a input and give it a id. <?php $id="example"; ?> <input type="text" name="id" value="<?php echo $id; ?>"> Get me. <?php if($_POST['id']=="example"){ echo "cheers mate good idea"; }else{ echo"Bad idea"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286371 Share on other sites More sharing options...
xyph Posted November 8, 2011 Share Posted November 8, 2011 If you use the above example, but change the type to 'hidden', the value will be carried to the action page without being directly visible to the end user. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286406 Share on other sites More sharing options...
Pain Posted November 9, 2011 Author Share Posted November 9, 2011 No, thats not working, besides my form is already has name "jamrate". The problem is that when the loop goes on, my form's name remains "submit1". And i have this code: <?php ... if($submit1) { $counted = $view_count + 1; $myQuery = "UPDATE jam SET count = '$counted' WHERE id = '$view_id'"; mysql_query($myQuery); $counted3 = $view_count2 + $rating_post; $myQuery1 = "UPDATE jam SET count2 = '$counted3' WHERE id = '$view_id'"; mysql_query($myQuery1); $finalrating = $counted3/$counted; $myQuery2 = "UPDATE jam SET jamrate = '$finalrating' WHERE id = '$view_id'"; mysql_query($myQuery2); $finalquery = mysql_query("SELECT * FROM jam"); $numrows1 = mysql_num_rows($finalquery); ... ?> And this is my loop <?php ... while ($row = mysql_fetch_assoc($query)) { $view_count = $row['count']; $view_id = $row['id']; $view_count2 = $row['count2']; echo "Comments: " . $view_comments . "<br />"; echo "Jam Rating: " . $view_rating . "<br />"; echo '<form action="main.php?id=jams" method="POST"> Choose rating <input type="hidden" name="jamrate" /> <select name="jamrate"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> <input type="submit" name="submit1" value="Rate"> </form><br /><br />'; ... ?> So everytime a new loop starts, my form will update all rows, because the name remains "submit1", but i need to update only one, therefore id's or name's should be different each time. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286536 Share on other sites More sharing options...
Pain Posted November 9, 2011 Author Share Posted November 9, 2011 Sorry, my submit button is submit1, not my form. So now im confused, do i have to give different names (or id's) to the form or to the actual submit button? Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286556 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 to figure out which form has been submitted? you can do this by have unique submit button names, then check to see which one has been clicked using $_POST['submit_name'] Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286559 Share on other sites More sharing options...
Pain Posted November 9, 2011 Author Share Posted November 9, 2011 I need each button to get a name automatically. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286568 Share on other sites More sharing options...
Pain Posted November 9, 2011 Author Share Posted November 9, 2011 I fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/250725-giving-an-id-to-a-form/#findComment-1286579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.