/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How to tell which submit button was clicked

Hi all,
I have a php scipt I’m working on, where a user can click on 2 buttons. Depending on which button was clicked, I would like a hidden field’s value set to a value which would then be used to do some processing by php.

So, I was wondering if there is a way which I can tell if a submit button has been clicked, as I would like to write a function to take care of both buttons ?

Cheers,
Ben

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 21.2008 — If the submit buttons have name and value, the button will have it's pair sent to the server. No need for hidden fields.
Copy linkTweet thisAlerts:
@mosherbenauthorAug 21.2008 — For the script I'm writing, the actual hidden field I would like to set is used to be passed to my php script, which is used to evaluate what code to run. It's used to just keep track of "what to do" next, since the page is posting to itself.

If you like, I can post up some of my php code to illustrate what I mean ?

Ben
Copy linkTweet thisAlerts:
@davidbaltAug 21.2008 — What the previous poster was saying is that if you define the submit button like this:

<input type="submit" name="myfirstbutton" value="Go!" />

<input type="submit" name="mysecondbutton" value="Go!" />

You are only going to see either "myfirstbutton=Go" or "mysecondbutton=Go" on the PHP side. You can just say:

if ($_REQUEST['myfirstbutton'])

// do first button stuff

if($_REQUEST['mysecondbutton'])

// do second button stuff
Copy linkTweet thisAlerts:
@mosherbenauthorAug 21.2008 — David,

thanks for your reply ?

Yeah, that sounds more like what I need to do now. Just a quick question, what about other stuff I need to do the processing?

The script basicly displays rows from a table and has a checkbox at the end, and the checkboxs are needed to do the processing as well. How would I do that?

Ben
Copy linkTweet thisAlerts:
@davidbaltAug 21.2008 — Well, the best way to do it is to be clever in the naming of your INPUT checkboxes in the HTML.

PHP has the ability to automatically create an array out of incoming form data if the form element names contain brackets. For instance:

<input type="checkbox" name="checkbox[1]" />

<input type="checkbox" name="checkbox[2]" />

<input type="checkbox" name="checkbox[3]" />

<input type="checkbox" name="checkbox[4]" />

Let's say the user checks 1 and 3. Then they submit. On the PHP side, you'll see this:

$all_checked_boxes = $_REQUEST['checkbox'];

foreach($all_checked_boxes as $id=>$blah)

{

echo "$id was checked!";

}

You should see "1 was checked! 3 was checked!"

I hope that helps.
Copy linkTweet thisAlerts:
@mosherbenauthorAug 21.2008 — Thanks for the help David, its working now for both buttons ?

Cheers,

Ben
×

Success!

Help @mosherben spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...