2

forget.php PHP:

if (! (empty($_POST['emailforget'])) ) {
    echo "here in the function";
} else {
    echo "here";
}

AJAX:

$("#passreset").on('click', function(e) {

  var emailforget = $("#tempemail").val();
  alert(emailforget);
  //ajax call here
  $.ajax({
    type: "post",
    url: 'user/forgetpass.php',
    data: {
      'emailforget': emailforget
    },
    cache: false,
    contentType: false,
    processData: false,
    beforeSend: function() {
      //alert("here in function beforesent");
    },
    success: function(html) {
      //alert(html);
      document.getElementById("error").innerHTML = html;
    },
    error: function() {
      alert("alert error");
    }

  });

  return false;


});

HTML:

<input type="email" id="tempemail" name="tempemail" 
    placeholder="Enter your email address" value="ab.waseem@yahoo.com" required>

The code was working perfectly but suddenly stopped working.

Checking in Firefox and Chrome console gives no error.

The call is sent perfectly to the respective file.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
Talha Aslam
  • 101
  • 8
  • Use your developer console (Chrome or FF) and click the "Networking" tab, then limit to "XHR" - that's your AJAX requests. Trigger the AJAX call. Inspect the call - is it 200? If so, what is the response back from the server? (This is all available in the developer console: https://developer.chrome.com/devtools). Does the call happen? If not, then is your jQuery in the `head` of the document? If so, the `passreset` element does not exist when the jQuery loads, so see this Q&A: http://stackoverflow.com/questions/6173238/jquery-event-not-firing – random_user_name Jul 12 '16 at 16:47
  • Could it be as simple as your URL pointing to `user/forgetpass.php` and your actual file being `forget.php`? (Inferred from your question meta information) – Jonathan Jul 12 '16 at 16:48
  • @cale_b yes ajax call is 200, but it's not passing the data ($_POST['emailforget']) alway's empty.. – Talha Aslam Jul 12 '16 at 16:49
  • Also, if you're using jQuery, things like this: `document.getElementById("error").innerHTML=html;` could become `$('#error').html(html);`.... – random_user_name Jul 12 '16 at 16:49
  • What does the rest of your `
    ` look like?
    – Michael Hommé Jul 12 '16 at 16:49
  • @cale_b that suggestion makes no sense to me – Jonathan Jul 12 '16 at 16:51
  • @Jonathan Ajax call 200, I am getting the response from file, the issue is ($_POST['emailforget'])) alway's remain's null, data is not passing. – Talha Aslam Jul 12 '16 at 16:52
  • @Jonathan - I understand. I just know that - I can't explain why, but when I created the object "on the fly" in the AJAX call, it did not work, but when I declared it explicitly BEFORE the ajax call, it did. Just an idea! – random_user_name Jul 12 '16 at 16:52
  • @pmahomme reset of form is quite simple with a button – Talha Aslam Jul 12 '16 at 16:53
  • @cale_b yes alert(emailforget) contains the value – Talha Aslam Jul 12 '16 at 16:55
  • Change `data` to `data: { emailforget: $("#tempemail").val() },` please – Jonathan Jul 12 '16 at 16:57
  • @Jonathan still not works – Talha Aslam Jul 12 '16 at 16:59
  • @Jonathan - why would that change anything? – random_user_name Jul 12 '16 at 17:04
  • @cale_b it shouldn't. But by process of elimination there was something unsetting or failing to set the data property of the request. Grabbing directly without relying on any other prior assignment implied it was being broken some other way. – Jonathan Jul 12 '16 at 17:08

1 Answers1

2

The problem is processData being false. Just remove this option altogether and it should work. If this does not work, also remove contentType.

For more information read the documentation on the options carefully.

Setting processData to false implies sending the data in the form it was created. Set to true, it will process it into a query-string.

The default content type header is application/x-www-form-urlencoded; charset=UTF-8, and setting the content type to false likewise removes it entirely.

I would stay away from modifying these defaults unless you know exactly why you need to change it in the first place.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • For more details on **why** changing processData will make the difference, see this answer: http://stackoverflow.com/a/6253318/870729 – random_user_name Jul 12 '16 at 17:03
  • @TalhaAslam you're welcome :) Please don't forget to accept the answer when the grace period expires~ – Jonathan Jul 12 '16 at 17:11