PHP + Javascript Validation

have created a form that sends the contents to a mysql database.
i want the form to be validated using javascript b4 hand, it works without the php to get it to send to the database but when i press submit, it won’t validate here is my code…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script language="JavaScript">
<!-

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("FirstName", "LastName", "EmailAddress","Question1", "Question2", "Question3", "Question4", "Question5");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "EmailAddress", "Question1", "Question2","Question3", "Question4", "Question5");
	// dialog message
	var alertMsg = "Please complete the following fields:\
";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\
";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\
";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
// -->
</script>

<form method="POST" action="<? echo ( $self ); ?>"
<form name="formcheck" onsubmit="return formCheck(this);">
<form action="insert_record.php" method="POST">

First Name: <input type=text name="FirstName" size="25"><br>
Last Name: <input type=text name="LastName" size="25"><br>
E-mail Address: <input type=text name="EmailAddress" size"25" /><br />
Question1: <input type=text name="Question1" size"25" /><br />
Question2: <input type=text name="Question2" size"25" /><br />
Question3: <input type=text name="Question3" size"25" /><br />
Question4: <input type=text name="Question4" size"25" /><br />
Question5: <input type=text name="Question5" size"25" /><br />
Question6: <input type=text name="Question6" size"25" /><br />
Question5: <input type=text name="Additions" size"25" /><br />


<input type=submit value="Submit Form">
</form>

<?php

include("dbinfo.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database ) or die( "Unable to select database");

$self = $_SERVER['PHP_SELF'];

@$FirstName=$_POST['FirstName'];
@$LastName=$_POST['LastName'];
@$Question1=$_POST['Question1'];
@$Question2=$_POST['Question2'];
@$Question3=$_POST['Question3'];
@$Question4=$_POST['Question4'];
@$Question5=$_POST['Question5'];
@$Question6=$_POST['Question6'];
@$Additions=$_POST['Additions'];

if($FirstName){

$sql="INSERT INTO contact ( FirstName, LastName, EmailAddress, Question1, Question2, Question3, Question4, Quesion5, Question6, Additions) VALUES ( '$FirstName','$LastName','$EmailAddress','$Question1','$Question2','$Question3','$Question4','$Question5','$Question6','$Additions')";

$result=mysql_query($sql)or die("Insert Error: ".mysql_error());

if($result){
print "Record added\
";
}

}
?>
</body>
</html>

If the JavaScript is not stopping the form from being submitted to your server then PHP is not the problem, and no point posting the PHP forum.

Your JS has old coding practices, like using form object elements instead of just using elements IDs. Looks like a tutorial from before the turn of the century.

BUT I’d say you’re problem is because you are opening 3 forms in a row (nesting them), and only closing one. You only want ONE form, there is no need for a bunch of overlapping and contradictory opening <form> tags.


<form method="POST" action="<? echo ( $self ); ?>" 
<form name="formcheck" onsubmit="return formCheck(this);"> 
<form action="insert_record.php" method="POST"> 

ok so if i want it to just check the form then post it to the server, how would i do that??

I’d pick a JavaScript library, such as jQuery do a search for ‘jQuery form validation tutorials’.
There’s a bit too much to learn to answer it with one forum post.