0

我有一个模式对话框中的表单。当我提交表单时,我没有收到来自 php.ini 的响应。我知道表单和脚本正在运行,因为我可以在对话框之外运行它们并且一切正常。

这是我的表单html代码:

 <div id="add_user">
        <form action="resetProcess.php" method="post">
          <input type="hidden" name="action" value="Reset Password" />
          <table width="385" border="0" cellspacing="0" cellpadding="3">
            <tr>
              <td colspan="3">
                </td>
              </tr>
            <tr>
              <td width="191" align="right"><label for="firstname2">First name *</label></td>
              <td width="194" colspan="2"><input type="text" name="firstname" id="firstname2" value="" /></td>
              </tr>
            <tr>
              <td align="right"><label for="lastname2">Last name *</label></td>
              <td colspan="2"><input type="text" name="lastname" id="lastname" value="" /></td>
              </tr>
            <tr>
              <td align="right"><label for="email2">Email address *</label></td>
              <td colspan="2"><input type="text" name="email" id="email" value="" /></td>
              </tr>
            <tr>
              <td colspan="3" style="padding-top:20px;">
              <input  type="submit" name="action1" id="requestButton" value="Get Email" /></tr>
            </table>
        </form>

</div>

这是php进程文件。请记住,这可以正常工作,因为它是在它自己的浏览器窗口中提交的。

<?php 
    // Required files
    include("includes/common.inc.php");
    require_once( "users.class.php" );

    session_start();

// check if the reset password form has been submitted.
if ( isset( $_POST["action1"] ) and $_POST["action"] == "Reset Password" ) {    

      $user = new User( array(
        "firstname" => isset( $_POST["firstname"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["firstname"] ) : "",                  
        "lastname" => isset( $_POST["lastname"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["lastname"] ) : "",
        "email" => isset( $_POST["email"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]@/", "", $_POST["email"] ) : "",

      ) );

$existingUser = User::getByEmailAddress( $user->getValue( "email"));

  if ($existingUser) {
  var_dump($existingUser);
  echo "Success!!  Your Request has been sent!";
  } else {
  echo "That email address does not match anyone in our system.  Please go back and re-enter your information.";
  }
}

?>

这是头文件中包含的js代码:

         <script>
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
    $( "#dialog" ).dialog({
        autoOpen: false,
        show: "fade",
        hide: "fade",
        width: "400px",
    });

    $( "#reset-form" ).click(function() {
        $( "#dialog" ).dialog( "open" );
        return false;

    });

    // Hide Form error labels
    $('.error').hide();

    $('#requestButton').click(function(e){
        e.preventDefault();
        var firstname = $('#firstname').val();
        if (firstname == "") {
            $('label#firstname_error').show();
            $('label#firstname').focus();
            return false;
        }
        var lastname = $('#lastname').val();
        if (lastname == "") {
            $('label#lastname_error').show();
            $('label#lastname').focus();
            return false;
        }
        var email = $('#email').val();
        if (email == "") {
            $('label#email_error').show();
            $('label#email').focus();
            return false;
        }
        var dataString = 'firstname=' + firstname + '&lastname=' + lastname + '&email=' + email;

        // ajax call
        $.ajax({
            type: "POST",
            url: "resetProcess.php",
            data: dataString,
            success: function(result){
                //$("#passRequest").fadeOut(500, function(){
                    console.log("Result: " + result);
                //});
            },
        });    
        return false;
    });

    });
    </script>

最后,我将包含类文件中的查询方法:

              public static function getByEmailAddress( $email ) {
            $conn = parent::connect();
            $sql = "SELECT * FROM " . TBL_USERS . " WHERE email = :email";

            try {
              $st = $conn->prepare( $sql );
              $st->bindValue( ":email", $email, PDO::PARAM_STR );
              $st->execute();
              $row = $st->fetch();
              parent::disconnect( $conn );
              if ( $row ) return new User( $row );
            } catch ( PDOException $e ) {
              parent::disconnect( $conn );
              die( "Query failed: " . $e->getMessage() );
            }
          }

谢谢您的帮助!!

4

4 回答 4

0

问题是 php 脚本正在检查以确保提交按钮已通过,并且还设置了其他隐藏字段。

// check if the reset password form has been submitted.

if ( isset( $_POST["action1"] ) 和 $_POST["action"] == "重置密码" )

这不是与 AJAX Post 请求一起发送的。

于 2012-11-05T16:25:52.123 回答
0

为什么不发送这样的数据呢?

$.ajax({
    type: "POST",
    url: "resetProcess.php",
    data: {
        'firstname': firstname,
        'lastname': lastname,
        'email': email
    },
    success: function(result) {
        //$("#passRequest").fadeOut(500, function(){
        console.log("Result: " + result);
        //});
    },
});​
于 2012-11-02T15:06:17.810 回答
0

我认为你实际上是在做一个 get 请求,但指定 post。

在您的 ajax 调用中传递这个而不是字符串:

var data = {
    'firstname': firstname,
    'lastname': lastname,
    'email': email
}

或者您更改您的方法以发布并保留 get-query-string 原样。

于 2012-11-02T15:06:22.417 回答
0

我会检查所需的文件是否正确包含/需要,

// Required files
include("includes/common.inc.php");
require_once( "users.class.php" );

因为也许您访问 resetProcess.php 文件的方式与没有 ajax 的方式不同。您可以通过检查 User 类是否实际存在来检查它。

于 2012-11-02T16:01:03.013 回答