0

更新:

代码按原样编写,并将保持这种状态。但是,现在它不会将用户在表单中写入的数据提交到数据库。怎么了。

这是第一个文件。它包含 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);
         }
}}
?>
4

1 回答 1

2

表单上的按钮是type="submit"; 这将提交表单并重定向页面,除非您return false;onclick属性中有它。相反,我建议将按钮更改为常规按钮而不是提交按钮:

<input type="button" name="submit" value="Register" onclick="load('register.php', 'result');" />

此更改将阻止您的页面在提交表单后重定向。

编辑:查看完整代码后,POST 页面看起来在处理中存在逻辑错误(以及一些语法错误;请参阅我对关闭花括号问题的评论)。

if (empty($errors) === true) {块以} else {您输出的用户已成功注册的 else 块内的 and 结尾。翻译过来,这意味着“如果有初始错误,告诉用户他们注册成功”。相反,将之后(包括)的所有内容更改} else {为:

if (!empty($_POST) && empty($errors)) {
    // the form has been submitted and there are no errors
    echo 'You have successfully registered. Please check your email to activate your account.';
    $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);
} else if (!empty($errors)) {
    // there are errors =[
    echo output_errors($errors);
}
于 2012-07-31T04:55:32.047 回答