Page 1 of 1

Get radio buttons to retain info after failed validation

Posted: Tue Sep 05, 2006 8:54 pm
by cocklesby
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I have created a php form which includes validation for mandatory fields. 

My problem is that if validation fails the page refreshes. All the text fields retain the information the user has previously entered, but I don't know how to make it so radio buttons and checkboxes also retain the information.

Could anyone help me out? it would be much appreciated. Here is my code so far:

Code: Select all

<?php	
	$email="gavin@*****";
	$emailsubject="Competition Submission";
	$confirmation="Thank you for entering our competition.";

	$required_fields="First_name,Surname,School,City,Street_and_number,Which_one_of_the_following_best_describes_you,I_agree_to_participate_in_the_evaluation,I_agree_to_the_terms_and_conditions";
	$required_email_fields="Email";
	
	
	$mail_priority="3"; // 1 is high, 3 is normal
//ob_start();
$ax=$_GET["ax"];
if($ax=="submit")
{
	$mail="yes";
	$req_check=preg_split('/,/',$required_fields, -1, PREG_SPLIT_NO_EMPTY);
	$req_run=sizeof($req_check);
	$error_message="";
	for($i=0;$i<$req_run;$i++)
	{
		$cur_field_name=$req_check[$i];
		$cur_field=$_POST[$cur_field_name];
		if($cur_field=="")
		{
			$error_message = $error_message."* You haven't completed \"".$req_check[$i]."\" <br>";
			$mail="no";
			$error_message = str_replace("_", " ", $error_message);
		}
	}
	$email_check = preg_split('/,/',$required_email_fields, -1, PREG_SPLIT_NO_EMPTY);
	$email_run=sizeof($email_check);
	for($i=0;$i<$email_run;$i++)
	{
		$cur_email_name = $email_check[$i];
		$cur_email = $_POST[$cur_email_name];
		if($cur_email=="" || !eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$cur_email))
		{
			$error_message = str_replace("_", " ", $error_message); 
			$error_message = $error_message."* You haven't entered your \"".$email_check[$i]."\" or the email you have entered is not a valid address.<br>";
			$mail="no";
		}
	}
	if($mail=="yes")
	{
		// email			
		$emaildetails = "\n\nDetails as follows:\n";
		foreach($_POST as $key=>$value)
		{
			$value = stripslashes($value);
			if(!substr_count(strtolower($key), "submit"))
			{
				$emaildetails.= "\n".$key.": ".$value;
			}
		}
		$emaildetails = str_replace("_", " ", $emaildetails); 
		$emailmessage = $emailsubject.$emaildetails;
		$emailextra = "From: Website@***.com\n";
		$emailextra.= "X-Priority: $mail_priority\n";
		mail ($email, $emailsubject, $emailmessage, $emailextra);
		$success = true;			
	}
}


?>
<!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=utf-8">
	<title>Competition</title>
	<link rel="stylesheet" href="main.css" type="text/css">


</head>
<body>
			
		<table  class="comp">
		<tr><td>
			
		<?php if(!isset($_GET['ax']) || $error_message != "") { ?>
			<form id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?ax=submit">
			 <?php if($error_message != "") { ?>
<p class="indent"><strong><?php echo $error_message; ?></strong></p>

                    <?php } ?>
				
				
								
	<!-- question 1 -->	
			<table class="questionTable">
				
				<tr>
					<td colspan="2"><b>About you:</b> (* denotes a mandatory field)</td>
				</tr>
			
				<tr class="evenYellow">
					<td class="bob">Your first name: * </td>
					<td><input type="text" name="First_name" class="compTextarea" value="<?php echo $_POST['First_name']; ?>"></td>
				</tr>	
				
				<tr>
					<td class="bob">Your surname: *</td>
					<td><input type="text" name="Surname" class="compTextarea" value="<?php echo $_POST['Surname']; ?>"></td>
				</tr>
				<tr class="evenYellow">
					<td class="bob">Your email address:  *</td>
					<td><input type="text" name="Email" class="compTextarea" value="<?php echo $_POST['Email']; ?>"></td>
				</tr>	
				
				<tr>
					<td colspan="2"><br><b>Your Schools mailing address:</b><br></td>
				</tr>
				
				<tr class="evenYellow">
					<td>Full name of your School: *</td>
					<td><input type="text" name="School" class="compTextarea" value="<?php echo $_POST['School']; ?>"></td>
				</tr>
				<tr>
					<td class="bob">Number and Street (or P O Box): *</td>
					<td><input type="text" name="Street_and_number" class="compTextarea" value="<?php echo $_POST['Street_and_number']; ?>"></td>
				</tr>
				<tr class="evenYellow">
					<td class="bob">Suburb:</td>
					<td><input type="text" name="Suburb" class="compTextarea" value="<?php echo $_POST['Suburb']; ?>"></td>
				</tr>
				<tr>
					<td class="bob">City/Town: *</td>
					<td><input type="text" name="City" class="compTextarea" value="<?php echo $_POST['City']; ?>"></td>
				</tr>
			</table>	
			<table class="questionTable_comp">
			
				<tr>
					<td colspan="2"><b>Which one of the following best describes you? *</b></td>
				</tr>
				
				<tr>
					<td class="radioButton"><input type="radio" value="Principal" name="Which_one_of_the_following_best_describes_you" class="inputRadio"></td>
					<td>A principal</td>
				</tr>	
				
				<tr>
					<td><input type="radio" value="Teacher" name="Which_one_of_the_following_best_describes_you" class="inputRadio"></td>
					<td>A teacher</td>
				</tr>	
				
				<tr>
					<td><input type="radio" value="School administrator" name="Which_one_of_the_following_best_describes_you" class="inputRadio"></td>
					<td>A school administrator</td>
				</tr>	
				
				<tr>
					<td><input type="radio" value="Other" name="Which_one_of_the_following_best_describes_you" class="inputRadio"></td>
					<td>Other (Specify)</td>
				</tr>
				
				<tr>
					<td colspan="2"><textarea name="Role_(Other)" cols="30" rows="2" class="textarea_boxes"></textarea></td>
				</tr>
				</table>

				<table class="questionTable_compend">
				<tr>
					<td><input type="checkbox" name="I_agree_to_participate_in_the_evaluation" class="inputRadio" value="Yes"></td>
					<td>I agree to participate in the evaluation * </td>
				</tr>
				
				<tr>
					<td><input type="checkbox" name="I_agree_to_the_terms_and_conditions" class="inputRadio" value="Yes"></td>
					<td><br>I confirm I am a representative of the above named 	school and I agree to the terms and conditions. *
				</td>
				</tr>



			</table>		
		
				<label class="send"><input type="image" src="i/btn-send.gif" id="send" name="Submit" alt="Submit button"></label>

			</form>
			<? } else { ?>
<p class="indent"><strong><?php echo $confirmation; ?></strong></p>
 <? } ?>
 
 </td></tr>
 </table>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Sep 05, 2006 8:58 pm
by feyd
The following should be added to each of the selected checkboxes and radio buttons.

Code: Select all

checked="checked"

Posted: Wed Sep 06, 2006 12:22 am
by cocklesby
Thanks, but I don't want them checked when they first come to the page. I only want to boxes they checked to remain filled in after a failed validation refreshes the page.

Posted: Wed Sep 06, 2006 1:29 am
by thallish
Check your $_POST['Which_one_of_the_following_best_describes_you'] value. Whatever it returns is what you chould check.
You have to check each of them, but you can make it easier by writing all the radio buttons in a foreach loop.

And rememberr to

Code: Select all

unset()
the variable afterwards :wink:

Posted: Wed Sep 06, 2006 11:55 am
by n00b Saibot
near start, declare a var to hold its value. (In ref. to good coding principles, always declare & instatiate your vars)

Code: Select all

$radioVal = '';
next check for POST submission...

Code: Select all

if(count($_POST) > 0) {
  $radioVal = $_POST['radio'];
next in the HTML...

Code: Select all

<input type="radio" name="radio" value="1" <?php if($radioVal == 1) echo 'checked="true"'; ?> />
<input type="radio" name="radio" value="2" <?php if($radioVal == 2) echo 'checked="true"'; ?> />
<!-- ... and so on -->

Posted: Mon Sep 11, 2006 5:39 pm
by cocklesby
Great, thanks a lot, that worked for me.