0

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.

5
  • And you're certain the form is being submit to the script, right? Commented Mar 26, 2013 at 23:32
  • use onSubmit instead of action for the js-code in the <form>-tag ? Commented Mar 26, 2013 at 23:33
  • just changed the js validation to onSubmit, but same action. I'm not sure i understand what you're asking Evan, im sorry Commented Mar 26, 2013 at 23:36
  • If you include print_r($_POST); on the first row of your PHP-file. What do you get? Commented Mar 26, 2013 at 23:43
  • wow this is awesome, didn't know you could do this! Found my problem! Feel free to make a separate answer and i'll mark yours as answered and i'll comment my findings. Commented Mar 26, 2013 at 23:51

2 Answers 2

2

Change

<form action="StudentSubmit.php" action="validating()" method="post" class="fancy-form">

to

<form action="StudentSubmit.php" method="post" class="fancy-form">

And add the following:

$('form').submit(function ()
{

  if (!validating())
  {
    return false;
  }

}
)
Sign up to request clarification or add additional context in comments.

Comments

0

You're replacing your form action in the opening form tag with your javascript. If you set two attributes the last set attribute will be the one used. You can use onclick() on the button or onsubmit().

<form action="StudentSubmit.php" onsubmit="validating()" method="post" class="fancy-form">

Javascript validation only works on people using browsers. It will not work for spambots or people who have it turned off. Your best bet is to use PHP validation on the server side, then clean your inputs prior to injecting them into your database.

Also check the vars to make sure they're not empty:

if(!empty(strip_tags($_POST['firstname']))){
 $firstname = strip_tags($_POST['firstname']);
}

$firstname = $mysqli->real_escape_string($firstname);

You could also quote for a PDO connection

$conn->quote($firstname)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.