Skip to content Skip to sidebar Skip to footer

Ajax: Why Does $_request On Server Side Not Contain The Variable Passed From Js In Url By Get Method?

I have three divs saved in an array as simple_html_dom objects. I needed to change a CSS property of two of them on the click of a button. That's easy, but then I also need to make

Solution 1:

I fail to see the error. How do you check that you're not sending the data properly? I suggest using this piece of code in order to check what your server is receiving from your request:

functionxyz() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = newXMLHttpRequest();
        } else {
            xmlhttp = newActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);// Let's see what the server is echoing
            }
        }
        xmlhttp.open("GET","myfile.php?pass_back="+"pass_back",true);
        xmlhttp.send();
    }

Please, let us know the output.

Solution 2:

I would recommend that you use jquery and firebug, and first get rid of the following problem: why is passback variable not received.

Create the file: test.php

<!DOCTYPE html><html><head><metacharset="utf-8"/><scriptsrc="https://code.jquery.com/jquery-2.1.1.js"></script><title>Html page</title></head><body><script>
    (function($){

        $.post("myfile.php", {
            passback: "pooo"
        }, function(d){
            console.log(d);
        });


    })(jQuery);

// or if you cannot use jqueryvar xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=newXMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // do something with the response, if you need it...// document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "myfile.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("passback=pooo");

// src:  http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp</script></body></html>

Then create the file: myfile.php

<?phpif(isset($_REQUEST['passback'])){
    echo"ok";
}
else{
    echo"oops";
}

Next, download firefox, and download the firebug extension (or you can use chrome's console alternatively). The goal here is to see what is being passed through the network.

Launch test.php in a browser, open the firebug console (or chrome's console), and debug until you see the "ok" message.

Post a Comment for "Ajax: Why Does $_request On Server Side Not Contain The Variable Passed From Js In Url By Get Method?"