user1574187
user1574187

Reputation: 13

How do I write the PHP code that responds to "yes" and "no" buttons in an HTML form?

<body background="logo1.jpg">
<IMG SRC="ONGC.jpg" ALT="some text" WIDTH=1050 HEIGHT=306>

<form method="post" action="passing.php">
  <br>
  <font size="5" color="800517"><b>&nbsp;PLEASE LOG IN TO ENTER VISITOR DETAILS</b></font>
  <input type="radio" name="yes" value="v1"><b>YES
  <input type="radio" name="yes" value="V2">NO</b>
  </p></p>
  <input type="submit" name="submit" value="approve"/>
  <input type="button" value="Reload Page" onClick="document.location.reload(true)">

</form>

i have written this simple program in html..now my query is how do i write down the php code for yes/no button..i.e if yes button is clicked and submitted then only i will be redirected to passing.php page otherwise it will redirect me back to the same current page..thanks!!!

Upvotes: 0

Views: 6560

Answers (3)

inVader
inVader

Reputation: 1534

$_POST['yes']

Will contain eityer v1 or V2 if the user has made choice. Use isset($_POST['yes']) to see if a selection has been made or if passing.php has been called without the 'yes' parameter. Then check if it is v1, V2 or something else and handle the cases accordingly.

Upvotes: 2

DaveRandom
DaveRandom

Reputation: 88687

HTML:

<input type="radio" name="yes" value="1"><b>YES</b>
<input type="radio" name="yes" value="0"><b>NO</b>

PHP:

if (!empty($_POST['yes'])) {
  // Yes
} else {
  // No
}

Using 1/0 in combination with empty()) in this manner accommodates the user's selection, as well as allowing the value to be completely missing (in which case it defaults to "no").

Yes/no is a boolean (true/false) decision, and it should be treated as such.

Upvotes: 3

knittl
knittl

Reputation: 265443

Check which value got set:

switch($_POST['yes']) {
  case 'v1': // checked 'yes'
  break;
  case 'V2': // checked 'no'
  break;
  default: // didn't check anything
}

You can generate an HTTP redirect with header('Location: http://example.com/page.php'); exit;

Upvotes: 0

Related Questions