简体   繁体   中英

Not recieving values sent via AJAX POST to PHP

I'm trying I'm trying to send a specific section of my URL string, to my php file, to then populate my page accordingly. Everything is working fine, except for the AJAX POST method. I've tried doing var_dump of the POST variable in my PHP, and my array is empty (so I know nothing is getting through).

The success DOES return as being passed, so I don't know where the data is going. I'm testing locally on XAMPP, and I've combed through SoF and no luck on any of the fixes. My code is below.

Screen shot of page:

在此处输入图片说明

jQuery AJAX Request:

$(document).ready(function() {
    str = window.location.href;
    pos = str.search("pages/"); //42
    send = str.slice(42, -5);
    console.log(send);
    console.log(pos);
    $.ajax({
        type: "POST",
        url: "retrieve.php",
        data: {
            tom: send
        },
        success: function() {
            $.get("retrieve.php", function(data, status) {
                    $("#main").html(data);
                }) //ends GET function   
        },
        error: function() {
            console.log(arguments)
        }
    }); //ends POST request               
}); //ends DOC-READY function

PHP:

echo "<i>hello</i>";
echo var_dump($_POST);
$url = $_POST['tom'];
json_decode($url);
echo $url;

Try the below,

Also you don't need to json_decode unless you send a json request,And please make sure all the post values are passing.

Ajax:

$(document).ready(function() {
    var str = window.location.href;
    var pos = str.search("pages/"); //42
    var send = str.slice(42, -5);
    console.log(send);
    console.log(pos);
    $.ajax({
        type: "POST",
        url: "retrieve.php",
        data: {
            'tom': send//make sure this is not empty
        },
        success: function(data) {
            console.log(data);
        },
        error: function(arguments) {
            console.log(arguments)
        }
    }); //ends POST request               
}); //ends DOC-READY function

PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //echo "<i>hello</i>";
    //echo var_dump($_POST);
    $url = $_POST['tom'];
    json_encode($url);
    echo $url;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM