从动态加载的页面获取PHP POST

时间:2013-01-07 16:22:41

标签: php ajax forms post

我有一个使用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上的“加入”按钮,然后点击第二个链接,查看我的完整表格。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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;

}