I'm trying to submit data to mysql from an html form, however before i do this i want to use some javascript to validate the user input. What's currently happening is the js validation works fine once the user clicks submit, and the php file is called, however when submitting the data to the DB the fields are all empty. Here's what i currently have:
HTML Form
<form action="StudentSubmit.php" action="validating()" method="post" class="fancy-form">
.
.
<div>
<div class="fieldgroup">
<em>*</em><input type="text" name="addressline1" id="addressline1" class="required" minlength="7">
<label for="addressline1">Street #1</label>
</div>
</div>
.
.
.
<div class="submit-wrap">
<button type="submit" class="submit">Submit</button>
</div>
JS
checkdata =
{
checkNull: function(textboxval) {
if ($(textboxval).val() == null || $(textboxval).val() == "") {
return false;
}
else {
return true;
}
},
checkNumeric: function(textboxval) {
var value = $(textboxval).val();
if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
return true;
} else {
return false;
}
},
checkEmail: function(textboxval) {
var x = $(textboxval).val();
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
{
return false;
}
},
isChecked: function(textboxval) {
if ($(textboxval).is(':checked') == false)
{
return false;
} else {
return true;
}
}
};
function validating() {
var returnValue = true;
var boxFields = new Array();
boxFields[0] = "#firstname";
boxFields[1] = "#lastname";
boxFields[2] = "#addressline1";
boxFields[3] = "#homephone";
boxFields[4] = "#cellphone";
boxFields[5] = "#city";
boxFields[6] = "#state";
boxFields[7] = "#guardian";
boxFields[8] = "#zip";
boxFields[9] = "#emergencycontactperson";
boxFields[10] = "#emergencycontactphone";
var nullValue = true;
for (var i = 0; i < boxFields.length; i++) {
if (checkdata.checkNull(boxFields[i]) == false) {
nullValue = false;
break;
}
}
var numFields = new Array();
numFields[0] = "#homephone";
numFields[1] = "#cellphone";
numFields[2] = "#emergencycontactphone";
var numericValue = true;
for (var j = 0; j < numFields.length; j++) {
if (checkdata.checkNumeric(numFields[j]) == false) {
numericValue = false;
break;
}
}
var checkFields = new Array();
checkFields[0] = "#signature";
var checkValue = true;
for (var k = 0; k < checkFields.length; k++) {
if (checkdata.isChecked(checkFields[k]) == false) {
checkValue = false;
break;
}
}
if (nullValue == false || numericValue == false || checkValue == false) {
returnValue = false;
alert("Some required fields were left empty");
return false;
} else {
return true;
}
}
PHP
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$dob = $_POST['dob'];
$emergencycontact = $_POST['emergencycontactperson'];
$address = $_POST['addressline1'] . " " . $_POST['addressline2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$homephone = $_POST['homephone'];
$cellphone = $_POST['cellphone'];
$guardian = $_POST['guardian'];
$inneighborhood = 0;
if ($zip == "49503")
$inneighborhood = 1;
$con = mysqli_connect("localhost", "cookarts_root", "password_here", "cookarts_database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO student (FirstName, LastName, DOB, EmergencyContact, Address, City, State, ZIP, CellPhone, HomePhone, Guardian, InNeighborhood)
VALUES
('$firstname', '$lastname', '$dob', '$emergencycontact', '$address', '$city', '$state', '$zip', '$cellphone', '$homephone', '$guardian', '$inneighborhood')";
if ($con->query($sql) === TRUE) {
header( 'Location: http://cookartscenter.net/ThankYou.php' ) ;
}
else {
echo 'Error: '. $con->error;
}
mysqli_close($con);
?>
Just to clearify, there is no problem connecting to the DB, my problem is after the JS validates the php doesn't correctly pull the data from the form. If you need more of my html code i'd be happy to provide it. Thanks.