-2

NOTE: Possible duplicate question is not helpful, as it does not compensate for multiple options for different pages while also passing form information.

I need to take user input from the current page (for now, this is just a single text field), and there are three buttons. The three buttons are supposed to go to different pages respectively, but all three need the value from the text field to do their job.

Essentially, I need to "pass" the value from the text field to either of the three pages that the buttons should redirect to. Currently, the buttons are unable to redirect as I do not know how to do that.

This is my code thus far:

<html>
<body>

<form name="form" action="" method="post">
Name: <input type="text" name="name" value="<?php echo $name;?>">
</form>
<button type="button">button1</button>
<button type="button">button2</button>
<button type="button">button3</button>

</body>
</html>

No JavaScript please.

moosefoot
  • 133
  • 3
  • 11
  • What you want is to check for a POST request and handle redirect if the post is successful. I'm sure someone will be kind enough to give you a link and mark this as duplicate. – Brian Mar 14 '17 at 01:23
  • I'm afraid you're rewording the question. I was asking _how_ to redirect or retrieve input. – moosefoot Mar 14 '17 at 01:27
  • 1
    Possible duplicate of [How to create an HTML button that acts like a link?](http://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link) – adeam Mar 14 '17 at 01:44
  • ^ no. This question does not take into account handling user input. – moosefoot Mar 14 '17 at 01:53
  • @moosefoot Either you use JavaScript to rewire the form depending on clicked button, or you identify the pressed button from a single PHP entry point and pass the info along within PHP. Once you have decided which solution you want it should be easy to search for relevant examples. – Sheepy Mar 14 '17 at 08:41
  • Possible duplicate of [How to submit a form to two different pages depending on the button clicked, without javascript](http://stackoverflow.com/questions/7056963/how-to-submit-a-form-to-two-different-pages-depending-on-the-button-clicked-wit) – Sheepy Mar 14 '17 at 08:42

3 Answers3

1

The page where a form leads to is defined in its action attribute of the formtag. Just write the filepath + filename into that attribute and after submitting, you'll be redirected to that page and can use the POST parameter values there.

But you'll need to add a submit button - one, not three, and as an input tag

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • How would I make sure that a submission is done only when the user presses one of the three buttons? Where and how are parameters passed? – moosefoot Mar 14 '17 at 01:29
  • Also, the three buttons are needed so I know what the user wants to do, and redirect them to the appropriate page. By using a single button, I am not able to determine _what page_ the user wishes to go to. – moosefoot Mar 14 '17 at 01:30
  • you can use a select menu or an option list instead of those three buttons and add a submit button. There can only be one submit button in a form. Have a look here: https://www.w3schools.com/tags/att_form_method.asp – Johannes Mar 14 '17 at 01:32
  • The example provided can only ever redirect to a single page (in this case, `action_page.php`). This is not applicable to my situation, unless I use three separate forms for each option. That would be ridiculous. – moosefoot Mar 14 '17 at 01:36
  • it can redirect to a .php page/script that isn't displayed but depending on the sent POST parameters can redirect automatically to the desired page (in no time - nobody will notice that there's another script in between). – Johannes Mar 14 '17 at 01:40
1
<?php
if(isset($_POST['button1'])){
    $link='index_1.php?name='.$_POST['name'];
    header('location:'.$link); 
 }elseif(isset($_POST['button2'])){ 
   $link='index_2.php?name='.$_POST['name'];
   header('location:'.$link); 
 } elseif(isset($_POST['button3'])){ 
   $link='index_3.php?name='.$_POST['name']; 
   header('location:'.$link);
 }
?>

<form action="" method="POST">
    Name: <input type="text" name="name" value="">
    <button type="submit" name="button1" value="button1">button1</button>
    <button type="submit" name="button2" value="button2">button2</button>
    <button type="submit" name="button3" value="button3">button3</button>
</form>
Shooter
  • 51
  • 6
  • This solution does not work because I cannot access the value entered into the input once I am redirected. The redirection works great, though. – moosefoot Mar 14 '17 at 03:17
  • chnage the previous php code with this code and you can get the value from url as $_GET['name'] in the redirected page if(isset($_POST['button1'])){ $link='index_1.php?name='.$_POST['name']; header('location:'.$link); } elseif(isset($_POST['button2'])){ $link='index_2.php?name='.$_POST['name']; header('location:'.$link); } elseif(isset($_POST['button3'])){ $link='index_3.php?name='.$_POST['name']; header('location:'.$link); } – Shooter Mar 14 '17 at 03:50
  • The code was very hard to read because it was not formatted, but this worked beautifully! Thank you. – moosefoot Mar 14 '17 at 04:09
  • you're welcome :) am sorry for the bad format i'll edit it in the main answer :) – Shooter Mar 14 '17 at 04:11
0

Use the submit type, and the check the value for the submitted (or the name you want to put it) in the PHP file, if the value is "button1" do something, if its "button2" etc.

<form action="/action_page.php" method="POST">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<button type="submit" name="submited" value="button1">button1</button>
<button type="submit" name="submited" value="button2">button2</button>
<button type="submit" name="submited" value="button3">button3</button>

</form>