1

i made a post form in html, but i have multiple choices so once the user clicks one of the choices then it will redirect them to the page each choice have a different choice can you all advice how can i achieve that? im coding in PHP.

      echo "<form action='home.php' method='post'>";
      echo "<input type='submit'  name='submit' value='Yes'>";
      echo "<input type='submit'  name='submit' value='No'>"; 
      // If no i want this button to redirect to another page instead of home.php
      echo "</form>";

I appreciate any help. I know this is very basic T_T

hellopanda
  • 61
  • 1
  • 5
  • do you need Yes/No value or any other values to be passed to the target page upon submit? – mitkosoft Jan 30 '20 at 08:00
  • can you show us the code for button submission. – Oops Jan 30 '20 at 08:05
  • @Oops, there is no such code. buttons itself are `type='submit'` so they are submitting the form actually. – mitkosoft Jan 30 '20 at 08:06
  • This will done with little javascript –  Jan 30 '20 at 08:06
  • 2
    You shouldn't have two elements with same name in form (i.e. have those named `submit1` and `submit2`). – Tpojka Jan 30 '20 at 08:07
  • @mitkosoft I asked,so that can check the form submission,.. if (isset($_POST['submit'])){ // do something } – Oops Jan 30 '20 at 08:10
  • Does this answer your question? [Navigate to a page on button click](https://stackoverflow.com/questions/29662692/navigate-to-a-page-on-button-click) – freetzyy Jan 30 '20 at 08:14
  • Does this answer your question? [Two submit buttons in one form](https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form) – showdev Jan 30 '20 at 08:16
  • "// If no i want this button to redirect to another page instead of home.php" Just make two anchor links instead of form. Each link should be href-ed with appropriate landing page. – Tpojka Jan 30 '20 at 08:17

2 Answers2

3

First of all change the name of submit fields. In your case both are same. I am giving it as yes and no.

  echo "<input type='submit'  name='yes' value='Yes'>";
  echo "<input type='submit'  name='no' value='No'>"; 

You can redirect to a page by using header functions in php. For this check which button is submitted. If you clicked on yes/no then redirect to your desired page. You can check it by

 if (isset($_POST['yes'])) {
    header("Location: home.php") // redirect to your desired page
 }

 if (isset($_POST['no'])) {
    header("Location: anotherPage.php") // redirect to your desired page
 }
Oops
  • 1,373
  • 3
  • 16
  • 46
2

You can use formaction attribute within your submit buttons:

<form method="post">
    <input type="submit" name="submit" formaction="home.php" value="No">
    <input type="submit" name="submit" formaction="otherPage.php" value="Yes">
</form>

This will submit the form to the desired page, together with any other parameters in your form (if any).

mitkosoft
  • 5,262
  • 1
  • 13
  • 31