-2

I try to send data to server with AJAX (pure JS). And I want to send it on the request body.

I know how to do it in jQuery:

$.ajax({
    url: 'someurl',
    data: 'foo'
});

What I mean is how I can send data to server like jQuery does when using ajax method with data attribute in pure js ?

Also, in PHP, how can I get this data?

I know that with laravel I need to do

Request::getContent();

(if I remember right)

But I don't know how to get this data in PHP.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Harman met
  • 241
  • 2
  • 3
  • 10
  • A Google search for "JavaScript AJAX example" shows many, many examples. Including right here on Stack Overflow: http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery As for how to get the data in PHP, any introductory PHP tutorial will show how to read GET and POST data. – David Aug 23 '16 at 19:12
  • @David you dont understand my question, I dont want to send the data in post/get variables I want to send it in request body – Harman met Aug 23 '16 at 19:27
  • That's what a POST request *does*. It sends data in the request body. If there's more to your question than what's being asked, perhaps you could elaborate in the question itself? Include an example of what you've attempted and where you got stuck. – David Aug 23 '16 at 19:30
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Gerard Roche Aug 23 '16 at 19:49

2 Answers2

2

In terms of sending the AJAX request via javascript without relying on jQuery, that's a pretty broad question. You should probably review the documentation here.

A simple example

// Old compatibility code, no longer needed.
if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
    httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 6 and older
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data = /* some data here */
httpRequest.send('data=' + encodeURIComponent(data));

To retrieve this in PHP you would use either $_POST or file_get_contents('php://input') depending on the Content-type header sent in the request. The above example we use a application/x-www-form-urlencoded Content-type header, with a POST HTTP verb, so the information will be populated in $_POST['data'].

Sherif
  • 11,786
  • 3
  • 32
  • 57
  • But I dont want to send the data as a post variable, I want to send the data in request body – Harman met Aug 23 '16 at 19:26
  • 1
    A POST variable ___is___ in the request body. If you wish to send it as something other than `application/x-www-form-urlencoded` content type you can always do that as well. – Sherif Aug 23 '16 at 19:28
-1

Hope this helps! This is an AJAX request at a very broad scope and I tried to make the example as easy as possible to understand! :)

my-web-page.php

<a onclick="<?php $myValue; ?>">Send request</a>

<script>
    $.ajax({
        type: "POST",
        url: 'my-script.php',
        data:{
            action: 'send',
            key: $myValue
        }
    });
</script>

my-script.php

<?php
    $myValue = $_POST['key'];
?>
pascalallen
  • 296
  • 1
  • 3
  • 15