0

I'm very new to anything outside of PHP, so forgive me if this comes off as simple.

I'm trying to create a simple registration form, which then calls a PHP function, which inserts the data.

The problem is, the data doesn't seem to be going to the page.

My form:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function () {
$('#register').on('submit', function (e) {
    $.ajax({
        type: 'POST',
        url: 'http://warofman.com/actions.php?type=register',
        data: $('#register').serialize(),
        success: function () {
            alert('Form was submitted.');
            console.log(e);
        }
        });
    e.preventDefault();
    });
});
</script>
</head>
<body>
    <form id="register">
        Username: <input type="text" name="login"><br>
        Email: <input type="text" name="email"><br>
        Password: <input type="password" name="password"><br>
        Confirm Password: <input type="password" name="confpass"><br>
        <input type="submit" name="submit" value="Register">
    </form>
</body>
</html>

And then, the PHP:

function register()
{
    $login = $_POST['login'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $confpass = $_POST['confpass'];

    do_reg($login, $email, $password, $confpass);
}

Where do_reg() calls the process of registering.

4
  • Show how you're inserting the information into the database because if that is how you're trying, it's not... correct. Commented Dec 11, 2013 at 0:09
  • What are you passing through serialize? the problem must be there. Try alerting the serialized data or try to log it.. Commented Dec 11, 2013 at 0:12
  • 1
    if you do in php "print_r($_POST);" it's receiving correctly the POST data? If so.. it's do_reg() problems Commented Dec 11, 2013 at 0:12
  • As it turns out, I am completely simple. The problem was in how I was calling my database class, so the code was not actually running. (Way to go PHP for silently failing!) I accepted Rao's answer for the best practice advice. :) Commented Dec 11, 2013 at 1:35

2 Answers 2

1

Make the following changes, (follow good practice)

<form id="register" action="" method="post">
        <label for="login">Username:</label>
        <input id="login" type="text" name="login"><br>

        <label for="email">Email:</label>
        <input type="email" name="email" id="email" /><br>

        <label for="password">Password:</label> 
        <input type="password" name="password" id="password"><br>

        <label for="confpass">Confirm Password:</label>
        <input type="password" name="confpass" id="confpass"><br>

        <input type="submit" name="submit" value="Register">
    </form>

On php side:

You are writing everything inside a function(register) and not calling it, instead do something like this,

if(isset($_POST['submit'])){
  $login = $_POST['login'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $confpass = $_POST['confpass'];
  do_reg($login, $email, $password, $confpass);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can't pass a query string in the URL using POST. Add the "type=register" to your data:

$.ajax({
    type: 'POST',
    url: 'http://warofman.com/actions.php',
    data: "type=register&" + $('#register').serialize(),
    success: function () {
        alert('Form was submitted.');
        console.log(e);
    }
    });
e.preventDefault();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.