How is it possible to keep your form values, including drop down boxes and radio buttons, after you submit a form and return an error. The values stay for the text boxes, but no the drop down and radio button. Confused?
PHP Form
What you need to do is put a few little conditional statements in your code to select the appropriate value in the event of an error. EG
<?php
if (!error)
{
do processing
}
else
{
print_form($_POST);
}
function print_form($v)
{
?>
<form action ="" method="post">
<input type="radio" name="radio_button" value="1" <?php if($v['radio_button']==1) echo " selected ";?>>
<input type="radio" name="radio_button" value="2" <?php if($v['radio_button']==2) echo " selected ";?>>
<input type="radio" name="radio_button" value="3" <?php if($v['radio_button']==3) echo " selected ";?>>
</form>
<?php
}
?>
Or something similar.
But what if your whole form is already inside of <?php and ?>.
It doesn't matter...The important part is:
if($v['radio_button']==1) echo " selected ";
so if you are echoing your input field code:
<?php
echo '<input type="radio" name="radio_button" value="1"';
if($v['radio_button']==1) echo " selected ";echo '>';
echo '<input type="radio" name="radio_button" value="2"';
if($v['radio_button']==2) echo " selected ";echo '>';
etc
?>
Its not the prettiest way of doing it.
Here is the code. How would that work here?
<?php
$to = "example@example.com";
if (!isset($_POST['submit'])) {
showForm();
} else { //form submitted
$error = 0;
if(empty($_POST['name'])) {
$error = 1;
$errstr[] = "Please enter your full name/family name";
}
if($_POST['rsvpc'] == "Yes" || $_POST['rsvpc'] == "No") {
}else{
$error = 1;
$errstr[] = "Please choose if you will be attending the ceremony";
}
if($_POST['rsvpc'] == "Yes") {
if($_POST['totalc'] == "0") {
$error = 1;
$errstr[] = "Please choose how many ceremony attendees you will have";
}
}
if($_POST['rsvpc'] == "No") {
$_POST['totalc'] = "0";
}
if($_POST['rsvpr'] == "Yes" || $_POST['rsvpr'] == "No") {
}else{
$error = 1;
$errstr[] = "Please choose if you will be attending the reception";
}
if($_POST['rsvpr'] == "Yes") {
if($_POST['totalr'] == "0") {
$error = 1;
$errstr[] = "Please choose how many reception attendees you will have";
}
}
if($_POST['rsvpr'] == "No") {
$_POST['totalr'] = "0";
}
if(empty($_POST['imagetext'])) {
$error = 1;
$errstr[] = "Please validate the image code";
} else {
include "securimage.php";
$img = new securimage();
$valid = $img->check($_POST['imagetext']);
if(!$valid) {
$error = 1;
$errstr[] = "The code you entered was incorrect";
}
}
if ($error == 1) {
echo "<span style='color:#FF0000; font-size:13px; font-weight:bold;'>ERROR:</span><BR><span style='color:#FF0000;'>";
foreach($errstr as $err) {
echo $err . "<BR>";
}
echo "</span><BR>";
showForm();
} else {
@mail($to, "RSVP - " . $_POST['name'] , "Full Name/Family Name: " . $_POST['name'] . "\n" . "Attending Ceremony?: " . $_POST['rsvpc'] . "\n" . "Ceremony Attendees: " . $_POST['totalc'] . "\n" . "Attending Reception?: " . $_POST['rsvpr'] . "\n" . "Reception Attendees: " . $_POST['totalr'] . "\n" . "Comments: " . stripslashes($_POST['comments']), "From: domain.net <noreply@domain.net>");
echo "<span style='color:#4F789F; font-size:30px;'>Thank You</span><BR><span style='color:#FF0000; font-size:25px;'>Submitted Successfully</span><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>";
}
} //else submitted
function showForm()
{
$_POST['message'] = @htmlspecialchars(@$_POST['message']);
echo <<<EOD
<form method="POST">
<b>Full Name/Family Name:</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<input type"text" name="name" value="{$_POST['name']}">
<BR><BR>
<b>Will you be attending?</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<BR>
Ceremony<BR>
<input type="radio" name="rsvpc" value="Yes">
Yes
<BR>
<SELECT NAME="totalc">
<OPTION value="0">- Select -</OPTION>
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
<OPTION value="6">6</OPTION>
<OPTION value="7">7</OPTION>
<OPTION value="8">8</OPTION>
<OPTION value="9">9</OPTION>
<OPTION value="10">10</OPTION>
</SELECT>
Number of Attendees (Including yourself)
<BR>
<input type="radio" name="rsvpc" value="No">
No
<BR><BR>
Reception<BR>
<input type="radio" name="rsvpr" value="Yes">
Yes
<BR>
<SELECT NAME="totalr">
<OPTION value="0">- Select -</OPTION>
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
<OPTION value="6">6</OPTION>
<OPTION value="7">7</OPTION>
<OPTION value="8">8</OPTION>
<OPTION value="9">9</OPTION>
<OPTION value="10">10</OPTION>
</SELECT>
Number of Attendees (Including yourself)
<BR>
<input type="radio" name="rsvpr" value="No">
No
<BR><BR>
<b>Comments:</b><BR>
<textarea name="comments" rows="5" cols="30">{$_POST['comments']}</textarea>
<BR><BR>
<h3>Spam Protection</h3>
<img src="securimage_show.php" style="border: none;"><BR>
<b>Enter code above:</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<input type="text" name="imagetext" /><BR><BR>
<input style="width: 125px;" type="submit" name="submit" value="Send RSVP"> <input type="reset" value="Clear" >
</form>
EOD;
}
?>
To be honest I'm not overly familiar with the Heredoc syntax, but I guess you'd have to escape out of the block of code in order to do the if statement.
I guess I could just use echo ""; for every line on the form, then it would work. Is there anything bad about using that many echo's?
There's no reason why you cant do it that way. Either that or use the ?> and <?php tags like I did in the first example. There might be a slight difference in speed, but I doubt you're going to notice it.
If you have large sections of HTML, it's probably better to just escape out of PHP.
Otherwise, you could also turn the form into one large echo() statement. Honestly, with the small amount of data you have, I doubt it matters either way, though escaping out of PHP would probably be the more efficient way to do it.