-3

The AJAX request goes through correctly, I checked with chrome's developer tools, there is a request on quiz.php page, but when I check for $_POST['risultato'] it looks doesn't exist. I noticed though that in Chrome's dev tools there's 2 quiz.php elements (one xhr the other document)

I tried changing the code in several ways, but it seems like it doesn't work

<?php
    if(isset($_POST['risultato'])){
        print($_POST['risultato']);
    }
?>

<script>    
    function inviaRisultati(ris){
            $.ajax({
                url: "quiz.php",
                type: "POST",
                cache: false,
                data: {risultato: ris},
                success: function(){
                    alert("INVIATI");
                }
            })
    }

The program is expected to return the result on quiz.php page (the same page where ajax request is fired), and it's supposed to print it somewhere

EDIT: I fixed it

<?php
    file_get_contents('php://input');
    if(isset($_POST['risultato'])){
        print($_POST['risultato']);
    }
?>

function inviaRisultati(param) {
   return $.ajax({
         url:"quiz.php",
         method:"POST",
         data:{action: "SLC", risultato :param},
         dataType:"text"
    });

}

inviaRisultati(1).done(function(response){``
    document.open(); 
    document.write(response);
});

3 Answers3

0

In Ajax, the data attribute is in JSON format. your data attribute will be like this

data: {risultato: ris}
user2993497
  • 525
  • 1
  • 7
  • 21
  • I tried setting data this way aswell, but it doesn't work anyway – Marius Bauld Apr 03 '19 at 18:01
  • are you sure `ris` is set? before the ajax call, alert or console.log `ris`. And see if it displays. Also, set `risultato` to some string like `"test" `and see if it gets posted. – user2993497 Apr 03 '19 at 18:03
  • Yes, I set it when a button is clicked, and it prints it correctly – Marius Bauld Apr 03 '19 at 18:06
  • try running `inviaRisultati("test")` after declaring it and see if it works. It should. and that will tell you that the problem most likely is elsewhere in your code. – user2993497 Apr 03 '19 at 18:09
  • 1
    Can use string for data also. Easier to use object but nothing wrong with what OP was doing – charlietfl Apr 03 '19 at 18:10
  • I tried doing that, it does the same, the POST request goes through, I can see it in chrome dev tools, but as I said there's 2 quiz.php elements, one that's the correct one that displays the data I've sent, and another (the one that's open) that doesn't display the data as It's supposed to – Marius Bauld Apr 03 '19 at 18:14
  • 1
    @MariusBauld comment about *"the one that is open"* is telling me that you are expecting something magical to happen that shouldn't happen and misunderstanding the whole process – charlietfl Apr 03 '19 at 18:45
0

Hi you can do it this way:

your php script:

     if (isset($_POST["action"])) {
    $action = $_POST["action"];
    switch ($action) {
        case 'SLC':
        if (isset($_POST["risultato"])) {
            $response = $_POST["risultato"];
            echo $response;
        }
    }
    break;
}

Where action is a command you want to do SLC, UPD, DEL etc and risultato is a parameter

then in your ajax:

 var param = $('#yourinputid').val();
function getInfo(param) {
   return $.ajax({
         url:"quiz.php",
         method:"POST",
         data:{action: "SLC", risultato :param},
         dataType:"text"
});

}

call it like this:

getInfo(param).done(function(response){
alert(response);
//do something with your response here
})

Hope it helps

stan chacon
  • 768
  • 1
  • 6
  • 19
-2

HTML CODE

<script>
        jQuery(document).ready(function(){
            ris = 'abcd';
            jQuery.ajax({
                url: "quiz.php",
                type: "POST",
                cache: false,
                data: {risultato: ris},
                success: function(){

                }
            })
        });
    </script>

PHP CODE

<?php

file_get_contents('php://input');

print($_POST['risultato']);

Console Output

enter image description here

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
  • `file_get_contents('php://input')` is not needed – charlietfl Apr 03 '19 at 18:36
  • Yep. I tested it now. – Alaksandar Jesus Gene Apr 03 '19 at 18:39
  • Well that's very strange...been using `$.ajax` with php for many years and never had problems with form encoded data not showing up in `$_POST`. Are you saying `$_POST` is empty without it? – charlietfl Apr 03 '19 at 18:39
  • Sorry. My mistake. I deleted that comment. It works without file_get_contents. – Alaksandar Jesus Gene Apr 03 '19 at 18:40
  • So this answer doesn't really provide anything relevant to aid OP other than it should work which everyone else already knows – charlietfl Apr 03 '19 at 18:46
  • The result in xhr is correct, it doesn't display on the page though, how am I supposed to display it? – Marius Bauld Apr 03 '19 at 18:50
  • My understanding is you are getting the result. But not displaying on the html page(chrome browser). If true, you need to create an element and using that element as reference, you can populate the data using jQuery. If false, can u show us your xhr call as i have shown in my answer image – Alaksandar Jesus Gene Apr 03 '19 at 18:55
  • @MariusBauld I think you assume that the php that generated the page should fill some element in the browser after the ajax post completes. If that is correct that's just not how ajax or php works. it sounds like you should study some ajax tutorials – charlietfl Apr 03 '19 at 19:03
  • charlietfl, I started using ajax today, and I didn't really know how to use it, I thought that as you sent the request the data would have been in $_POST or $_GET, but clearly it's not like that – Marius Bauld Apr 03 '19 at 19:08
  • @MariusBauld If you get the response as html codes then it will be easy. All you need to do is refer an element(lets say element with id="quiz") then $("#quiz").html(response) will give you the html output. If you get as data, you need handson experience with jQuery to populate it properly. – Alaksandar Jesus Gene Apr 03 '19 at 19:13