I am still new to jQuery, AJAX and PHP.
I was wondering what I can do wrong here. I take information about the client and try to return a confirmation message on the same page.
So, the problems that I have are: 1) Pressing the submit button refreshes my page? Why?
2) I have a submit button. How can I change the text of this with addCustomer.php results?
3) Is it good to have javascript and php code in the same file in the customer.php file?
Edit: I also use Firefox Tamper Data Addon - when I click the "Submit" button, for some reason the data is not sent.
Customer.php
<script type="text/javascript"> $(document).ready(function(){ $('#submit').click(function() { $.ajax({ type : 'POST', url : 'addCustomer.php', dataType : 'json', data:{ add_LN : $('#add_LN').val(), add_FN : $('#add_FN').val(), add_PN : $('#add_PN').val(), add_DOB : $('#add_DOB').val() }, success : function(data){ </script> <p> </p> <p>Add New Customer:</p> <div align="center"> <form> <table width="396" border="1"> <tr> <td width="133"><p>Last Name:</p> <p>First Name:</p> <p>Phone Number:</p> <p>Date of Birth:</p></td> <td width="144"><p> <input type="text" name="add_LN" id="add_LN" /> </p> <p> <input type="text" name="add_FN" id="add_FN" /> </p> <p> <input type="text" name="add_PN" id="add_PN" /> </p> <p> <input type="text" name="add_DOB" id="add_DOB" /> </p> </td> <td width="97"><input type="submit" name="submit" id="submit" value="Add Customer" /></td> <div id="confirmMsg"/> </tr> </table> </form> </div> <p> </p> </div> </div>
addCustomer.php
<?php $username="******"; $password="******"; $database="******"; if (isset ($_POST['add_LN'])) $lastName=$_POST['add_LN']; else die("Last Name not passed in POST"); if (isset ($_POST['add_FN'])) $firstName=$_POST['add_FN']; else die ("First Name not passed in POST"); if (isset ( $_POST['add_PN'])) $phone=$_POST['add_PN']; else die("Phone Number not passed in POST"); if (isset ($_POST['add_DOB'])) $dob=$_POST['add_DOB']; else die("Date of Birth not passed in Post"); //$membership==$_POST['membership']; mysql_connect("dbs4.cpsc.u.ca",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')"; if (mysql_query($query)){ echo "Thanks"; } else { echo "Failed to insert customer into database"; } mysql_close(); ?>
Many thanks for the help!