Reputation: 151
So this question is sort of complicated, so I'm going to be as descriptive as possible.
I recently started implementing AJAX into my website, so I thought I'd improve user experience by making it so if you didn't have an account, you could just click on the link below the login box and it would just load the registration.php file from the views folder.
And it is just giving me a hard time. The registration just goes back to the index page and it doesn't update the database at all. But when I go to the actual registration page that is being loaded, it works. Since I'm loading it from the index.php file that is in the root of the folder, does that mean I must change the nature of the links in the registration page?
Here's my JavaScript:
$(document).ready(function(){
/* Switch to registration */
$('a#registration').click(function(){
$('#login').load('./views/registration.php');
});
});
Here's my registration page code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method='post'>
<label> Username </label><br><input type='text' name='reg_username' size='20'>
<?php
if (isset($_POST['reg_username'])) {
//standard validation
}
?>
<br>
<label> E-Mail </label><br><input type='email' name='email' size='20'>
<?php
if (isset($_POST['email'])) {
//standard validation
}
?>
<br>
<label> Password: </label><br><input type='password' name='reg_password' size='20'>
<?php
if (isset($_POST['reg_password'])) {
//standard validation
}
?>
<br>
<input type='submit' name='submitted' value='Register'>
</form>
<?php
if (isset($_POST['submitted'])) {
if ($valid) {
$username = $_POST['reg_username'];
$email = $_POST['email'];
$password = $_POST['reg_password'];
$register_query = sprintf("INSERT INTO users(username, email, password) values('%s', '%s', '%s');", mysql_real_escape_string($username),
mysql_real_escape_string($email), mysql_real_escape_string(crypt($password, $username)));
require_once '../models/dbconnect.php';
database_connect();
require_once '../models/database_validation.php';
account_duplicate($username, $email);
if ($valid) {
$result = mysql_query($register_query);
if ($result) {
echo "Registration Successful!";
} ELSE {
echo "I think something went wrong...";
}
}
}
}
?>
I think the focus is on the form action and the require functions on the page. Am I using the require function right? What do you think what's wrong?
If you want to play with it hands on, here's the repository for it. It's my first open source project.
https://github.com/Lalien/Test_Site
Upvotes: 0
Views: 45
Reputation: 1745
PHP_SELF sends you to the page that is loaded on the browser, in this case, the index page. Change your form action to the registration page so that it will send the form to the correct page.
Plus, because you are missing echo
in there, your action is a blank string, which will always submit to the current page.
Upvotes: 1