Jazzy 1012
asked on
How do I put a session for a radio button?
I have this in my php code:
All the sessions come back except for my "gender" radio button, I tried putting :
value="<?=formDisplay(getS essionValu e("er_emai l"))?>" on each but it didnt work, how could I do this?
if(mysqli_num_rows($result) != 0)
{
$_SESSION['er_firstname'] = $firstname;
$_SESSION['er_lastname'] = $lastname;
$_SESSION['er_email'] = $email;
$_SESSION['er_cemail'] = $cemail;
$_SESSION['er_birthday'] = $birthday;
$_SESSION['er_gender'] = $gender; //do these
header("Location: index.php/?a=1");
}
Here it is my form that echos the sessions if there is already a row like it in the database: <form class="form-horizontal" action="add.php" method="post" onsubmit="return ValidationEvent()">
<fieldset>
<legend>SIGN UP! <i class="fa fa-pencil pull-right"></i></legend>
<div class="form-group">
<?php
if($_GET['a'] == 1)
{
echo "<p class='alert alert-danger'>Username Already taken</p>";
}
if($_GET['a'] == 2)
{
echo " <p class= 'alert alert-success'> Your registration is complete </p>";
}
?>
<div class="col-sm-6">
<input type="text" id="firstName" placeholder="First Name" class="form-control" name="firstname" value="<?=formDisplay(getSessionValue("er_firstname"))?>" autofocus required>
</div>
<div class="col-sm-6">
<input type="text" id="lastname" placeholder="Last Name" class="form-control" name="lastname" value="<?=formDisplay(getSessionValue("er_lastname"))?>" autofocus required>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="email" id="email" placeholder="Email" name="email" class="form-control" value="<?=formDisplay(getSessionValue("er_email"))?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="email" id="cemail" placeholder=" Re-enter Email" name="cemail" class="form-control" value="<?=formDisplay(getSessionValue("er_cemail"))?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" id="username" placeholder=" Username" name="username" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="password" id="password" placeholder="Password" name="password" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" id="datepicker" placeholder= "DOB" name="birthday" class="form-control" value="<?=formDisplay(getSessionValue("er_birthday"))?>" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1"></label>
<div class="col-sm-8">
<div class="row">
<label class="radio-inline">
<input type="radio" id="radio" value="Female" name= "gender" required>Female
</label>
<label class="radio-inline">
<input type="radio" id="radio" value="Male" name= "gender">Male
</label>
</div>
</div>
</div> <!-- /.form-group -->
<div class="form-group">
<div class="col-sm-4 col-sm-offset-3">
<button type="submit" class="btn btn-primary btn-block">Register</button>
</div>
</div>
</form>
All the sessions come back except for my "gender" radio button, I tried putting :
value="<?=formDisplay(getS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This appears to be similar to your other question. Here is that earlier code snippet, modified to take the session into account. In this example we look to the GET request for the source of the gender information, but it could just as easily be the database. The main advantage of using GET is that we can test from the browser address bar, so it's a little easier to see the behaviors, when compared to the database.
<?php // demo/temp_jasmine.php
/**
* https://www.experts-exchange.com/questions/28993540/How-do-I-put-a-session-for-a-radio-button.html
* https://www.experts-exchange.com/questions/28992406/Change-text-to-radio-button-and-calendar-form.html
*/
error_reporting(E_ALL);
session_start();
// WHAT GENDERS DO WE RECOGNIZE?
$genders = [ 'f', 'm', '?' ];
// WHAT IS THE DEFAULT GENDER?
$gender = '?';
// TRY TO GET THE GENDER FROM THE PHP SESSION
if (!empty($_SESSION['gender']))
{
$gender = $_SESSION['gender'];
}
// TRY TO OVERRIDE THE DEFAULT/SESSION WITH THE REQUEST VARIABLE
if (!empty($_GET['gender']))
{
$gender = trim(strtolower($_GET['gender']));
if (!in_array($gender, $genders)) $gender = '?';
}
// SET THE GENDER IN THE PHP SESSION
$_SESSION['gender'] = $gender;
// PREPARE THE RADIO BUTTONS
$radio = NULL;
$radio .= 'M: <input type="radio" value="m" name="gender" ';
if ($gender == 'm') $radio .= 'checked';
$radio .= ' /><br>';
$radio .= 'F: <input type="radio" value="f" name="gender" ';
if ($gender == 'f') $radio .= 'checked';
$radio .= ' /><br>';
// PREPARE THE FORM
$form = <<<EOD
<form>
Currently the gender is set to <b>$gender</b><br>
$radio
<input type="submit" />
</form>
EOD;
echo $form;
ASKER
Okay and how do I destroy the session after it has been echo out because if I decided not to register and referesh the page, the session variables are still there, so is there a way to destroy them?
ASKER