genius_supreme Posted August 28, 2012 Share Posted August 28, 2012 I'm trying to have a multiple submit button (or if there is any other way) to update each row of record individually. What i want is to identify which button was clicked. The records are displayed in a table format from 3 different tables in the same database. Each row of the query printed with a submit button. Previously i was able to do it with this code <td width=\200\" align=\"center\"><input type=\"text\" name=\"adult$j\" value=\"$adult\" size=\"6\"></td> in a loop and using this on form submit if(@$_POST['submit1']) $btn=1; and so on up to $btn=6; the thing is for this i know exactly (max) how many rows would be displayed so i have set a max of 6 form submit validation using if(@$_POST['submit1']) $btn=1; to identify which button was clicked. My current task is that I wouldn't know how many rows would be displayed but I still need to know which submit button was clicked. I can still use the previous code in a loop to create the submit buttons and it worked. It printed out 1 submit button for each row of record as I wanted. I need help on the submit part coz I really don't know how to identify the "actual" submitted button. Is there a way for me to identify which button was clicked from n number of rows/button? or is there a better work around for this? each button is to update individual records based on ID and which table is it from using single sql as below. $sql="UPDATE $tbl SET MembershipNumber=$memnumber WHERE MemberID=$id"; please help me out. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2012 Share Posted August 28, 2012 give submit buttons the same name but different values EG <?php if (isset($_POST['subBtn'])) { switch ($_POST['subBtn']) { case 'Submit1': $btn = 1; break; case 'Submit2': $btn = 2; break; case 'Submit3': $btn = 3; break; } echo "Button : $btn"; } ?> <form method='post'> <input type='submit' name='subBtn' value='Submit1' /><br /> <input type='submit' name='subBtn' value='Submit2' /><br /> <input type='submit' name='subBtn' value='Submit3' /><br /> </form> Quote Link to comment Share on other sites More sharing options...
genius_supreme Posted August 28, 2012 Author Share Posted August 28, 2012 Barand I appreciate your help again but that's not what I'm looking for. I wouldn't know now many case to put in when I don't know how many record will result in the query. the form will also be generated dynamically based on number of record Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 28, 2012 Share Posted August 28, 2012 If you're using code in a loop to create the buttons, use the same code to process them. Quote Link to comment Share on other sites More sharing options...
genius_supreme Posted August 28, 2012 Author Share Posted August 28, 2012 If you're using code in a loop to create the buttons, use the same code to process them. I use while loop to create the buttons as well as fetch array of records. how do I use another loop to process n number of buttons where n=unknown Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 28, 2012 Share Posted August 28, 2012 It isn't going to make any difference which submit button is clicked as they are all contained in the same form. Once a button is clicked the $_POST array will contain the value of the last button (if the buttons all have the same name attribute) or it will contain the values of all the buttons. You will never know which has been pressed. You can either update all of the records shown on the page i.e if there are 6 text fields then update all 6 records when a submit button is clicked, or you will have to contain each row within its own form and use a hidden field within each that contains the id of the record. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2012 Share Posted August 28, 2012 Once a button is clicked the $_POST array will contain the value of the last button (if the buttons all have the same name attribute) or it will contain the values of all the buttons. You will never know which has been pressed. If you think that is the case, try the code I posted. If I click "Submit2" the output and the post array looks like this Button : 2 Array ( [subBtn] => Submit2 ) Quote Link to comment Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 You have to watch out. From what I Remember some browsers won't send the submit button along if the 'return' key is used to submit the form. It'd be worth testing, at least Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2012 Share Posted August 28, 2012 How refreshing - a guru who with wisdom speaks Firefox - detects which button clicked but defaults to Submit1 (first one) if return is pressed IE9 - detects which button is clicked but includes no submit button in the post when return is pressed. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 28, 2012 Share Posted August 28, 2012 I wouldn't rely upon the button pressed at all, but rather compare the submitted values against the existing ones. Easiest way to do this, is to include the old value in a session variable, or something similar. Then you just loop through the values posted, compare them to what you have saved, and you'll know instantly what's been changed and what haven't. As an added benefit this will allow your users to update several rows at the same time, without having to send a POST request for every single one of them. Quote Link to comment Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 I wouldn't rely upon the button pressed at all, but rather compare the submitted values against the existing ones. Easiest way to do this, is to include the old value in a session variable, or something similar. Then you just loop through the values posted, compare them to what you have saved, and you'll know instantly what's been changed and what haven't. As an added benefit this will allow your users to update several rows at the same time, without having to send a POST request for every single one of them. Sessions aren't meant to hold mutable data, and if it must, you have to be very careful to isolate them to the current line of requests using some sort of unique token passed in the form itself. If a user browses to the same form in multiple tabs, your session has no way to differentiate between the two otherwise. It's a stateless protocol, so trying to enforce state that could possibly change in a per-request manner should be done so with care. The correct way to do this is use a different form for each submit button. It's more mark-up, but it won't harm your page visibly at all, and with PHP behind it, there's not much extra effort involved. Avoid using hacky solutions for the sole purpose of saving your website from a little bit of extra mark-up. You'll find your application will play better universally if you stick to the spec. Quote Link to comment Share on other sites More sharing options...
genius_supreme Posted August 29, 2012 Author Share Posted August 29, 2012 all this are getting too complicated for me. query result=n number of records while loop for query result fetch array $qry print button name="btn".$btn $btn++; end while loop assuming form has btn1 to btnN how do I identify which button was clicked? another way I can think off is using GET method via hyperlink for each row. I have done this before but thought of using a button would be easier. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 29, 2012 Share Posted August 29, 2012 xyph: I agree with you that it's not the most optimal solution, but it is the easiest one to get up and running. Personally, I'd probably store a snapshot in a semi-permanent storage, and query the database on update to see if any of the data had been updated from other sources in the meantime. To solve the OPs question using multiple forms would indeed be the best way to go about it, but I don't agree that he should be using multiple buttons in the first place. genius_surpeme: I recommend that you drop that idea of yours, and just use one form and one submit button. Just like I described above. Quote Link to comment Share on other sites More sharing options...
genius_supreme Posted August 29, 2012 Author Share Posted August 29, 2012 ChristianF point noted. guess will be using get method via hyperlink. Quote Link to comment 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.