hellboy Posted May 14, 2018 Share Posted May 14, 2018 Hello all, I need some help :). I want to send a post request via ajax. Everything works just fine in the console but i cannot reach the post request in php I have this button in my shopping-cart.php file as markup <button onsubmit="sendMail()" class="checkout">Checkout</button> this is what sendMail() does: function sendMail() { $.ajax({ method: "POST", url: "<?php echo $pages->get('/shopping-cart')->url ?>", data: { test: "test" } }) .done(function( msg ) { console.log( "return: " + msg ); }); } and this is what i want to echo in the first place per php: $ret = isset($_REQUEST["test"]) ? "this is a: ".$_REQUEST["test"] : "thats not working"; echo $ret; in the console i see the request with a status 200 Request Header: Request URL:http://localhost/shopping-cart/ Request Method:POST Status Code:200 OK Remote Address:[::1]:80 Referrer Policy:no-referrer-when-downgrade Cache-Control:no-store, no-cache, must-revalidate Connection:Keep-Alive Content-Encoding:gzip Content-Length:4527 Content-Type:text/html; charset=utf-8 Date:Mon, 14 May 2018 06:22:13 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive:timeout=5, max=100 Pragma:no-cache Server:Apache/2.4.10 (Debian) Vary:Accept-Encoding X-Frame-Options:SAMEORIGIN X-Powered-By:ProcessWire CMS X-XSS-Protection:1; mode=block Accept:*/* Accept-Encoding:gzip, deflate, br Accept-Language:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 Cache-Control:no-cache Connection:keep-alive Content-Length:9 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 Cookie:useCookies=active; wire=e7e454666fe95e1df8ee1a194de6eb82; wire_challenge=jA9Y9Yym1J52K9MzMN2c%2F%2FH9sX24RzlC; shopping={%22products%22:[{%22articleNumber%22:%22015%2001%20137%22%2C%22amount%22:1}]} Host:localhost Origin:http://localhost Pragma:no-cache Referer:http://localhost/shopping-cart/ User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36 X-Requested-With:XMLHttpRequest test:test the post is submitted but i cannot reach it within the php file can someone help me? thank you very much Link to comment Share on other sites More sharing options...
flydev Posted May 14, 2018 Share Posted May 14, 2018 Hi @hellboy 2 hours ago, hellboy said: Everything works just fine in the console Did you mean that you get the content of the msg javascript variable in your console or you just don't have any error output ? If yes, what is the content of msg ? Link to comment Share on other sites More sharing options...
androbey Posted May 14, 2018 Share Posted May 14, 2018 HI @hellboy and welcome to ProcessWire ? 2 hours ago, hellboy said: function sendMail() { $.ajax({ method: "POST", url: "<?php echo $pages->get('/shopping-cart')->url ?>", data: { test: "test" } }) .done(function( msg ) { console.log( "return: " + msg ); }); } In your function sendMail() you write : data: {test: "test"} The first test in this case wouldn't be a string but a variable. Try to change that into: data: {"test":"test"} 1 Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 ok now i get it in the console the whole html page is in the msg and the php says: thats not working Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 Oh hello thank you for your help. it seems that i cannot get the post request to php properly Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 what i actually want is that i can post a json to page and when succed show a success page Link to comment Share on other sites More sharing options...
lokomotivan Posted May 14, 2018 Share Posted May 14, 2018 <?php echo $pages->get('/shopping-cart')->url ?> your request not hitting the page, you need: <?php echo $pages->get('/shopping-cart/')->url ?> or to make sure u always get the right page even if you change name: <?php echo $pages->get('template=your_shopping_cart_template')->url ?> And dont forget in your shoppping-cart template to check for ajax request: <?php if($config->ajax) { // page was requested from ajax } Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 I dont know what im doing wrong... its realy sh... Its very easy to catch some data from an post request in other languages ... I only have this code right now and cant get it to work <?php if ($config->ajax) { echo "test"; } $action = $pages->get('/shopping-cart/')->httpUrl; ?> <form method="post" action="<?php echo $action ?>"> <input name="prnumber" value="23"> <input name="quantity" value="2323"> <button type="submit" class="checkout">Checkout</button> </form> </div> <?php if($input->post->submit) { echo "super"; } ?> i can see the post request in the network tab and all the data but cannot reach it in pw. its realy frustrating if i build the app not that far i would think about to switch to another cms ? Link to comment Share on other sites More sharing options...
Gary Austin Posted May 14, 2018 Share Posted May 14, 2018 (edited) When you first started this thread you were using Javascript ajax to send your post request... So someone showed how to use $config->ajax to check for request. In your latest example you are using a normal html form submit. To test for submit in that case you would use if ($input->post->submit). Javascript ajax and normal html form submit are sent in two different ways to server. Edited May 14, 2018 by Gary Austin 1 1 Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 thats right but i checking also against $input->post->submit and getting nothing ? Link to comment Share on other sites More sharing options...
Gary Austin Posted May 14, 2018 Share Posted May 14, 2018 So you should double check the url being used for your submit and probably post it here. What response code are you getting from the server? Link to comment Share on other sites More sharing options...
dragan Posted May 14, 2018 Share Posted May 14, 2018 1 hour ago, hellboy said: <button type="submit" class="checkout">Checkout</button> you should add a name or id to your submit button. 2 1 Link to comment Share on other sites More sharing options...
hellboy Posted May 14, 2018 Author Share Posted May 14, 2018 So now it works. No clue why, im getting through the git changes and compare it with that im having right now ? Thanks for all your great answers sorry that im beeing so harsh Thanks for your time. this is the code that works just fine <input class="checkout" type="submit" name="submit"> </form> </div> <?php if ($input->post->submit) { foreach($input->post as $key => $value) echo htmlentities("$key = $value") . "<br />"; echo "<p>Data successfully submitted {$sanitizer->text($input->post->prnumber)} amount: {$sanitizer->text($input->post->quantity)}</p>"; } AAAAAAAAAAAAAAAAHHHHHHHHHHHHHHH..... 14 minutes ago, dragan said: you should add a name or id to your submit button. That was the issue ..... oh my god.... ?? 3 Link to comment Share on other sites More sharing options...
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