更新:
代码按原样编写,并将保持这种状态。但是,现在它不会将用户在表单中写入的数据提交到数据库。怎么了。
这是第一个文件。它包含 XML HTTP REQUEST 和 html 表单。该文件还包含野生 .
<!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>
这是处理将数据插入服务器的第二个文件,以及单击提交按钮后我想要返回的内容。
<?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);
}
}}
?>