Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreive button value

Status
Not open for further replies.

ljCharlie

IS-IT--Management
Apr 21, 2003
397
US
I have two buttons in my .shtml form page and in my .php result page, I want to determine which button the user click to submit the form to my .php page to process the form. I have tried this:

$btnSubmit = $HTTP_GET_VARS["btnSubmit"];

to get the value of $btnSubmit and then determine what the value is so I can process the request accordingly. However, I always get an error saying that I have an undefined index of btnSubmit. In my .shtml form page, the two buttons are defined with the same name except that the value property is different. Any help will be appreciated!

Many thanks in advance!

ljCharlie
 
I don't think the submit button is sent as a variable, but I'm guessing. You can check by doing a foreach on $HTTP_GET_VARS and printing them all to see what you've got.

I would suggest adding a hidden field to the form which is set by a javascript function when the button is clicked.

If you're doing some validation of the form in javascript before it's submitted, put it in there.

If you need me to post the code for any of my suggestions above let me know.

It's not what you know, it's who knows it that counts.
 
The value of the button is sent, but if enter is pressed rather than one clicked then nothing is sent, and it all works slightly differently in different broswers, so good luck.
 
Here's the code that I have in my .shtml file for the button.

<INPUT TYPE=&quot;button&quot; NAME=&quot;btnSubmit&quot; Value=&quot;Update&quot; onClick=&quot;verification()&quot;>

When I do an alert from the Javascript of the button btnSubmit, I received undefine. How do I define the button? I thought by putting a property Value=&quot;Update&quot; in the input tag is a defining. If this is not defining, then how is it done? Please help.

ljCharlie
 
The path for form objects varies between browsers. I would suggest using a different method.

Change your buttons to:
<INPUT TYPE=&quot;button&quot; NAME=&quot;btnUpdate&quot; Value=&quot;Update&quot; onClick=&quot;verification(&quot;button1&quot;)&quot;>
<INPUT TYPE=&quot;button&quot; NAME=&quot;btnSubmit&quot; Value=&quot;Submit&quot; onClick=&quot;verification(&quot;button2&quot;)&quot;>

Notice the button1 and button2 between the brackets.

Then use the following code in your verification function:

function verification(whichButton) {
if(whichButton == &quot;button1&quot;) {
alert(&quot;button 1 pressed&quot;);
}
if(whichButton == &quot;button2&quot;) {
alert(&quot;button 2 pressed&quot;);
}
else {
alert(&quot;Something else happened&quot;);
}
}

When you're happy that this is working you can simplify it to something a bit more helpful:

function verification(whichButton) {
document.forms[&quot;myFormName&quot;].hiddenFieldName.value = whichButton;
}

This will send the value of whichButton along with the form (you need to make a hidden field in HTML) and you can then find the value in your php code using:

$btnSubmit = $HTTP_GET_VARS[&quot;hiddenFieldName&quot;];

Hope this helps

Marklar

It's not what you know, it's who knows it that counts.
 
Thanks, Marklar! It works but why can you explain why the way I did it did not work? In addition, why is it working in metho=&quot;GET&quot; only? I tried POST and it doesn't work.

Again, thanks! You've been a big help.

ljCharlie
 
I can't tell you why it didn't work unless I see the code.

If you use the post method you need to replace $HTTP_GET_VARS with $HTTP_POST_VARS or simply $_POST


Marklar

It's not what you know, it's who knows it that counts.
 
I did replace the POST with GET and the HTTP_GET_VARS with HTTP_POST_VARS but it's not working. Anyway, I can live with GET.

Again, thanks!

ljCharlie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top