AJAX request is sent, but $_POST doesn't update

jquery ajax
xmlhttprequest
ajax post
jquery ajax post json
ajax api call
asynchronous ajax call jquery example
jquery ajax post example php
javascript ajax post

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);
});

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

data: {risultato: ris}

Submitting Forms Without Reloading The Page: AJAX , You will learn not only how to make an AJAX call, but also how to do it the best way You cannot do it from the front-end, so you have to call the back-end. Usually, we would send a POST request to the server, handle it and redirect the function send_bug_report() { $data = $_POST; // check the nonce if� The URL required a specific parameter in which the request is sent. The optional success parameter is a callback function. It is used to retrieve the returned data that is executed if the request succeeds. Performing GET Request with AJAX using jQuery

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

jQuery AJAX POST Tutorial, PHP expert Jorge Colon walks through the basics of AJAX requests using jQuery and gives You won't be suffering from a venereal disease, I promise. We can send data to the server in a variety of different formats, but the default is� I have a very simple form with 3 checkboxes allowing users to flag a video as inappropriate. I'm trying to make a simple ajax request to create an email, and give the user some feedback. The form seems to post, and the info is being sent in the headers, but nothing is being returned. I'm really new to using ajax, so I'm sure I just missing

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

AJAX - Send a Request To a Server, send(string), Sends the request to the server (used for POST) asynchronously, the JavaScript does not have to wait for the server response, but can instead:. I built a vote system with ajax and php, and I send data to php page for saved data in db. I tried to send data with ajax post and php. My problem is the data is not send to the page.

jQuery AJAX get() and post() Methods, POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request� That information doesn’t change very often so there is no need to make a fresh request every time. There are also some who will say you should not use a POST request for every AJAX call as I

JQuery Ajax POST Method, data : A plain object or string that is sent to the server with the request. success : A callback function that is executed if the request succeeds.it� Our Ajax request will be sent there for processing. type indicates whether the request will send using HTTP ‘$_GET[]’ or ‘$_POST[]’ method. We use ‘$_POST[]’ method here, as we set it

jQuery.post(), Description: Send data to the server using a HTTP POST request. Required if dataType is provided, but can be null in that case. This jQuery XHR object, or " jqXHR," returned by $.post() implements the Promise interface, giving it all the� Code looks fine from what I can tell, no obvious problems. Can you use Firebug or something to inspect the request and make sure the POST data is all there and it’s posting to the correct address? Check the return data of the request too, and maybe test the request with cURL.

Comments
  • How is inviaRisultati() being called? From a form submit? If so, are you preventing the default submit behavior (like you should be)?
  • data: {risultato: ris},
  • I call it after a button is clicked inviaRisultati(value)
  • I'm pretty sure that passing a querystring is acceptable and jquery handles appropriately.
  • @AlaksandarJesusGene actually there another and it is $.ajax default...application/x-www-form-urlencoded
  • I tried setting data this way aswell, but it doesn't work anyway
  • 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.
  • Yes, I set it when a button is clicked, and it prints it correctly
  • Can use string for data also. Easier to use object but nothing wrong with what OP was doing
  • @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