Forum Moderators: coopster

Message Too Old, No Replies

two submit buttons in the same form?

can php decipher which submit button was clicked?

         

coolo

5:40 am on Feb 24, 2004 (gmt 0)

10+ Year Member



At the most basic level, I have a form that contains some text fields that the user is supposed to type some text in. The form has two submit buttons, and the intention is that the program called will do something different depending on which submit button is clicked.

For instance

<form method = "post" action = "saysomething.php">
Type Name:<br>
<input type = "text" name = "username" value = "">
<input type = "submit" value = "Your name">
<input type = "submit" value = "Child's name">
</form>

saysomething.php then looks something like this

<?
if (Your name is clicked){
print "Your name is $username";
}
else {
//Child's name is clicked
print "Your child's name is $username";
}
?>

Is there a way to make this work?

[edited by: coolo at 7:41 am (utc) on Feb. 24, 2004]

anchordesk

5:55 am on Feb 24, 2004 (gmt 0)

10+ Year Member



You can use a name attribute with the submit button

<input type = "text" name = "username" value = "">
<input type = "submit" name="yourName" value = "Your name">
<input type = "submit" name="childsName" value = "Child's name">

Then just read for $yourName or $childsName to not be empty.

coolo

7:03 am on Feb 24, 2004 (gmt 0)

10+ Year Member



thanks, I'm going to go give that a try.

coolo

7:39 am on Feb 24, 2004 (gmt 0)

10+ Year Member



ok, so I tried that, and well, at least the way I tried, it didn't work. Here's what my saysomething.php looks like now:

<?
if (empty($childsName){
//"Your name" button is clicked
print "Your name is $username";
}
else {
//"Child's name" button is clicked
print "Your child's name is $username";
}
?>

Anyhow, regardless of what button I click, the first part of the if statement is returned. Any thoughts?

jatar_k

8:00 am on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about javascript instead?

formpage.html

<script lang="javascript"> 
<!--
function subit(which) {
if (which == 1) {
document.myform.action = 'somepage.php?who=1';
} else if (which == 2) {
document.myform.action = 'somepage.php?who=2';
}
document.myform.submit();
}
//-->
</script>
<form method="post" name="myform">
Type Name:<br>
<input type="text" name="username" value = "">
<input type="button" value="Your name" onClick="javascript:subit(1);">
<input type="button" value="Child's name" onClick="javascript:subit(2);">
</form>

somepage.php

<? 
if ($_GET['who'] == 1) {
echo "who equals 1";
} else if ($_GET['who'] == 2) {
echo "who equals 2";
}
?>

mykel79

9:43 am on Feb 24, 2004 (gmt 0)

10+ Year Member



The code you wrote won't work because it has a parse error. It's missing a )
It should be:
<?
if (empty($childsName)){
.
.
.

Works fine for me. If it doesn't work for you, maybe due to server settings you need to access the variables like this:
$HTTP_POST_VARS['childsName']

coolo

10:27 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



ok, so I tried again this morning and it worked for me. Thanks much.