Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    43
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Problems with JavaScript and PHP posting to database

    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:

    Code:
    <form name="form1" id="form1" "method="post" action="insert.php">
    
    <table width="80%" border="0">
    <tr>
    <td align="right">First Name :&nbsp;</td>
    <td><input type="text" name="fname" style="width:170px;"></td>
    </tr>
    <tr>
    <td align="right">Last Name :&nbsp;</td>
    <td><input type="text" name="lname" style="width:170px;"></td>
    </tr>
    <tr>
    <td align="right">Phone :&nbsp;</td>
    <td class=><input type="text" name="phone" style="width:100px;">
    </td>
    </tr>
    <tr>
    <td align="right">Email :&nbsp;</td>
    <td> <input type="text" name="email" style="width:170px;"></td>
    </tr>
    <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>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <input type="hidden" name="submitted" value="1">
    <td><input type="submit" value="Submit" name="submit"></td>
    </tr>
    </table>
    </form>

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

    Code:
    <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("email","maxlen=50");
     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("phone","numeric");
     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");
    </script>
    This is my insert.php file that form uses:
    PHP Code:
    <?php
    $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)
    VALUES
    ('$_POST[fname]','$_POST[lname]','$_POST[phone]','$_POST[email]','$_POST[state]','$_POST[zcode]')"
    ;

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

    mysql_close($con)
    ?>
    Last edited by dareichmann; 07-12-2011 at 10:02 PM. Reason: quick changes and added form html

  • #2
    New Coder
    Join Date
    Jul 2011
    Posts
    43
    Thanks
    11
    Thanked 0 Times in 0 Posts
    It's ok. I found my error and fixed it. a form id tag was screwing things up for me. Everything is fixed!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •