Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

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
0
loppyrabit
Asked:
loppyrabit
  • 4
  • 4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now