Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method="POST" id="pdo-add">
- <input name="first" id="pdo-add-first" placeholder="First Name">
- <input name="last" id="pdo-add-last" placeholder="Last Name">
- <input name="product" id="pdo-add-product" placeholder="Product">
- <input name="add" type="submit" value="Add">
- </form>
- $(function() {
- $("#pdo-add").on("submit", function (event) {
- //event.preventDefault();
- var add_first = $("#pdo-add-first").val();
- var add_last = $("#pdo-add-last").val();
- var add_product = $("#pdo-add-product").val();
- $.ajax({
- url: "pdoAddSend.php",
- type: "POST",
- data: {
- "add_first": add_first,
- "add_last": add_last,
- "add_product": add_product
- },
- success: function (data) {
- // console.log(data); // data object will return the response when status code is 200
- if (data == "Error!") {
- alert("Unable to insert product record!");
- alert(data);
- } else {
- //$("#newsletter-form")[0].reset();
- $('.announcement_success').html('Product Successfully Added!');
- }
- },
- error: function (xhr, textStatus, errorThrown) {
- alert(textStatus + " | " + errorThrown);
- //console.log("error"); //otherwise error if status code is other than 200.
- }
- });
- });
- });
- ini_set('display_errors', 1);
- error_reporting(E_ALL);
- $add_first = $_POST['add_first'];
- $add_last = $_POST['add_last'];
- $add_product = $_POST['add_product'];
- try {
- $host = 'localhost';
- $name = '';
- $user = '';
- $password = '';
- $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
- }catch(PDOException $e) {
- echo $e->getMessage();
- }
- if(isset($_POST['add'])) {
- $stmt = $dbc->prepare("INSERT INTO users (first, last, product) VALUES (:first,:last,:product)");
- $stmt->bindParam(':first', $add_first);
- $stmt->bindParam(':last', $add_last);
- $stmt->bindParam(':product', $add_product);
- $stmt->execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement