Jump to content
hellboy

send ajax post request failed

Recommended Posts

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

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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"}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

ok now i get it in the console 

 

the whole html page is in the msg

 

and the php says: thats not working

Share this post


Link to post
Share on other sites

Oh hello thank you for your help. 

it seems that i cannot get the post request to php properly 

Share this post


Link to post
Share on other sites
<?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
}

 

Share this post


Link to post
Share on other sites

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 😞

 

Share this post


Link to post
Share on other sites

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 by Gary Austin
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

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?

 

Share this post


Link to post
Share on other sites
1 hour ago, hellboy said:

<button type="submit" class="checkout">Checkout</button>

you should add a name or id to your submit button.

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

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.... 😡🤬

  • Haha 3

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...