skip to Main Content

I submit a php form using jquery ajax, but when receive a parameter value of callback function success, the parameter value contains enter key in front of the value returned from the server.

The work around, I use includes method.

Does anyone know why the enter key inserted in front of the value returned from the server?

 $.ajax({
    type: "POST",
    url: "_action.php",
    data: dataString,
    success: function(result) {
      n = result.includes("success");
      if (n) {
          grecaptcha.reset();
          $("#spinner-contact").remove();
          $('#success_msg').html("<img class='img-fluid aligncenter mb-3' id='checkmark' src='media/img/form_submitted.jpg' />");  
      }
      else {
          grecaptcha.reset();
          $("#spinner-contact").remove();
          $('#success_msg').html("<div class='mt-3'><strong><h1 class='alert alert-warning'>Contact failed!</strong> Please submit again.</h1></div>");   
      }
    }
  });



$statement = runQPs("INSERT INTO my_guestbook(title, url, path, pos, content, isactive, date) VALUES(
            ?, ?, ?, ?, ?, ?, ?
        )",[$name, $email_from, $address, $telephone, $comments, 0, $now]);

        // Insert contact success or fail 
        if($statement->affected_rows === 0) {
            $result = "fail";
        }
        else {
            $result = "success";
        }

        echo $result;

enter image description here

3

Answers


  1. After echo the result variable you can die the statement something like that.

    echo $result;die;
    

    or

    die($result);
    
    Login or Signup to reply.
  2. i think, u can change your "echo" in php file to return "json"

    and in your ajax code:

    n = result.includes("success");
    

    to

    var response = result.message(name of return json);
    
    Login or Signup to reply.
  3. In PHP code you can return value like that:

    return json_encode(result);
    

    In JS file:

    success: function(result) {
      // this will be your response from PHP code
      // add you'r IF statement here
      console.log(result);     
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search