cpx3 Posted August 1, 2023 Posted August 1, 2023 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.
cwsoft Posted August 2, 2023 Posted August 2, 2023 Hi and welcome, How does your PHP script look like, what does it return as result on success or failure?
cpx3 Posted August 2, 2023 Author Posted August 2, 2023 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.
eelkenet Posted August 2, 2023 Posted August 2, 2023 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. }
cpx3 Posted August 2, 2023 Author Posted August 2, 2023 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...
poljpocket Posted August 2, 2023 Posted August 2, 2023 Any chance your .htaccess for PW is non-standard?
Gideon So Posted August 3, 2023 Posted August 3, 2023 @cpx3 Just out of curiousity, how do you embed the formbuilder form?? Gideon
cpx3 Posted August 3, 2023 Author Posted August 3, 2023 With method D 15 hours ago, poljpocket said: Any chance your .htaccess for PW is non-standard? No, I did not touch it.
cwsoft Posted August 3, 2023 Posted August 3, 2023 @cpx3Are there any Javascript errors in the console? Seems you are using jQuery for the Ajax request. Maybe there are some conflicts with other JS libs.
wbmnfktr Posted August 4, 2023 Posted August 4, 2023 Had a similar issue a while back because I used this:'Content-Type': 'application/json', Just switched the content type and it started working:'Content-Type': 'application/x-www-form-urlencoded', 2
Inxentas Posted August 18, 2023 Posted August 18, 2023 $_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. 2
cpx3 Posted August 25, 2023 Author Posted August 25, 2023 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! 3
Stefanowitsch Posted August 25, 2023 Posted August 25, 2023 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. 1
cpx3 Posted October 10, 2023 Author Posted October 10, 2023 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now