0

i try to do a very simple Webserver witch can send and get JSON Data.

JavaScript:

var username = document.getElementsByName('username')[0].value;
var antwort1 = document.getElementsByName('frag1')[0].value;
var antwort2 = document.getElementsByName('frag2')[0].value;
var antwort3 = document.getElementsByName('frag3')[0].value;

//JSON
var jsondata = {"data" :[
{"name": username},
{"antwort1":antwort1},
{"antwort2":antwort2},
{"antwort3":antwort3}]};


var url = "https://...../apps/server.php";
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "POST", url, true ); 
xmlHttp.send(JSON.stringify(jsondata));
console.log(xmlHttp.responseText);
alert(xmlHttp.responseText);

PHP:

<?php

    $json_data;

    if( $_GET["json"]) {
     echo $json_data; 
     exit();
   }
   /*
    if(!isset($_POST)){
    //$json_data = json_decode($_POST["data"]);
    echo  "test POST";
    exit();
    }
    */

    if(!isset($_POST)){
    $json_data = file_get_contents('php://input');
    echo  $json_data;
    exit();
    }

?>

My main Problem is how can i send JSON to my php server. Ando how can i check this. Finaly i just want to save the Json Data and send it back.

Send JSON data from Javascript to PHP?

I try it like in the link above.

UPDATE: like rick

JavaScript: Two Button one for GET one for POST

   function btn1() {
    alert("btn1");
    var url = "https://....../apps/server.php?json=null";
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", url, false ); // false for synchro nous request
    xmlHttp.send( null );
    alert(JSON.stringify(xmlHttp.responseText));
    console.log(xmlHttp.responseText);


  };


  function btn0() {
    alert("test");
    var username = document.getElementsByName('username')[0].value;
    var antwort1 = document.getElementsByName('frag1')[0].value;
    var antwort2 = document.getElementsByName('frag2')[0].value;
    var antwort3 = document.getElementsByName('frag3')[0].value;
    //alert(username+" "+antwort1+" "+antwort2+" "+antwort3);
    //JSON
    var jsondata = {"data" :[
    {"name": username},
    {"antwort1":antwort1},
    {"antwort2":antwort2},
    {"antwort3":antwort3}]};
    //alert(jsondata.data[0].name);

    var url = "https://...../server.php";
    var xmlHttp = new XMLHttpRequest();

    xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        alert(xmlHttp.responseText);
      }
    };
    xmlHttp.open( "POST", url, true ); 
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send("jsonData="+JSON.stringify(jsondata));

  };

PHP

 <?php
        $json_data = "";

        if( $_GET["json"]) {
         echo $json_data; 
         exit();
       }

        if(!isset($_POST)){
            $json_data = $_POST["name"];
            echo  $json_data;
            exit();
        }


    ?>
Community
  • 1
  • 1

1 Answers1

1

For the POST part it's esier to use application/x-www-form-urlencoded so you can treat the POST data as if it's a form.

For the response you have to setup a callback cause your call is async.

try something like that

var url = "https://...../apps/server.php";
var xmlHttp = new XMLHttpRequest();

xmlHttp .onreadystatechange = function() {
  if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
    alert(xmlHttp.responseText);
  }
};
xmlHttp.open( "POST", url, true ); 
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send("jsonData="+JSON.stringify(jsondata));

For the php

if(!isset($_POST)){
    $json_data = $_POST["jsonData"];
    echo  $json_data;
    exit();
}

It's 10 years than I don't write a line in PHP but I hope the concept is clear.

rick
  • 1,869
  • 13
  • 22
  • I try it but i get a error : Undefined index: json in /var/.../public_html/apps/server.php on line 13. PHP : $json_data; if( $_GET["json"]) { echo $json_data; exit(); } if(!isset($_POST)){ $json_data = $_POST["name"]; echo $json_data; exit(); } It do not work and this is my problem. i got all the time. – thenewOne1234567890 Jun 16 '16 at 15:30
  • 1
    I made a typo, edited. I coded directly here so it can be some errors. Just a question, It's your first time with PHP and JS? if so I'll try to explain a little bit more – rick Jun 16 '16 at 15:33
  • yes it my first time. Why i can not save this json with POST in php and get the same json with GET? – thenewOne1234567890 Jun 16 '16 at 15:35
  • 1
    Ok I'll try to explain better. Try the updated version if isn't working I'll try to write a complete sample – rick Jun 16 '16 at 15:37
  • ok you can see now my update version on the top. – thenewOne1234567890 Jun 16 '16 at 15:44
  • 1
    In the php you still have the name in the $_POST["name"] – rick Jun 16 '16 at 15:47
  • i change it in jasonData. But it still dont work.But why you put jsondata. what i need to put in it? its not just "data"? like the start of my json – thenewOne1234567890 Jun 16 '16 at 15:49
  • hi it seem to work. but one think is still a problem how can i overwritte the json data in php and send it with the get request? – thenewOne1234567890 Jun 16 '16 at 16:09