I do not know what im doing wrong. I have been looking through other forums that say my problem might be related to not having closed curly braces closed or a short php tag <? .... I have none of these as far as I can tell. This is a form that lets you know if any fields are left blank.
<?php
if (count($_POST) > 0)
{
function check_if_field_submitted($field_to_check)
{
if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
{
return TRUE;
}
else
{
return "YOU MUST FILL IN THE $field_to_check FIELD!";
}
}
//--------------------------------------------------------
$error_messages = array();
//Validate the input
//Trim the fields
$_POST['first_name'] = trim($_POST['first_name']);
$_POST['last_name'] = trim($_POST['last_name']);
$_POST['comments'] = trim($_POST['comments']);
$_POST['first_name'] = strip_tags($_POST['first_name']);
$_POST['last_name'] = strip_tags($_POST['last_name']);
$_POST['comments'] = strip_tags($_POST['comments']);
//Required fields:
if (check_if_field_submitted('first_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('first_name');
}
if (check_if_field_submitted('last_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('last_name');
}
if (check_if_field_submitted('hobbies') !== TRUE)
{
$error_messages[] = check_if_field_submitted('hobbies');
}
if (check_if_field_submitted('university') !== TRUE)
{
$error_messages[] = check_if_field_submitted('university');
}
if (check_if_field_submitted('year') !== TRUE)
{
$error_messages[] = check_if_field_submitted('year');
}
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
?>
<DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($error_messages) && count($error_messages) > 0)
{
echo "<ul>";
foreach($error_messages as $message)
{
echo "<li>$message</li>";
}
echo "</ul>";
}
?>
<h1>Register or Fail</h1>
<form method="post" action="index.php">
<fieldset>
<label>First Name</label>
<input type='text' name='first_name' value="<?php if(isset($_POST['first_name'])) { echo $_POST['first_name']; } ;?>" />
<label>Last Name</label>
<input type='text' name='last_name' value='<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ;?>'/>
</fieldset>
<fieldset>
<label>What are your hobbies?</label>
<input type="checkbox" name="hobbies" value="movies" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies
<input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports
<input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books
<input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games
<input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'science') { echo "checked='checked'"; } ?> /> FOR SCIENCE!
</fieldset>
<fieldset>
<label>What year are you?</label>
<input type="radio" name="year" value="1" <?php if(isset($_POST['year']) && $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman
<input type="radio" name="year" value="2" <?php if(isset($_POST['year']) && $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore
<input type="radio" name="year" value="3" <?php if(isset($_POST['year']) && $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior
<input type="radio" name="year" value="4" <?php if(isset($_POST['year']) && $_POST['year'] == '4') { echo "checked='checked'"; } ?> /> Senior
</fieldset>
<fieldset>
<label>What university are you attending?</label>
<select name="university">
<option value="" <?php if(isset($_POST['university']) && $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option>
<option value="1" <?php if(isset($_POST['university']) && $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option>
<option value="2" <?php if(isset($_POST['university']) && $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option>
<option value="3" <?php if(isset($_POST['university']) && $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option>
<option value="4" <?php if(isset($_POST['university']) && $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option>
</select>
</fieldset>
<fieldset>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
Problem:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
Answer:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
If I were you I would find an IDE with bracket matching/highlighting.
It looks like you're missing a closing brace on one of your if statements.
I think you want to change these lines:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
to this:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}
Related
I need help with enableing/disableing parts of my website.
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS'])) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
Everytime I uncheck the box it checks itself again...
Check if this works for you:
<?php
$disabled = array();
$disabled['SRS'] = isset($_POST['SRS']) && (int)$_POST['SRS'] === 1;
$disabled['Bilder'] = isset($_POST['Bilder']) && (int)$_POST['Bilder'] === 1;
$disabled['KWS'] = isset($_POST['KWS']) && (int)$_POST['KWS'] === 1;
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" value="1" <?php if($disabled['SRS'] === true) echo "checked"; ?> name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" value="1" <?php if($disabled['Bilder'] === true) echo "checked"; ?> name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" value="1" <?php if($disabled['KWS'] === true) echo "checked"; ?> name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if ($disabled['SRS'] === true) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
We control what checkbox is checked by using the (guess!) checked attribute. If you are using XHTML then change checked to checked="checked"
A different but similar approach with submit onclick.
<?php
$check_SRS = "";
$check_Bilder = "";
$check_KWS = "";
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) { $check_SRS = "checked";}
if (isset($_POST['Bilder']) && intval($_POST['Bilder']) == 1) { $check_Bilder = "checked";}
if (isset($_POST['KWS']) && intval($_POST['KWS']) == 1) { $check_KWS = "checked";}
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_SRS); ?>>SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" value="1" onclick="submit()" <?php echo htmlspecialchars($check_Bilder); ?>>Bilder</input>
<input type="checkbox" name="KWS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_KWS); ?>>Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
A version of elseif statements to accommodate what are assumed to be the other includes depending on the state of the checkboxes - if one only can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}elseif(intval($_POST['SRS']) != 1){
include'SRS.php';
}elseif(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}elseif(intval($_POST['Bilder']) != 1){
include'Bilder.php';
}elseif(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}elseif(intval($_POST['KWS']) != 1){
include'KWS.php';
}
?>
if more than one can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}else{
include'SRS.php';
}
if(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}else{
include'Bilder.php';
}
if(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}else{
include'KWS.php';
}
?>
How can i show value when checkbox was checked using loop for ?
.....................................................................................................................................................................
<form method="post">
<input type="checkbox" name="a_0" value="0">0
<input type="checkbox" name="a_1" value="1">1
<input type="checkbox" name="a_2" value="2">2
<input type="checkbox" name="a_3" value="3">3
<input type="checkbox" name="a_4" value="4">4
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<4;$i++)
{
if(${"_POST[a_{$i}]"} != '')
{ echo ${"_POST[a_{$i}]"}; }
else
{ echo "no"; }
}
}
?>
Change:
${"_POST[a_{$i}]"}
To:
$_POST['a_' . $i]
Or (note the double quotes):
$_POST["a_$i"]
Try like this:
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<4;$i++)
{
if(${"_POST[a_{$i}]"} != '')
{ echo $_POST['a_{$i}']; }
else
{ echo "no"; }
}
}
?>
I'm writing a php form and I can't get the drop down boxes and check boxes to stick as in when I fill in my details but don't click something it will keep everything else filled but that one part that wasn't filled out. I can do it for the input text and radio buttons but I can't get it done for drop downs and checkboxes
<!DOCTYPE html>
<style>
#import url(stickyTextInput.css);
</style>
<?php
if(isset($_REQUEST["left"]))
{
process_form();
}
else
{
display_form_page('');
}
?>
<?php
function display_form_page($error)
{
$self =$_SERVER['PHP_SELF'];
$first_name = isset($_REQUEST['name']) ? $_REQUEST['name']:'';
$last_name = isset($_REQUEST['lastname']) ? $_REQUEST['lastname']:'';
$age = isset($_REQUEST['age']) ? $_REQUEST['age']:'';
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']: '';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head>
<title>
Forms Sticky input
</title>
<style>
#import url(stickyTextInput.css);
</style>
<style type="text/css">
.error
{
color:#ff0000
}
</style>
</head>
<body>
<?php
if($error)
{
echo "<p>$error</p>\n";
}
?>
<form action= "<?php echo $self?>" method = "post">
<h1>Forms-Sticky Input</h1>
<label>First Name:</label>
<input type="text" size="10" maxlength="40" name="name" value = "<?php echo $first_name?>">
<br>
<label>Last Name:</label>
<input type="text" size="10" maxlength="40" name="lastname" value = "<?php echo $last_name?>">
<br>
<label>Age:</label>
<input type="text" name="age" size="10" value="<?php echo $age?>">
<br>
<label>Gender</label>
<input type="radio" name="gender" value="male" <?php check($gender, "male")?>>Male
<input type="radio" name="gender" value="female" <?php check ($gender, "female")?>>Female
<br>
<label>Select favourite Colour</label>
<select name= "color">
<option <?php checkradio($color, "Red")?>>Red
<option <?php checkradio($color, "Blue")?>>Blue
<option <?php checkradio($color, "Green")?>>Green
<option <?php checkradio($color, "Pink")?>>Pink
<option selected="selected" disabled="disabled">
</select>
<br>
<label>Food</label>
<input type="checkbox" name="food[]" value="beans" <?php checkbox ($food, "beans")?>>Beans
<input type="checkbox" name="food[]" value="crisps" <?php checkbox ($food, "crisps")?>>Crisps
<input type="checkbox" name="food[]" value="lemons" <?php checkbox ($food, "lemons")?>>Lemons
<br>
<div id="buttons">
<input type="submit" name="left" id="left" value="Submit" >
<input type="reset" name="right" id="right" value="Reset" >
</div>
</form>
</body>
</html>
<?php
}
?>
<?php
// If $group has the value $val then select this list item
function check($group, $val)
{
if ($group === $val)
{
echo 'checked = "checked"';
}
}
?>
<?php
function checkradio($group, $val)
{
if ($group === $val)
{
echo 'selected = "selected"';
}
}
?>
<?php
// If $group has the value $val then select this list item
function checkbox($group, $val)
{
if ($group === $val)
{
echo 'checked = "checked"';
}
}
?>
<?php
function process_form()
{
$error = validate_form();
if($error)
{
display_form_page($error);
}
else
{
display_output_page();
}
}
?>
<?php
function validate_form()
{
$first_name = trim($_REQUEST['name']);
$last_name = trim($_REQUEST['lastname']);
$age = trim($_REQUEST['age']);
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
$error = '';
$reg_exp = '/^[a-zA-Z\-\']+$/';
$reg_exp1 = '[0-9]{3}';
if(!preg_match($reg_exp, $first_name))
{
$error .= "<span class=\"error\">First Name is invalid (letters, hyphens, ', only)</span><br>";
}
if (!preg_match($reg_exp, $last_name))
{
$error .= "<span class=\"error\">Last Name is invalid (letters, hyphens, ', only)</span><br>";
}
if (!is_numeric($age))
{
$error .= "<span class=\"error\">Age is invalid (numbers only)</span><br>";
}
if (strlen($gender) == 0)
{
$error .= "<span class=\"error\">Select Male/Female</span><br>";
}
if (strlen($color) == 0)
{
$error .= "<span class=\"error\">Select one color</span><br>";
}
if (! is_array($food))
{
$error .= "<span class=\"error\">You must select one food</span><br>";
}
return $error;
}
?>
<?php
function display_output_page()
{
$first_name = trim($_REQUEST['name']);
$last_name = trim($_REQUEST['lastname']);
$age = trim($_REQUEST['age']);
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head><title>Form Results</title></head>
<body>
<h1>Form Results</h1>
<?php
echo " First Name: $first_name<br/>\n";
echo " Last Name: $last_name<br/>\n";
echo " Age: $age<br/>\n";
echo " Gender: $gender<br/>\n";
echo " Favourite Color: $color<br/>\n";
echo "<ul>";
if (is_array($food))
{
echo "Favourite Food is:";
foreach($food as $selection)
{
echo "<li>$selection</li>";
}
}
echo "</ul>";
?>
</body>
</html>
<?php
}
?>
Possibly a browser issue, however most browsers only require 'checked' at the end of the html input element for checkboxes. However you appear to be outputting checked = "checked" This is possibly the problem. Have a look at the sample below:
<?php
$name = isset($_GET['name']) ? $_GET['name'] : '';
$agree = isset($_GET['agree']) ? 'checked' : '';
$title = isset($_GET['title']) ? $_GET['title'] : '';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="">
<input type="text" name="name" id="name" value="<?=$name;?>">
<input type="checkbox" name="agree" id="agree" <?=$agree;?>>
<select name="title" id="title">
<option value="Mr" <?=$title == 'Mr' ? 'selected' : ''?>>Mr</option>
<option value="Mrs" <?=$title == 'Mrs' ? 'selected' : ''?>>Mrs</option>
<option value="Miss" <?=$title == 'Miss' ? 'selected' : ''?>>Miss</option>
</select>
<button type="submit">submit</button>
</form>
</body>
</html>
After processing the values the output html should be as follows:
<form action="">
<input type="text" name="name" id="name" value="test">
<input type="checkbox" name="agree" id="agree" checked>
<select name="title" id="title">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss" selected>Miss</option>
</select>
<button type="submit">submit</button>
</form>
My intention is to retrieve data from a database and have created the following functions but receive an undefined function, grades, error message. The code is as follows:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select name="gpa">
<option value="4">3.5 or above</option>
<option value="3">3.0-3.4</option>
<option value="2">2.5-2.9</option>
<option value="1">2.0-2.4</option>
<option value="0">Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>
<option value ="2">Literature</option>
<option value ="3">History</option>
<option value ="4">Humanities</option>
<option value ="5">Science</option>
</select>
Then the PHP file that processes the form submission:
<?php
require_once('processor.php');
class Processor {
#code
function get_response($id) {
$dsn = "mysql:host=localhost;dbname=user";
$username = "root"; // database username
$password = "stewie12"; // database password
try {
$enter = new PDO($dsn, $username, $password);
$sql = "SELECT response FROM recommendations WHERE id = ? ";
$new_item = $enter->prepare($sql);
$new_item->setFetchmode(PDO::FETCH_ASSOC);
$new_item->execute(array($id));
foreach($new_item as $nw) {
return $nw['response'];
}
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
return "";
}
function GetWeird () {
$santiago="1992";
if ($santiago="1992") {
$id = 1;
} else {
$id = 2;
}
echo $this->get_response($id);
}
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
And the view page:
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>
To access the class function use below code,
You have to use it like this $class_variable->function_name()
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php $processor->Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php $processor->Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php $processor->School($school); ?></p>
Please how can I get the status of radio buttons based on their value in the database?
I want to set them to either checked or unchecked based on their respective values in the database. field_type is int(1 or 0).
Is there a more efficient way to do this?
$news_alert_status_a = "unchecked";
$news_alert_status_k = "unchecked";
$news_alert_status_n = "unchecked";
$events_alert_status_a = "unchecked";
$events_alert_status_k = "unchecked";
$events_alert_status_n = "unchecked";
$questions_alert_status_a = "unchecked";
$questions_alert_status_k = "unchecked";
$questions_alert_status_n = "unchecked";
$editorials_alert_status_a = "unchecked";
$editorials_alert_status_k = "unchecked";
$editorials_alert_status_n = "unchecked";
if ($news_alert == 1)
{
$news_alert_status_a = "checked";
}
elseif ($news_alert == 2)
{
$news_alert_status_k = "checked";
}
elseif($news_alert == 3)
{
$news_alert_status_n = "checked";
}
if ($events_alert == 1)
{
$events_alert_status_a = "checked";
}
elseif ($events_alert == 2)
{
$events_alert_status_k = "checked";
}
elseif($events_alert == 3)
{
$events_alert_status_n = "checked";
}
if ($questions_alert == 1)
{
$questions_alert_status_a = "checked";
}
elseif ($questions_alert == 2)
{
$questions_alert_status_k = "checked";
}
elseif($questions_alert == 3)
{
$questions_alert_status_n = "checked";
}
if ($editorials_alert == 1)
{
$editorials_alert_status_a = "checked";
}
elseif ($editorials_alert == 2)
{
$editorials_alert_status_k = "checked";
}
elseif($editorials_alert == 3)
{
$editorials_alert_status_n = "checked";
}
The PHP code:
<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['gender'];
if ($selected_radio = = 'male') {
$male_status = 'checked';
}
else if ($selected_radio = = 'female') {
$female_status = 'checked';
}
}
?>
The HTML FORM code:
<FORM name ="form1" method ="post" action ="radioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'
<?PHP print $male_status; ?>
>Male
<Input type = 'Radio' Name ='gender' value= 'female'
<?PHP print $female_status; ?>
>Female
<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>
<?php
if($_POST)
{
if($_POST['gender'] != "")
{
$gender = $_POST['gender'] == "male" ? 1 : 0;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Radio Button</title>
</head>
<body>
<form method="post">
<label for="radio_female">Female</label><input type="radio" name="gender" id="radio_female" value="female"/>
<label for="radio_male">Male</label><input type="radio" name="gender" id="radio_male" value="male"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>