NoLimes
NoLimes

Reputation: 81

PHP AJAX POST/GET

I almost finished my little project but i have a problem with reading data sent to server.

function main() {
jQ(document).on("keyup", "form input", function () {
        var data = new FormData();
        var value = jQ(this).val();
        data.append('value', value);
        console.log("PRINTUJEMY HASELKO: " +value);
        // mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
        jQ.ajax({
            type    : "POST",
            url     : "http://www.mywebsite/result.php",
            data    : data,

            crossDomain: true,
            contentType: false,
            processData: false,

        });
    })
    .keyup();

I attaching a php file :

header("Access-Control-Allow-Origin: *");

$x = ( $_GET['value'] );
echo "PHP Zaladowane".$x;
$q = intval($_GET['$value']);

if

( $_REQUEST["value"] )
{
   $name = $_REQUEST['value'];

   echo "Welcome $x $q". value. $q;
}

function cors() {

    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

    echo "You have CORS!";
}
?> 

I can see in console the success message "PRINTUJEMY HASELKO: ddd jquery.min.js:4 XHR finished loading: POST "http://www.mywebsite/result.php"."

My website is not showing anything (Values from inputs). Have a good night. :)

Upvotes: 1

Views: 121

Answers (1)

Alcinator
Alcinator

Reputation: 317

Your javascript code looks like it's making a POST request to your PHP, in which case you should use $_POST rather than $_GET or $_REQUEST.

Make sure you sanitize any data that comes from there before you use it though, otherwise you're opening yourself to injection attacks.

Upvotes: 1

Related Questions