Combining HTML Forms

hi all
i need a little help with this i have to forms:
<form name="form1" method="post" action="$PHPSELF?action=A">
<input name="name" type="text">
  <input type="submit" name="Submit" value="first name">
</form>
<form name="form1" method="post" action="$PHPSELF?action=B">
<input name="name" type="text">
  <input type="submit" name="Submit" value="Last name">
</form>
i would like to combine these so the textbox only appears once on the form and is sent to a different url based on which of the two submit buttons is pressed
any help is gratefully received
loppyrabitAsked:
Who is Participating?
 
Diablo84Commented:
The PHP approach would be something like:

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="name" type="text">
  <input type="submit" name="SubmitA" value="first name">
  <input type="submit" name="SubmitB" value="Last name">
</form>

Then:

<?php
if (array_key_exists('SubmitA',$_POST)) {
 //firstname button was clicked
}
elseif (array_key_exists('SubmitB',$_POST)) {
 //lastname button was clicked
}
?>

Or the same using isset.

The alternative would be using JavaScript to change the forms action onClick but this isn't a reliable method given that JS may be disabled in the users browser.

Diablo84
0
 
ldbkuttyCommented:
Have two different textboxes for firstname and lastname and two submit buttons with same name but different value. Only the submitted button value will be passed with the form.

Example:

<?php
if(isset($_POST["Submit"]))
{
  if($_POST["Submit"] == "First name"]) {
    // First name submit button was hit.
    echo $_POST["first_name"];
  }
  else if($_POST["Submit"] == "Last name"]) {
    // Last name submit button was hit.
    echo $_POST["last_name"];
  }
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHPSELF']; ?>">
  <input name="first_name" type="text">
  <input name="last_name" type="text">
  <input type="submit" name="Submit" value="First name">
  <input type="submit" name="Submit" value="Last name">
</form>
0
 
ldbkuttyCommented:
Ah ok, you want only one textbox!

<?php
if(isset($_POST["Submit"]))
{
  if($_POST["Submit"] == "First name") {
    // First name submit button was hit.
  }
  else if($_POST["Submit"] == "Last name") {
    // Last name submit button was hit.
  }
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHPSELF']; ?>">
  <input name="name" type="text">
  <input type="submit" name="Submit" value="First name">
  <input type="submit" name="Submit" value="Last name">
</form>

Diablo: I thought two submit buttons with different names will be passed irrespective of the button submitted. Or am I wrong? :=)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
ldbkuttyCommented:
Ok, I was wrong!
0
 
Diablo84Commented:
For the sake of example, this would be the JS approach if you *really* need to use it, as i said it is not as reliable though... and is also pointless if you are submitting the data to the same page (with a different flag) anyway..

<form name="form1" method="post" action="pagea.php">
<input name="name" type="text">
<input type="submit" name="button1" value="first name" onClick="this.form.action = 'pagea.php';">
<input type="submit" name="button2" value="Last name" onClick="this.form.action = 'pageb.php';">
</form>

>> Diablo: I thought two submit buttons with different names will be passed irrespective of the button submitted. Or am I wrong? :=)

hmm, i will have to check this, i had thought it was the same principle as checkboxes... they will only exist in the post array if they are clicked...
0
 
Diablo84Commented:
>>  Ok, I was wrong!

oh ok, made me wonder then =D

loppyrabit,

please be sure to give joint credit in this thread with the split button, we have two sets of good contributions. :)

Diablo84
0
 
ldbkuttyCommented:
Thats alright, no problem Diablo. Have a nice sunday. :=)
0
 
Diablo84Commented:
Thank you, you too :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.