0

我有一个使用 ajax 动态更新的页面,我有一个动态加载的表单,当单击提交按钮时,它会动态加载另一个页面。执行此操作时如何访问我的 POST 变量?我试过$_POST['variable']没有运气。

ajaxloader.js

注册-form.php

<?php
session_start();
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
    echo '<ul class="err">';
    foreach($_SESSION['ERRMSG_ARR'] as $msg) {
        echo '<li>',$msg,'</li>'; 
    }
    echo '</ul>';
    unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="loginForm" name="loginForm" method="post" action="javascript:ajaxpage('account/register-exec.php', 'content');">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
  <th>First Name </th>
  <td><input name="firstName" type="text" class="textfield" id="firstName" /></td>
</tr>
<tr>
  <th>Last Name </th>
  <td><input name="lastName" type="text" class="textfield" id="lastName" /></td>
</tr>
<tr>
  <th>Username</th>
  <td><input name="username" type="text" class="textfield" id="username" /></td>
</tr>
<tr>
  <th>Password</th>
  <td><input name="password" type="password" class="textfield" id="password" /></td>
</tr>
<tr>
  <th>Confirm Password </th>
  <td><input name="cpassword" type="password" class="textfield" id="cpassword" /></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><input type="submit" name="Submit" value="Register" /></td>
</tr>
</table>
</form>

注册-exec.php

<?php
//Start session
session_start();

//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Sanitize the POST values
$firstName = clean($_POST['firstName']);
$lastName = clean($_POST['lastName']);
$username = clean($_POST['username']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);

//Input Validations
if($firstName == '') {
    $errmsg_arr[] = 'First name missing';
    $errflag = true;
}
if($lastName == '') {
    $errmsg_arr[] = 'Last name missing';
    $errflag = true;
}
if($username == '') {
    $errmsg_arr[] = 'Username missing';
    $errflag = true;
}
if($password == '') {
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}
if($cpassword == '') {
    $errmsg_arr[] = 'Confirm password missing';
    $errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
    $errmsg_arr[] = 'Passwords do not match';
    $errflag = true;
}

//Check for duplicate username
if($username != '') {
    $qry = "SELECT * FROM member WHERE username='$username'";
    $result = mysql_query($qry);
    if($result) {
        if(mysql_num_rows($result) > 0) {
            $errmsg_arr[] = 'Username already in use';
            $errflag = true;
        }
        @mysql_free_result($result);
    }
    else {
        die("Query failed");
    }
}

//If there are input validations, redirect back to the registration form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: register-form.php");
    exit();
}

//Create INSERT query
$qry = "INSERT INTO member(firstName, lastName, username, password) VALUES('$firstName','$lastName','$username','".md5($_POST['password'])."')";
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    header("location: register-success.php");
    exit();
}else {
    die("Query failed");
}
?>

您可以通过点击 tri-peoria.org 上的“加入”按钮并点击第二个链接来查看我的完整表格。

4

2 回答 2

0

您的 Javascript 正在发送GET请求,根据您的声明,您正在检查 $_POST

page_request.open('GET', url, true)

所以更新那些匹配然后再试一次

于 2013-01-07T16:27:29.410 回答
0

您的 javascript 没有发送任何数据并且使用的是GET请求,而不是POST. 您需要将表单中的数据提取到要发送的变量中。将下面的参数名称和 elementID 替换为您的表单元素 ID。

formData = buildData();

page_request.open('POST', url, true);
page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
page_request.send(formData);

function buildData() {
  //build a variable to store the form data, use encodeURI to encode any chars that require encoding
  var postFormVars = "addressLine1=" + encodeURI( document.getElementById("addressLine1").value ) +
  "&addressLine2=" + encodeURI( document.getElementById("addressLine2").value ) +
  "&addressLine3=" + encodeURI( document.getElementById("addressLine3").value ) +
  "&town=" + encodeURI( document.getElementById("town").value ) +
  "&postcode=" + encodeURI( document.getElementById("postcode").value );

  return postFormVars;

}
于 2013-01-07T16:37:20.993 回答