View Full Version : Problems with JavaScript and PHP posting to database

07-12-2011, 04:20 PM
Hello All,

Forgive me if my terminology is off, as I am a Java and PHP newbie, but i will do my best to explain my problems.

I am building a simple site for my job and the HTML and CSS is done. I used a simple PHP script to post form data to an SQL table. I wanted to first create a client side form validator to ensure proper data was being entered into the form (First name, last name, phone, email, state, zip code). Data was being posted to the form no problem prior to the Javascript validation. Now that the Javascript validation is in place, no data is passed to the database and only empty records appear in the table when the form is submitted.

How do I get Javascript and PHP to play nicely together?

I still need to create a server side validation and also a sanitation script to make sure no one tries to inject malicious code, but my main focus now is making sure data can get posted to the database. Please help me out, and correct any incorrect terminology I'm using.

This is my form:

<form name="form1" id="form1" "method="post" action="insert.php">

<table width="80%" border="0">
<td align="right">First Name :&nbsp;</td>
<td><input type="text" name="fname" style="width:170px;"></td>
<td align="right">Last Name :&nbsp;</td>
<td><input type="text" name="lname" style="width:170px;"></td>
<td align="right">Phone :&nbsp;</td>
<td class=><input type="text" name="phone" style="width:100px;">
<td align="right">Email :&nbsp;</td>
<td> <input type="text" name="email" style="width:170px;"></td>
<tr> <td align="right">State :&nbsp; </td> <td><select name="state" style="width:150px;"> <option value="000" selected="selected">Select</option> <option value="Alabama">Alabama</option> <option value="Alaska" >Alaska</option> <option value="Arizona" >Arizona</option> <option value="Arkansas" >Arkansas</option> <option value="California" >California</option> <option value="Colorado" >Colorado</option> <option value="Connecticut" >Connecticut</option> <option value="Delaware" >Delaware</option> <option value="District-of-Columbia" >District of Columbia</option> <option value="Florida">Florida</option> <option value="Georgia" >Georgia</option> <option value="Hawaii" >Hawaii</option> <option value="Idaho" >Idaho</option> <option value="Illinois" >Illinois</option> <option value="Indiana" >Indiana</option> <option value="Iowa" >Iowa</option> <option value="Kansas" >Kansas</option> <option value="Kentucky" >Kentucky</option> <option value="Louisiana" >Louisiana</option> <option value="Maine" >Maine</option> <option value="Maryland" >Maryland</option> <option value="Massachusetts" >Massachusetts</option> <option value="Michigan" >Michigan</option> <option value="Minnesota" >Minnesota</option> <option value="Mississippi" >Mississippi</option> <option value="Missouri" >Missouri</option> <option value="Montana" >Montana</option> <option value="Nebraska" >Nebraska</option> <option value="Nevada" >Nevada</option> <option value="New-Hampshire" >New Hampshire</option> <option value="New-Jersey" >New Jersey</option> <option value="New-Mexico" >New Mexico</option> <option value="New-York" >New York</option> <option value="North-Carolina" >North Carolina</option> <option value="North-Dakota" >North Dakota</option> <option value="Ohio" >Ohio</option> <option value="Oklahoma" >Oklahoma</option> <option value="Oregon" >Oregon</option> <option value="Pennsylvania" >Pennsylvania</option> <option value="Rhode-Island" >Rhode Island</option> <option value="South-Carolina" >South Carolina</option> <option value="South-Dakota" >South Dakota</option> <option value="Tennessee" >Tennessee</option> <option value="Texas" >Texas</option> <option value="Utah" >Utah</option> <option value="Vermont" >Vermont</option> <option value="Virginia" >Virginia</option> <option value="Washington" >Washington</option> <option value="West-Virginia" >West Virginia</option> <option value="Wisconsin" >Wisconsin</option> <option value="Wyoming" >Wyoming</option> </select> </td> </tr><tr>
<td align="right">Zip Code:&nbsp;</td>
<td><input type="text" name="zcode" style="width:50px;"></td>
<input type="hidden" name="submitted" value="1">
<td><input type="submit" value="Submit" name="submit"></td>

This is my javascript code that links to an external .js file:

<script type="text/javascript">

var frmvalidator = new Validator("form1");
frmvalidator.addValidation("fname","req","Please enter your First Name");
frmvalidator.addValidation("fname","minlen=2","Minimum length for First Name is 2 characters");
frmvalidator.addValidation("fname","maxlen=20","Max length for First Name is 20 characters");
frmvalidator.addValidation("lname","req","Please enter your Last Name");
frmvalidator.addValidation("lname","minlen=2","Minimum length for Last Name is 2 characters");
frmvalidator.addValidation("lname","maxlen=20","Max length for Last Name is 20 characters");
frmvalidator.addValidation("email","req","Please enter a Valid Email Address");
frmvalidator.addValidation("phone","req","Please enter your 10-digit Phone Number. Example: 5051234567");
frmvalidator.addValidation("phone","minlen=10","Please enter your 10-digit Phone Number. Example: 5051234567");
frmvalidator.addValidation("phone","maxlen=10","Please enter your 10-digit Phone Number. Example: 5051234567");
frmvalidator.addValidation("state","dontselect=000","Please select your state");
frmvalidator.addValidation("zcode","req","Please enter your 5-digit Zip Code");
frmvalidator.addValidation("zcode","minlen=5","Please enter your 5-digit Zip Code");
frmvalidator.addValidation("zcode","maxlen=5","Please enter your 5-digit Zip Code");

This is my insert.php file that form uses:

$con = mysql_connect("localhost","**********","**********");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("sms2011_leads", $con);

$sql="INSERT INTO sms2011_prospects (fname, lname, phone, email, state, zcode)

if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "<script>window.location = 'http://www.nationalcollegesearch.org/thankyou.html';</script>";


07-12-2011, 05:29 PM
It's ok. I found my error and fixed it. a form id tag was screwing things up for me. Everything is fixed!