Reputation: 5
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.
Upvotes: 0
Views: 253
Reputation: 1731
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();
});
Upvotes: 0
Reputation: 7152
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);
}
Upvotes: 1