4

I have a basic signup/ login page that submits the data to the SQL database with php. However, I would like the page not to redirect on submission with help of jQuery AJAX (either successful or not).

This is what I currently have and it is not working. It doesn't show any error messages.

HTML - signup.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Signup</title>
    <meta charset="utf-8">
</head>
<body>
    <form>
        <table>
            <tbody>
                <tr>
                    <td>
                        <input type="text" name="first" placeholder="First Name" id="first">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" name="last" placeholder="Last Name" id="last">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Signup" id="signup">
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

JavaScript - signup.js

function submit() {
    $("form").submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'signup.php',
            data: $('form').serialize(),
            success: function() {
                console.log("Signup was successful");
            },
            error: function() {
                console.log("Signup was unsuccessful");
            }
        });
    });
}

$(document).ready(function() {
    submit();
});

PHP - signup.php

<?php
  include_once "db_connect.php";

  $post_FirstName = $_POST['first'];
  $post_LastName = $_POST['last'];

  $addData = "INSERT INTO details (firstname, lastname) VALUES ('$post_FirstName', '$post_LastName')";

  if ($conn->query($addData) === TRUE) {
    echo "Working";
  } else {
    echo "Not working";
  }
?>

Here is the JSFiddle.

I hope you guys can help. Thanks in advance :)

david
  • 3,225
  • 9
  • 30
  • 43
Ryan Hawdon
  • 389
  • 2
  • 6
  • 15
  • [demo](https://jsfiddle.net/guradio/fwgdt2pt/1/) you can have something like demo.change your button type submit to button and create an event handler – guradio May 04 '16 at 05:40

3 Answers3

3

You have some brakes and parentheses not properly closed

function submit() {
    $("form").submit(function(e) {
        e.preventDefault();
        $.ajax({
      type: 'POST',
      url: 'signup.php',
      data: $('form').serialize(),
      success: function() {
        console.log("Signup was successful");
      },//here
      error: function() {
        console.log("Signup was unsuccessful");
      }
    });});//here
}

$(document).ready(function() {
    submit();
});
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
3

If you are using ajax no need to use input type as submit use button.

$(document).ready(function() {
$("#signup").click(function(e) {
    e.preventDefault();
    $.ajax({
  type: 'POST',
  url: 'signup.php',
  data: $('form').serialize()
  success: function() {
    console.log("Signup was successful");
  }
  error: function() {
    console.log("Signup was unsuccessful");
  }
});
});

Also change here

$post_FirstName = $_POST['first']; // name is `first` not `firstname`
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
2

No need to call submit function. Just this will do, (you missed comma and closing tag):

<script>
  $("form").submit(function(e) {
    e.preventDefault();
    $.ajax({
      type: 'POST',
      url: 'signup.php',
      data: $('form').serialize(),
      success: function() {
        console.log("Signup was successful");
      }, //You missed this
      error: function() {
        console.log("Signup was unsuccessful");
      }
    });
  }); //You missed this
</script>
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • document ready is needed if you are loading jQuery script after this function and moreover this fires event after button click. – Thamilhan May 04 '16 at 05:51