Page 1 of 1

2 submit buttons on 1 form?

Posted: Sat Jul 06, 2002 1:13 pm
by virgil
Hi PhP's(peeps)

Is it possible to submit form results to 2 diffrent pages depending on selections or conditions made in the form?


It's a simple login form. NAME, EMAIL, PASSWORD. There is a password hint field in the database. I want to have the user enter NAME, EMAIL and PASSWORD to go to page A by using "SUBMIT" OR enter just NAME and EMAIL and get back a password hint (printed right on the login form) by using a second "GET HINT" submit button. I would like to use just one form so the user dosen't have to reenter the NAME, EMAIL twice.


I'm thinking maybe

Code: Select all

<form method=POST action=<?=$send_data;?>

<input type="submit" value="Send to page A" name="page A">

<input type="submit" value="Send to page B" name="page B">
IF (isset($page A)) &#123;
      $send_data="page_A.php"
&#125;
IF (isset($page B)) &#123;
      $send_data="page_B.php"
&#125;
It kind of works, but runs through the form twice.(The second time actually sets the action. Any way to get "page_A.php" to function as a defualt action URL and go through on the first try? I don't care if the second choice runs the form again.


Hope I was clear enough.
Any questions, just ask.

Thanks for any help at all :) Virgil

Posted: Sat Jul 06, 2002 2:02 pm
by twigletmac
You just need to set $send_data to begin with so move your if statement thingie up to the top and change it about a bit:

Code: Select all

<?php 
if (isset($page_B)) &#123; // If page_B button has been clicked previously
    $send_data = 'page_B.php'; 
&#125; else &#123; // If nothing has been clicked yet or page_A button has been clicked
    $send_data = 'page_A.php';
&#125;
echo '<form method="POST" action="'.$send_data.'">';
// REST OF FORM
echo '<input type="submit" value="Send to page A" name="page_A">';
echo '<input type="submit" value="Send to page B" name="page_B">';
?>
Although if the page_B button was clicked first time the action would be page_A still. It would probably be a better solution to have one form handling page with either an if..else statement or a switch in it that ran code based on which button had been pressed. It wouldn't be too difficult to check if the password box had been filled in and if it wasn't show the hint and if it had do the login authentication bit.

Mac

Posted: Sat Jul 06, 2002 3:07 pm
by hob_goblin
sounds like a job for JS.. or you could just send it to one page, and in that page.. check what was put in

Code: Select all

if(isset($name) && isset($email) && isset($password))&#123;
//statements
&#125; else if(isset($name) && isset($email))&#123;
//show the hint
&#125; else &#123;
echo "fill in all fields!";
&#125;

Yet another solution

Posted: Sat Jul 06, 2002 3:38 pm
by rokamortis
This is similar to what hob_goblin posted, just a little different. The easiest way I found to do this would be to submit to a single page and either give the buttons the same name and check the value or give the buttons different names and check to see which one isset(). I like using the isset() option better.

Form Code:

Code: Select all

<form method=POST action=submitForm.php> 
<input type="submit" value="Send to page A" name="pageA"> 
<input type="submit" value="Send to page B" name="pageB"> 
</form>
submitForm.php Code:

Code: Select all

if(isset($pageA))&#123;
//We know pageA button was clicked - add relevent code here
&#125; else if(isset($pageB))&#123;
//We know pageB button was clicked - add relevent here
&#125; else &#123;
//Something bad happened - add some error checking code here 
&#125;

Posted: Sat Jul 06, 2002 6:08 pm
by virgil
Hey Php's

I've been screwin' with this for 8 hours straight. 8O I finally gave up and went with rokamortis idea (I actually just shoved a whole copy of page A (picking up the hint value on the way) into page B and to the user it looks like you just updated Page A(except for the title). But on the surface it does have the 2 submits, and looks and acts like I wanted. I was so close, or so it felt. Using a conditional variable for the action= atrub. seems like it would be standard. Alas not. Maybe in a beta of 7.0.


:D MANY THANKS TO ALL FOR THE HELP!!! :D