Hi,
I have a form that is validated with javavscript, once validation is passed the details are written to a MySql db and an email sent to the user. However, only the first form field is ever validated and the script then prints my "Thank you" message.
Do I need to initialise the $submit value ?
Have I got something in the wrong place ?
I really hope somebody has the patience to help me as it's driving me nuts.
The code is as follows (I have abbreviated some of the form as it is quite large).
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="Javascript"><!--
function check(frm){
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
if (!frm.first_name.value){
alert("Please enter your First name");
frm.first_name.focus();
frm.first_name.select();
return false;
}
if (!frm.last_name.value){
alert("Please enter your Last name");
frm.last_name.focus();
frm.last_name.select();
return false;
}
if (!frm.address1.value){
alert("Please enter the first line of your address");
frm.address1.focus();
frm.address1.select();
return false;
}
if (!frm.city.value){
alert("Please enter your City/Town");
frm.city.focus();
frm.city.select();
return false;
}
if (!frm.postcode.value){
alert("Please enter your postcode");
frm.postcode.focus();
frm.postcode.select();
return false;
}
if (!frm.search_post.value){
alert("Please enter the main part of a post code");
frm.search_post.focus();
frm.search_post.select();
return false;
}
if (!frm.tel1.value){
alert("Please enter a Telephone No.");
frm.tel1.focus();
frm.tel1.select();
return false;
}
if (frm.email.value.length > 1) {
if (frm.email.value.indexOf('@') == -1){
alert('Please enter your Full E-mail address.');
frm.email.focus();
frm.email.select();
return false;
}
if (frm.email.value.indexOf('.') == -1){
alert('Please enter your Full E-mail address.');
frm.email.focus();
frm.email.select();
return false;
}
}
if (!frm.school.value){
alert("Please enter your School Name");
frm.school.focus();
frm.school.select();
return false;
}
if (!frm.login.value){
alert("Please enter a Login Id");
frm.login.focus();
frm.login.select();
return false;
}
// check for minimum lengths
if (frm.login.value.length < minLength) {
alert('Your Login Id must be ' + minLength + ' characters long. Please try again.');
frm.login.focus();
frm.login.select();
return false;
}
// check for spaces
if (frm.login.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed within your Login Id..");
frm.login.focus();
frm.login.select();
return false;
}
if (!frm.pass1.value){
alert("Please create a password");
frm.pass1.focus();
frm.pass1.select();
return false;
}
// check for minimum lengths
if (frm.pass1.value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Please try again.');
frm.pass1.focus();
frm.pass1.select();
return false;
}
// check for spaces
if (frm.pass1.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed within the password.");
frm.pass1.focus();
frm.pass1.select();
return false;
}
if (!frm.pass2.value){
alert("Please verfiy your password");
frm.pass2.focus();
frm.pass2.select();
return false;
}
if (frm.pass1.value > frm.pass2.value){
alert("The passwords don't match, please try again.");
frm.password.focus();
frm.password.select();
return false;
}
if (frm.pass1.value < frm.pass2.value){
alert("The passwords don't match, please try again.");
frm.password.focus();
frm.password.select();
return false;
}
return true;
}
//--></SCRIPT>
<body bgcolor="#FFFFFF">
<!----- START PHP CODE -------------->
<?php
if ($submit) {
// update.................................. --->
// field checking...remove spaces and set title case
$first_name = ucwords(strtolower($firstname));
$last_name = ucwords(strtolower($lastname));
$address1 = ucwords(strtolower($address1));
$address2 = ucwords(strtolower($address2));
$city = strtoupper($county);
$county = strtoupper($county);
$postcode = ucwords($postcode);
$paid = 'N';
$enhanced = 'N';
$spaces1 = ' ';
// get last id
//------------------------------------------------------------------------------------------
$db = mysql_connect("localhost", "xxxxxx", "xxxxxx");
mysql_select_db("motoring1",$db);
$result = mysql_query("select MAX(Id) as maxnum FROM members",$db);
$row = mysql_fetch_array($result);
$str2 = $row["maxnum"] ;
$str2 = $str2 + 1 ;
// write record
$sql = "INSERT INTO members (first_name,last_name,school,tel1,tel2,search_post,postcode,city,county,add1,add2,reg_date,paid,enhanced,login,pass,email,web) VALUES ('$first_name,'$last_name','$school','$tel1','$tel2','$search_post','$postcode','$city','$county','$add1','$add2','$reg_date','$paid','$enhanced','$login','$pass1','$email','$web')";
$result = mysql_query($sql);
//--- Send email if one was entered ----------->
$spaces = ' ';
if ($email > $spaces) {
$to = $email ;
$from = "me@me.com";
$subject = "Messages";
$message = "Hello from me".
"\n".
"\n Your Registration details :-".
"\n".
"\n Login Id: ". $spaces . $login.
"\n Password: ". $spaces . $pass1.
"\n".
"\n You submitted the following details :-".
"\n".
"\n School: ". $spaces . $school.
"\n".
"\n Personal Details".
"\n First Name: ". $spaces . $first_name.
"\n Last Name: ". $spaces . $last_name
"\n Address: ". $spaces . $address1 . $spaces . "address2 .
"\n Tel.No 1: ". $spaces . $tel1.
"\n Tel.No 2: ". $spaces . $tel2.
"\n E-mail: ". $spaces . $email.
"\n Web Site: ". $spaces . $web.
"\n".
"\n".
"\n We appreciate you taking the time to register. Thanks".
"\n".
"\n Site Adminstrator ".
"\n http://www.me.com ".
mail($to, $subject, $message, "From: me@me.com\nReply-To: me@me.com\nX-Mailer: PHP/" . phpversion());
}
echo "Thank you. <BR><BR>
\n";
} else{
?>
<!----- END PHP CODE --------------------->
<form method="post" action="<?php echo $PHP_SELF?>" onSubmit="return check(this)">
<table width="100%" border="0" cellspacing="0" cellpadding="0" name="reg table 1">
<tr>
<td width="10"> </td>
<td colspan="2" background="gfx/blue_background.png"><b>Personal Details</b>
<font size="1">(please read our Privacy Policy)</font></td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"> </td>
<td> </td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"><font color="#3333FF">First Name</font></td>
<td>
<input type="text" name="first_name" size="20" maxlength="20">
</td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"><font color="#3333FF">Last name</font></td>
<td>
<input type="text" name="last_name2" size="20" maxlength="20">
</td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"><font color="#3333FF">Address line 1</font></td>
<td>
<input type="text" name="address1" size="25" maxlength="25">
</td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100">Adress Line 2</td>
<td>
<input type="text" name="address2" size="25" maxlength="25">
</td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"><font color="#3333FF">Town/City</font></td>
<td>
<input type="text" name="city" size="25" maxlength="25">
</td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width="100"><font color="#3333FF">County</font></td>
<td>
<input type="text" name="county" size="25" maxlength="25">
</td>
<td width="10"> </td>
</tr>
...... table code removed to make this post smaller!......
<tr>
<td width="10"> </td>
<td width=" " colspan="2" background="gfx/blue_background.png"><b>Directory Update</b></td>
<td width="10"> </td>
</tr>
<tr>
<td width="10"> </td>
<td width=" " colspan="2"> Please press the Submit button to add your entry.<br>
</td>
<td width="10"> </td>
</tr>
<table width="100%" border="0" cellspacing="0" cellpadding="0" name="reg table 4">
<tr>
<td width="10">
<div align="left"> </div>
</td>
<td width="200">
<input type="submit" name="submit" value="Register">
<input type="reset" name="Reset" value="Reset">
</td>
<td>
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
Thanks for any help you can offer.
JG
ps. register-globals is set to ON (if this makes a difference?)