Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <form method="POST" id="pdo-add">
  2. <input name="first" id="pdo-add-first" placeholder="First Name">
  3. <input name="last" id="pdo-add-last" placeholder="Last Name">
  4. <input name="product" id="pdo-add-product" placeholder="Product">
  5. <input name="add" type="submit" value="Add">
  6. </form>
  7.  
  8. $(function() {
  9.  
  10. $("#pdo-add").on("submit", function (event) {
  11. //event.preventDefault();
  12.  
  13. var add_first = $("#pdo-add-first").val();
  14. var add_last = $("#pdo-add-last").val();
  15. var add_product = $("#pdo-add-product").val();
  16.  
  17. $.ajax({
  18. url: "pdoAddSend.php",
  19. type: "POST",
  20. data: {
  21. "add_first": add_first,
  22. "add_last": add_last,
  23. "add_product": add_product
  24. },
  25. success: function (data) {
  26. // console.log(data); // data object will return the response when status code is 200
  27. if (data == "Error!") {
  28. alert("Unable to insert product record!");
  29. alert(data);
  30. } else {
  31. //$("#newsletter-form")[0].reset();
  32. $('.announcement_success').html('Product Successfully Added!');
  33. }
  34. },
  35. error: function (xhr, textStatus, errorThrown) {
  36. alert(textStatus + " | " + errorThrown);
  37. //console.log("error"); //otherwise error if status code is other than 200.
  38. }
  39. });
  40. });
  41. });
  42.  
  43. ini_set('display_errors', 1);
  44. error_reporting(E_ALL);
  45.  
  46. $add_first = $_POST['add_first'];
  47. $add_last = $_POST['add_last'];
  48. $add_product = $_POST['add_product'];
  49. try {
  50. $host = 'localhost';
  51. $name = '';
  52. $user = '';
  53. $password = '';
  54.  
  55. $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
  56.  
  57. }catch(PDOException $e) {
  58. echo $e->getMessage();
  59.  
  60. }
  61.  
  62. if(isset($_POST['add'])) {
  63.  
  64. $stmt = $dbc->prepare("INSERT INTO users (first, last, product) VALUES (:first,:last,:product)");
  65.  
  66. $stmt->bindParam(':first', $add_first);
  67. $stmt->bindParam(':last', $add_last);
  68. $stmt->bindParam(':product', $add_product);
  69.  
  70. $stmt->execute();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement