简体   繁体   中英

Jquery ajax no response from php

I have a form that is in a modal dialog box. When I submit the form I am not getting the response from php. I know that the form and script is working because I can run them outside the dialog and everything works.

Here is my form html code:

 <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>

Here is the php process file. Please remember this works fine in it is submitted in it's own browser window.

<?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.";
  }
}

?>

Here is the js code that is included in the header file:

         <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>

and lastly I will include the query method from the class file:

              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() );
            }
          }

Thank you for your help!!

Why not send the data like this as well?

$.ajax({
    type: "POST",
    url: "resetProcess.php",
    data: {
        'firstname': firstname,
        'lastname': lastname,
        'email': email
    },
    success: function(result) {
        //$("#passRequest").fadeOut(500, function(){
        console.log("Result: " + result);
        //});
    },
});​

I think you are actually doing a get request, but specifying post.

In your ajax call pass this instead of the string:

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

Or you change your methods to post and leave the get-query-string as it is.

I would check if the required files are properly inlcuded/required,

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

because maybe you're accessing the resetProcess.php file differently then you would without ajax. You could check it by checking if the User class actually exists.

The problem was that the php script was checking to make sure the submit button was passed and the other hidden field was set also.

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

if ( isset( $_POST["action1"] ) and $_POST["action"] == "Reset Password" )

This was not being sent with the AJAX Post request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM