...

View Full Version : Using PHP Validation with JavaScript



apowers20
07-07-2005, 09:47 PM
Hi,

A little problem I am facing. What is happening is that a user selects a radio button and then a JavaScript displays the appropriate fieldset with more options for the user to pick.

Upon submitting the form, there is a PHP validation script that is run and if there is an error the page reloads with the error message, but my fieldset that was displayed on the prior page does not show unless I click the radio button again (The radio button is marked already on this page).

My question is:
How do I get my PHP validation to recognize the JavaScript and leave the users selection checked and shown if there is an error? Any help would be much appreciated. Thanks.

The code that I need to say something:

<?php
if ($errors) {
print "<p class='stdfnt_red'>&nbsp;&nbsp;There are errors in the form.<br>&nbsp;&nbsp;Please correct the fields marked in red</p>";

if ($_POST['style'] == 'calsecrx' || 'calsecrx2' || 'calsecrx3' || 'calsecrx3b' || 'calsecrx4' || 'calsecrx4c') {
Display the fieldset with an error;
}
if ($_POST['style'] == 'calsecrx4b') {
Display the fieldset with an error;
}
}
?>

kent
08-05-2005, 07:26 PM
Hey,

This keeps the radio button still selected after an alert is displayed for a
wrong entry in the form. The script at the bottom handles the error message displayed by php while the page was still loading. Hope this helps!

<html lang="en">
<head>
<script type="text/javascript">
var errorinmiddle = 0;
var selectedvalue;

function error(message, selval) {
alert(message);
errorinmiddle = 1;
selectedvalue = selval;
}
</script>
</head>
<body >
<?php
$calsecrx = array("calsecrx1", "calsecrx2", "calsecrx3", "calsecrx4");

if (isset($_REQUEST[formradio]) && $_REQUEST[formradio] != "calsecrx3") {
echo "<p>&nbsp;&nbsp;There are errors in the form.<br>&nbsp;&nbsp;Please correct the fields marked in red</p>";

if($_REQUEST[error] == 0) {
echo "<script type='text/javascript'>";
echo "error('You selected ".$_REQUEST[formradio]." when you need to select calsecrx3', '".$_REQUEST[formradio]."');";
echo "</script>";
}
}
?>
<form method="post" name="userform">
<input type="hidden" name="error" value="0">
<? foreach($calsecrx as $key => $value) { ?>
<? if(isset($_REQUEST[formradio]) && $_REQUEST[formradio] == $value) { ?>
<? if($_REQUEST[error] == 1) { ?>
<input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>" checked/><font color="#FF0000"><?=$value?></font><br>
<? } else { ?>
<input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>" checked/><?=$value?><br>
<? } ?>
<? } else { ?>
<input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>"/><?=$value?><br>
<? } ?>
<? } ?>
<input type="submit" value="submit"/>
</form>
<script type="text/javascript">
if(errorinmiddle) {
errorinmiddle = -1;
window.document.userform.error.value = 1;
window.document.userform.submit();
} else if (errorinmiddle == -1) {
errorinmiddle = 0;
window.document.userform.error.value = 0;
window.document.userform.submit();
}
</script>
</body>
</html>


kent



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum