Forum Moderators: coopster

Message Too Old, No Replies

One form, three submit buttons with different values

Can PHP read submit button value and use that information?

         

Jeremy_H

9:41 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



I have a form that then gets sent for processing to a PHP script.

In this form, I would like to have one input box, and three submit buttons, with different values.

Values like "Reprint" or "Answer Key" or "Hints".

What I would like to do is for the PHP script to be able to read the value of which ever submit button was pressed, and then be able to use that information in determining how to process the input value.

Is that possible, or am I looking at having to create three different forms on that one page?

Thanks

coopster

9:45 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, you can do that, just monitor for the button to be posted. Something like ...
if (isset($_POST['reprintSubmitButton'])) { 
// do something
} else if (isset($_POST['hintSubmitButton'])) {
// do something
}

Jeremy_H

10:01 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Thanks coopster for your reply, but I'm a bit confused:

"No, you can do that"

Is that "No, I can't do that", or "Yes, I can do that"?

Hoping that I can, I tried adding it to my PHP script, but with no luck. Are you suggesting I add those commands to the page with the script or the page with the form?

Thanks

bomburmusicmallet

10:12 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Hi Jeremy

On the page with the form, create your buttons so that the name="a_unique_name". Of course, the value will be what is printed on the button, and the types will be "submit".

print '<input type="submit" name="reprint" value="Reprint">';
print '<input type="submit" name="answerkey" value="Answer Key">';
print '<input type="submit" name="hints" value="Hints">';

These can go in the same for as long as the form action is the same.

In the form action file, check the value of the name of the submit button pressed to see what to do:

if($_POST['reprint'])
{ reprint(); }
elseif($_POST['answerkey'])
{ answerkey(); }
elseif($_POST['hints'])
{ hints(); }

HTH, Jenny

coopster

10:21 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry for the poorly written response. Yes, you can have 3 buttons in a single form. Here is a sample snippet that shows what you can do ...
<?php 
$name = (isset($_POST['name']))? $_POST['name'] : '';
?>
<html><head><title>Submit Example</title></head><body>
<h1>Sample</h1>
<form action="<?php print strip_tags($_SERVER['PHP_SELF']);?>" method="post">
<fieldset>
<legend>Submit Example</legend>
Name: <input name="name" id="name" type="text" value="<?php print $name;?>" /><br /><br />
<input name="reprint" type="submit" value="Reprint" /><br />
<input name="answerKey" type="submit" value="Answer Key" /><br />
<input name="hints" type="submit" value="Hints" /><br />
</fieldset>
</form>
<fieldset>
<legend>Posted Form Values<legend/>
<pre>
<?php
// Let's have a look at everything that is happening:
print_r($_POST);
if (isset($_POST['reprint'])) {
print "\nThis is when we would process a 'Reprint' request!";
} else if (isset($_POST['answerKey'])) {
print "\nThis is when we would process a 'Answer Key' request!";
} else if (isset($_POST['hints'])) {
print "\nThis is when we would process a 'Hints' request!";
}
?>
</pre>
</fieldset>
</body></html>

Added: thanks Jenny!

dmorison

10:27 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've always done this:

<form>
<input type='submit' name='submit' value='foo' />
<input type='submit' name='submit' value='bar' />
</form>

And then in the handler you can do either:

if ($_POST["submit"] == "foo")
{
// do foo
}
if ($_POST["submit"] == "bar")
{
// do bar
}

..or alternatively use a case construct:

switch($_POST["submit")
{
case "foo":
//do foo
break;

case "bar":
//do bar
break;
}

Note that "foo" and "bar", as the value parameter of the input field "submit" are the actual text that would appear on the button. In most of my apps, this ends up as being "Save" or "Cancel" - something like that; with the form handler looking something like this:

if ($_POST["submit"] == "Cancel")
{
header("Location: some_page.php");

exit();
}
if ($_POST["submit"] == "Save")
{
// process rest of form

header("Location: some_page.php");

exit();
}

dmmh

11:11 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



also works, but dont forget to account for those pesky spaces if its anything over 1 word :p

whoisgregg

11:34 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With multiple <input type="submit"...> buttons, what happens when a user submits the form by hitting the enter key?

[edited by: whoisgregg - Mon, 09 Jan 2006 23:34:44 GMT]

Jeremy_H

11:47 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Thank all so much, it works great!

whoisgregg, who I did was set it so it would check to see if button A was pressed, if so, it would do it's thing. Else if button B was pressed, if so, it would do another thing. Else it would do a third and default thing.

Then, if the person presses enter then it would do the default thing.

In my limited trials, it worked that way.

Everyone, thanks again!

grandpa

5:41 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With multiple <input type="submit"...> buttons, what happens when a user submits the form by hitting the enter key?

That depends. You can exercise a little control over the default button. One way would be to assign a tabindex to the default. This should ensure that the default button is always the first choice. Of course, if the user hits the tab button then all bets are off, in that case the active button will be submitted.

I've noticed at least one site that will not let you submit any form with the Enter key. You MUST click the submit button to submit the form, or the Back button to move back a screen. I'm sure it's JS controlled, and it certainly goes a long way in preventing user errors.

You can, of course, also use focus to set your default key.