Hello everyone:
I used a javascript file to validate user input before form can be submitted.
I used extract function to extract value being submitted to php file. My problem is that after user click the submit button, php script won’t display information being submitted.
I commented out link to javascript link, Submitted information displayed. This make me think can Javascript and PHP co-exist together? I want to use JavaScript to do client-side validation first, then using PHP file to display submitted data.
my project consists of three file a html file, a javascript file and one php file.
if user input an invalid entry on the form, javascript should prompt alert message. If form entry is valid php script should display data submitted.
Please look at my code and give me some suggestion how to make it work. Thanks!
html file code:
[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3. org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Computer Center Account Application Form</title>
<script src=”form_validation.js” type=”text/javascript”></script>
</head>
<body onLoad=”document.account_form.fname.focus()”>
<form name=”account_form” id=”account_form” action=”form_info.php” method=”post” onSubmit=”return form_validation()”>
First Name:
<input type=”text” name=”fname” id=”fname” size=”15″ maxlength=”15″ /><br /><br />
Last Name
<input type=”text” name=”lname” id=”lname” size=”15″ maxlength=”15″ /><br /><br />
<p>
<input type=”submit” value=”submit” />
<input type=”reset” value=”reset”/>
</p>
</form>
</body>
</html>
Javascript Code
[CODE]function form_validation()
{
var fnameOK = false;
lnameOk = false;
var first_name_field = document.getElementById(“fname”);
if(fnameOK)
return true;
else
return false;
}//end form_validation fn
function checkFirstName(elementReference)
{
if(elementReference.value == “”)
{
alert(“First Name field is blank”);
return false;
}
else if(elementReference.value.length < 2 || elementReference.value.length > 15)
{
alert(“First Name field should between 2 to 15 characters”);
return false;
}
else
return true;
}
PHP code
[code=php]
<?php
extract($_POST); //extract data from $_POST array
$fname = stripslashes($fname);
$lname = stripslashes($lname);
//mail the form information
print ($fname.”<br />”);
print ($lname.”<br />”);
?>
webdev077
6-12-07