Jump to content

multiple submit button handling


genius_supreme

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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. :P

 

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.

Link to comment
Share on other sites

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.