Forum Moderators: coopster
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
"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
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
<?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>
<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();
}
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!
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.