UPDATE:
Code is written as is and will stay that way. However, now it doesn’t submit the data the user wrote in the form to the database. What’s wrong.
Here is the first file. It contains the XML HTTP REQUEST and html form. This file also contains the wild .
<!DOCTYPE html>
<html>
<head>
<?php
require_once 'core/init.php';
?>
<meta charset="utf-8" />
<title></title>
<script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js">
//directly below is that wild script tag
</script>
<script type="text/javascript">
function load(thefile, thediv) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject ('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
parameters1 = 'username='+document.getElementById('username').value;
parameters2 = 'email='+document.getElementById('email').value;
parameters3 = 'password='+document.getElementById('password').value;
parameters4 = 'password_again='+document.getElementById('password_again').value;
parameters5 = 'first_name='+document.getElementById('first_name').value;
parameters6 = 'last_name='+document.getElementById('last_name').value;
xmlhttp.open('POST', thefile, true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.send(parameters1 + '&' + parameters2 + '&' + parameters3 + '&' +
parameters4 + '&' + parameters5 + '&' +parameters6);
}
</script>
</script>
<title>Pop Up Sign Up</title>
</head>
<body>
<div id="popupbox">
<form name="signup" action="" method="post">
Desired Username:
<input id="username" placeholder="Username" value="<?php echo
(isset($desired_username) ? strip_tags($desired_username) : '');?>" type="text"
placeholder="Bob123" name="username" size="14" />
<br /> <br />
Your Email:
Register
<input id="email" placeholder="jasontanner328@gmail.com" value="<?php echo
(isset($desired_email) ? strip_tags($desired_email) : '');?>" type="email"
name="email" size="14" />
<br /> <br />
Your Password:
<input id="password" placeholder="Password" name="password" type="password"
size="14" />
<br /> <br />
Your Password Again:
<input id="password_again" placeholder="Password Again"
name="password_again" type="password" size="14" />
<br /> <br />
First Name:
<input id="first_name" placeholder="Jason" value="<?php echo
(isset($desired_first_name) ? strip_tags($desired_first_name) : '');?>"
name="first_name" type="text" size="14" />
<br /> <br />
Last Name:
<input id="last_name" placeholder="Tanner" name="last_name" value="<?php echo
(isset($desired_last_name) ? strip_tags($desired_last_name) : '');?>" type="text"
size="14" />
<br /> <br />
<center><input type="button" name="submit" value="Register"
onclick="load('register.php', 'popupbox');" /></center>
</form>
</div>
<span id="result">
</span>
</body>
</html>
Here is the second file that deals with inserting the data into the server and what I want to return after the submit button has been clicked on.
<?php
require_once 'core/init.php';
logged_in_redirect();
if (empty($_POST) === false) {
$desired_username = $_POST['username'];
$desired_email = $_POST['email'];
$desired_first_name = $_POST['first_name'];
$desired_last_name = $_POST['last_name'];
$required_fields = array
('username','email','password','password_again','first_name','last_name');
foreach ($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'Fields marked with an asterisk are required.';
break 1;
}
}
if (empty($errors) === true) {
if (user_exists($_POST['username']) === true || strlen($_POST ['username']) < 6) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken
or is too short. 6 characters are the minimum.';
}
if (preg_match("/\\s/", $_POST ['username']) == true) {
$errors[] = 'Sorry there is a space in your username.';
}
if (strlen($_POST ['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters';
}
if ($_POST ['password'] !== $_POST['password_again']) {
$errors[] = 'Make sure both passwords submitted are the same.';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address is required.';
}
if (email_exists($_POST['email']) === true) {
$errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already in use.';
}
if (strlen($_POST ['first_name']) < 2) {
$errors[] = 'Your first name must contain at least two characters.';
}
if (strlen($_POST ['last_name']) < 2) {
$errors[] = 'Your last name must contain at least two characters.';
} }
} else {
//if (isset($_GET['success']) && empty($_GET['success'])) {
//echo 'You have successfully registered. Please check your email to activate your
account.';
// } else {
if (empty ($_POST) === false && empty($errors) === true){
$register_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'email_code' => md5($_POST['username'] + microtime())
);
register_user($register_data);
echo 'WIN';
// header('Location: register.php?success');
// exit();
// } else
if (empty($errors) === false){
echo output_errors($errors);
}
}}
?>
The button on your form is
type="submit"; This will submit the form and redirect the page, unless you have itreturn false;in theonclickattribute. I would suggest, rather, to change the button to a regular button instead of a submit-button:This change will stop your page from redirecting after submitting the form.
EDIT: After reviewing the full code, the POST-page looks like it has a logic error in the processing (and a few syntax errors; see my comment on the question regarding closing curly braces).
The
if (empty($errors) === true) {block ends with an} else {and inside the else-block you output that the user has successfully registered. Translated, this means “if there is an initial error, tell the user they successfully registered.” Instead, change everything after (and including) the} else {to: