Retain checkbox and radio button values
Moderator: General Moderators
Retain checkbox and radio button values
Here's a short explanation of what I'm doing.
I've a form that have checkbox, radio button and text field. This form will be submitted to a php file, the purpose of this page is to allow users to review what they have chosen, and if there's an mistake made, there's in a "Edit" button that brings them back to the form.
The problem I'm facing is, I'm unable to retain the selected values in the form.
This is the radio button and checkbox for my form:
<form name="form1" method="post" action="confirm_edit.php">
<input type="checkbox" name="c1" value="screening">
<input type="checkbox" name="c2" value="connecting">
<input name="food" type="radio" value="Fish and Chips">
<input name="food" type="radio" value="Chicken Chop">
</form>
This is confirm_edit.php:
For radio button this is what I have:
if ($room == 'Fish and Chips')
{
print '<b>Food:</b> Fish and Chips <br><br>';
}
else
{
print '<b>Food:</b> Chicken Chop <br><br>';
}
And
For my checkbox this is what I have:
if (isset($_POST['Submit'])) {
print "<b>You chose:</b> <br><br>";
if (isset($_POST['c1'])) {
$c1 = $_POST['c1'];
if ($c1 == 'screening') {
print "-Screening<br><br>";
}
}
if (isset($_POST['c2'])) {
$c2 = $_POST['c2'];
if ($c2 == 'connecting') {
print "-Connecting <br><br>";
}
}
I have this at the end of confirm_edit.php page:
<form name="form1" method="post" action="">
<input type="submit" name="Submit" value="CONFIRM BOOKING">
<a href="booking.php">Edit
</a>
</form>
So, it's like this when i press this "Edit" it will return to the form and I needed those selected values to retain.
Thanks.
I've a form that have checkbox, radio button and text field. This form will be submitted to a php file, the purpose of this page is to allow users to review what they have chosen, and if there's an mistake made, there's in a "Edit" button that brings them back to the form.
The problem I'm facing is, I'm unable to retain the selected values in the form.
This is the radio button and checkbox for my form:
<form name="form1" method="post" action="confirm_edit.php">
<input type="checkbox" name="c1" value="screening">
<input type="checkbox" name="c2" value="connecting">
<input name="food" type="radio" value="Fish and Chips">
<input name="food" type="radio" value="Chicken Chop">
</form>
This is confirm_edit.php:
For radio button this is what I have:
if ($room == 'Fish and Chips')
{
print '<b>Food:</b> Fish and Chips <br><br>';
}
else
{
print '<b>Food:</b> Chicken Chop <br><br>';
}
And
For my checkbox this is what I have:
if (isset($_POST['Submit'])) {
print "<b>You chose:</b> <br><br>";
if (isset($_POST['c1'])) {
$c1 = $_POST['c1'];
if ($c1 == 'screening') {
print "-Screening<br><br>";
}
}
if (isset($_POST['c2'])) {
$c2 = $_POST['c2'];
if ($c2 == 'connecting') {
print "-Connecting <br><br>";
}
}
I have this at the end of confirm_edit.php page:
<form name="form1" method="post" action="">
<input type="submit" name="Submit" value="CONFIRM BOOKING">
<a href="booking.php">Edit
</a>
</form>
So, it's like this when i press this "Edit" it will return to the form and I needed those selected values to retain.
Thanks.
Re: Retain checkbox and radio button values
Edit is a link and isn't submitting the form, first of all. Second, even if it did, $_POST would be replaced by the contents of the new form. You'd need to add the $_POST values from the first page to the form in the second page, even if by hidden input. You'll then want two submit buttons (or links) maybe with some javascript to determine the form's action based on which button was pressed.
Re: Retain checkbox and radio button values
Not really related to your question, but I noticed the above when looking at your code. What if $room equals something other than those 2 options?abze36 wrote:Code: Select all
if ($room == 'Fish and Chips') { print '<b>Food:</b> Fish and Chips <br><br>'; } else { print '<b>Food:</b> Chicken Chop <br><br>'; }
Re: Retain checkbox and radio button values
Oh, that was a mistake. It's suppose to be food.
I did have a $_POST in the first page, and $_GET in the second page. I have research on many ways for codes, but it doesn't seem to work for my case. Or is adding session_start () might help?
I did have a $_POST in the first page, and $_GET in the second page. I have research on many ways for codes, but it doesn't seem to work for my case. Or is adding session_start () might help?
Re: Retain checkbox and radio button values
That wasn't really the point.abze36 wrote:Oh, that was a mistake. It's suppose to be food.
I don't see a $_GET in the second page. Either way, that's not important. If you want to send them back to the original form with the values they entered, you need to send those values back as well. There are many ways you could do that; populating a form, using session data, or even cookies.abze36 wrote:I did have a $_POST in the first page, and $_GET in the second page. I have research on many ways for codes, but it doesn't seem to work for my case. Or is adding session_start () might help?
Re: Retain checkbox and radio button values
Okay, so if I'm using session, is there anything i need to change for the input type="checkbox" name="c1" value="screening"> and <input name="food" type="radio" value="Fish and Chips">? or there's a need to change the coding in my second page?
Sorry for the trouble, I'm kinda confuse on how to do it.
Sorry for the trouble, I'm kinda confuse on how to do it.
Re: Retain checkbox and radio button values
If you want to store information in session data, you'll need session_start, and then you'll need to save things to $_SESSION. I still think you'd do better using $_POST.
Re: Retain checkbox and radio button values
I'm using $_POST now, but when i click on "Edit" to return to the page, the selected values are not retain.
Re: Retain checkbox and radio button values
Have you populated the form on the second page with the values from the first page?
Re: Retain checkbox and radio button values
Yes, I have already done so.
Re: Retain checkbox and radio button values
Post your code (in a code box, please) and let's take a look.
Re: Retain checkbox and radio button values
This is the first page (form):
This is the second page:
Anyway, I did not $_POST the checkbox, i only do it for the radio button
Code: Select all
<?php
$room = $_GET['room'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="form.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="post" action="booking_form_now.php">
<p class="Title">BOOKING OF AUDITORIUM</p>
<table width="490" height="93" border="0">
<tr>
<td colspan="6"><span class="Heading">ROOM ARRANGMENT</span></td>
</tr>
<tr>
<td width="107"><span class="Content">U-Shaped</span></td>
<td width="42"><span class="Content">
<input name="room" type="radio" value="u_shaped">
</span></td>
<td width="103"><span class="Content">Standard</span></td>
<td width="41"><span class="Content">
<input name="room" type="radio" value="standard">
</span></td>
<td width="130"><span class="Content">Panel Speakers</span></td>
<td width="41"><span class="Content">
<input name="room" type="radio" value="panel_speakers">
</span></td>
</tr>
<tr>
<td><span class="Content">Exam </span></td>
<td><span class="Content">
<input name="room" type="radio" value="exam">
</span></td>
<td><span class="Content">Cluster</span></td>
<td><span class="Content">
<input name="room" type="radio" value="cluster">
</span></td>
<td><span class="Content">Fish-Bone</span></td>
<td><span class="Content">
<input name="room" type="radio" value="fish_bone">
</span></td>
</tr>
</table>
<table width="787" height="132" border="0">
<tr>
<td height="37" colspan="8"><p class="Heading"> INSTRUCTIONAL EQUIPMENT REQUIREMENTS</p></td>
</tr>
<tr>
<td width="160" height="37"><span class="Content">Project/Screen </span></td>
<td width="25"><span class="Content">
<input type="checkbox" name="c1" value="project/screen">
</span></td>
<td width="160"><span class="Content">Internet Connection</span></td>
<td width="25"><span class="Content">
<input type="checkbox" name="c2" value="internet connection">
</span></td>
<td width="160"><span class="Content">Rostrum Monitor</span></td>
<td width="25"><span class="Content">
<input type="checkbox" name="c3" value="rostrum monitor">
</span></td>
<td width="160"><span class="Content">White Board</span></td>
<td width="25"><span class="Content">
<input type="checkbox" name="c4" value="white board">
</span></td>
</tr>
<tr>
<td><span class="Content">Slide Projector </span></td>
<td><span class="Content">
<input type="checkbox" name="c5" value="slide projector">
</span></td>
<td><span class="Content">VCR Player</span></td>
<td><span class="Content">
<input type="checkbox" name="c6" value="vcr player">
</span></td>
<td><span class="Content">Table Mic (for speakers)</span></td>
<td><span class="Content">
<input type="checkbox" name="c7" value="table mic (for speakers)">
</span></td>
<td><span class="Content">Flipchart Board</span></td>
<td><span class="Content">
<input type="checkbox" name="c8" value="flipchart board">
</span></td>
</tr>
<tr>
<td><span class="Content">Visualizer</span></td>
<td><span class="Content">
<input type="checkbox" name="c9" value="visualizer">
</span></td>
<td><span class="Content">DVD/VCD Player</span></td>
<td><span class="Content">
<input type="checkbox" name="c10" value="dvd/vcd player">
</span></td>
<td><span class="Content">Table Mic (for participants)</span></td>
<td><span class="Content">
<input type="checkbox" name="c11" value="table mic (for participants)">
</span></td>
<td><span class="Content">Laser Pointer</span></td>
<td><span class="Content">
<input type="checkbox" name="c12" value="laser pointer">
</span></td>
</tr>
<tr>
<td><span class="Content">OHP</span></td>
<td><span class="Content">
<input type="checkbox" name="c13" value="ohp">
</span></td>
<td><span class="Content">Cassette Player</span></td>
<td><span class="Content">
<input type="checkbox" name="c14" value="cassette player">
</span></td>
<td><span class="Content">Hand-Held Mic</span></td>
<td><span class="Content">
<input type="checkbox" name="c15" value="hand-held mic">
</span></td>
<td><span class="Content">Voice Recorder</span></td>
<td><span class="Content">
<input type="checkbox" name="c16" value="voice recorder">
</span></td>
</tr>
<tr>
<td><span class="Content">Laptop </span></td>
<td><span class="Content">
<input type="checkbox" name="c17" value="laptop">
</span></td>
<td><span class="Content">Stage Monitor</span></td>
<td><span class="Content">
<input type="checkbox" name="c18" value="stage monitor">
</span></td>
<td><span class="Content">Clip-On-Mic</span></td>
<td><span class="Content">
<input type="checkbox" name="c19" value="clip-on mic">
</span></td>
<td> </td>
<td> </td>
</tr>
</table>
<p class="Content">
<input type="submit" name="Submit" value="Proceed">
</p>
</form>
</body>
</html>
Code: Select all
<?php
$room = $_POST['room'];
print "<u>REQUIREMENTS</u><br><br>";
if($room == 'u_shaped')
{
print '<b>Room Arangement:</b> U-Shaped <br><br>';
}
elseif ($room == 'standard')
{
print '<b>Room Arangement:</b> Standard <br><br>';
}
elseif ($room == 'panel_speakers')
{
print '<b>Room Arangement:</b> Panel Speakers <br><br>';
}
elseif ($room == 'exam')
{
print '<b>Room Arangement:</b> Exam <br><br>';
}
elseif ($room == 'cluster')
{
print '<b>Room Arangement:</b> Cluster <br><br>';
}
else
{
print '<b>Room Arangement:</b> Fish-Bone <br><br>';
}
if (isset($_POST['Submit'])) {
print "<b>Equipment:</b> <br><br>";
if (isset($_POST['c1'])) {
$c1 = $_POST['c1'];
if ($c1 == 'project/screen') {
print "-Project/Screen<br><br>";
}
}
if (isset($_POST['c2'])) {
$c2 = $_POST['c2'];
if ($c2 == 'internet connection') {
print "-Internet Connection <br><br>";
}
}
if (isset($_POST['c3'])) {
$c3 = $_POST['c3'];
if ($c3 == 'rostrum monitor') {
print "-Rostrum Monitor <br><br>";
}
}
if (isset($_POST['c4'])) {
$c4 = $_POST['c4'];
if ($c4 == 'white board') {
print "-$c4 <br><br>";
}
}
if (isset($_POST['c5'])) {
$c5 = $_POST['c5'];
if ($c5 == 'slide projector') {
print "-Slide Projector <br><br>";
}
}
if (isset($_POST['c6'])) {
$c6 = $_POST['c6'];
if ($c6 == 'vcr player') {
print "-VCR Player <br><br>";
}
}
if (isset($_POST['c7'])) {
$c7 = $_POST['c7'];
if ($c7 == 'table mic (for speakers)') {
print "-Table Mic (for speakers) <br><br>";
}
}
if (isset($_POST['c8'])) {
$c8 = $_POST['c8'];
if ($c8 == 'flipchart board') {
print "-Flipchart Board <br><br>";
}
}
if (isset($_POST['c9'])) {
$c9 = $_POST['c9'];
if ($c9 == 'visualizer') {
print "-Visualizer <br><br>";
}
}
if (isset($_POST['c10'])) {
$c10 = $_POST['c10'];
if ($c10 == 'dvd/vcd player') {
print "-DVD/VCD Player <br><br>";
}
}
if (isset($_POST['c11'])) {
$c11 = $_POST['c11'];
if ($c11 == 'table mic (for participants)') {
print "-Table Mic (for participants) <br><br>";
}
}
if (isset($_POST['c12'])) {
$c12 = $_POST['c12'];
if ($c12 == 'laser pointer') {
print "-Laser Pointer <br><br>";
}
}
if (isset($_POST['c13'])) {
$c13 = $_POST['c13'];
if ($c13 == 'ohp') {
print "-OHP <br><br>";
}
}
if (isset($_POST['c14'])) {
$c14 = $_POST['c14'];
if ($c14 == 'cassette player') {
print "-Cassette Player <br><br>";
}
}
if (isset($_POST['c15'])) {
$c15 = $_POST['c15'];
if ($c15 == 'hand-held mic') {
print "-Hand-Held Mic <br><br>";
}
}
if (isset($_POST['c16'])) {
$c16 = $_POST['c16'];
if ($c16 == 'voice recorder') {
print "-Voice Recorder <br><br>";
}
}
if (isset($_POST['c17'])) {
$c17 = $_POST['c17'];
if ($c17 == 'laptop') {
print "-Laptop <br><br>";
}
}
if (isset($_POST['c18'])) {
$c18 = $_POST['c18'];
if ($c18 == 'stage monitor') {
print "-Stage Monitor <br><br>";
}
}
if (isset($_POST['c19'])) {
$c19 = $_POST['c19'];
if ($c19 == 'clip-on mic') {
print "-Clip-On-Mic <br><br>";
}
}
}
?>
<form name="form1" method="post" action="">
<input type="submit" name="Submit" value="CONFIRM BOOKING">
<a href="booking_form.php">Edit
</a>
</form>
Re: Retain checkbox and radio button values
The initial problem remains; the form at the end of the second page contains no values. Either submitting the form or clicking the edit link will cause everything entered on the form in the first page to be lost. Try adding something like this to the form on the second page:
When the user submits the form, the values will be retained. Of course, since the form on the second page has no action, it will just reload the page and display the same results again. For the edit link to work, you'll need to make it submit the form with the first page as an action. Then, in the first page, you'll want to set the values of all the elements on the form to $_POST['whatever_name'].
Hope that helps a little.
Code: Select all
foreach ($_POST as $k => $v)
{
echo "<input type=\"hidden\" name=\"{$k}\" value=\"{$v}" />";
}Hope that helps a little.
Re: Retain checkbox and radio button values
I'm trying it out right now. But am I suppose to change the radio button and checkbox to array in order to use foreach? or I can just add it in to my current code?
Re: Retain checkbox and radio button values
They're already part of the $_POST array.