treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Post and Get data from JS to PHP

  • hi,

    this the first time i'm create a server side so i'm kinda lost in here.. :)

    i want to from my client side to send the data to the server and the server validate the data and send back the response.

    so i found something like this when i was searching

    function submitData() {
    console.log('Sending Data');
    var JSONObject = {}; 
    var url = 'http://mywebsite/server/'
    
    // construct an HTTP reques
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
    
    // send the collected data as JSON
    xhr.send(JSON.stringify(JSONObject)); 
    
    xhr.onloadend = function () {
    //check the result & error messages
      };
    
    }
    

    i'm need help :(

    thanks in advance.

  • ... what is your question?

  • I have no idea how to submit form with native js.

    in jQuery you can use $.post or $.get

    example: $.post('process.php', {field1: "value", field2: "value"}, function(result){ //result is response from process.php, such as echo $value etc. });

    hope it helps.

  • Hello friend this is also one of the code for js to php

    <?php session_start(); // ... php code ... echo '<script type="text/javascript">alert("Welcome '. $_SESSION['nume']. '");'; ?>