Jump to content

$_POST array is empty when sent via ajax


cpx3
 Share

Recommended Posts

Hello everybody,

I try to send data from a formbuilder form with ajax (to verify a mail address). Everything works fine, but as well $_POST as $_GET are always empty. Any idea what I am doing wrong?

$("#Inputfield_email").on("blur", function (e) {
    e.preventDefault();
    
      // Get the form data as an object
      var postdata = $("#Inputfield_email").val();
      alert(postdata); //Works fine
      // Send an AJAX request to the server to retrieve the selected format
      $.ajax({
        url: "/ajax/ajax-is-newsletter-subscriber/",
        type: "GET",
        data: {daten: postdata},
        success: function (result) {
		// Works fine to, but result (print_r($_GET) is empty
          alert("OK" + result);
        }
        }
      });
  });

EDIT: When I use my own php file which is outside Processwire, everything works fine. So the problem seems to be that the GET and POST data gets deleted on the way.

Link to comment
Share on other sites

1 hour ago, cwsoft said:

Hi and welcome,

How does your PHP script look like, what does it return as result on success or failure?

it looks like print_r($_GET) and the result is an empty array. The problem occurs only if I use a Processwire page to handle the request.

Link to comment
Share on other sites

Any reason for not using PW's own $input variable? https://processwire.com/api/ref/wire-input/

First of all, your example code should probably be a POST request as you are submitting data and not requesting a page for instance.
Then you can simply log the received data with something like this, in order to get the content from your object {'daten':'...'}, and validate it as email.

$email = $input->post("daten", "email");
if ($email) {
	//... do what you need to do. For instance create a record, and echo some valid JSON as a status message to display to the user. 
}

 

Link to comment
Share on other sites

First, a million thanks. I tried it that way, too, but without success. GET, POST, everything. The only thing I mentioned is the fact, that when I use get the /?name=value part gets deleted. When I use a page outside Processwire, it works perfectly...

 

Link to comment
Share on other sites

  • 2 weeks later...

$_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. Not when using application/json. For json, you'd typically do something like this:

$data = json_decode(file_get_contents('php://input'), true);

AJAX is Javascript, and Javascript libraries often use the JSON method. I'm not sure if jQuery does, but I had the same issue when working with AngularJS in the past.

  • Like 2
Link to comment
Share on other sites

Actually, the answer is much simpler. I had the same problem with another script. When I call the page with "url: '/ajax/captcha-verify'", the data gets deleted, with url: '/ajax/captcha-verify/' it works. Thanks to everybody!

  • Like 3
Link to comment
Share on other sites

8 minutes ago, cpx3 said:

Actually, the answer is much simpler. I had the same problem with another script. When I call the page with "url: '/ajax/captcha-verify'", the data gets deleted, with url: '/ajax/captcha-verify/' it works. Thanks to everybody!

Ah that is a classic mistake I also made in the past. Page paths always have to end with a backslash in ProcessWire (but that is adjustable in the template settings!).

When you call up an URL you can leave the backslash - this will work - but in that case theres a redirect going on that leads to the data-loss when submitting form data.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Actually that was no solution for that problem as the data get deleted at another site, too. It is a simple AJAX POST, works, when I send it directly (I tried it with get) and all the data is deleted when I do it inside a Processwire templates directory. I made a new directory outside and now it works perfectly, so there cannot be any problems with my code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...