简体   繁体   中英

pass value to another php using ajax and javascript object..?

when i tried to json_decode the json.stringify data it returned NULL so used **json_last_error to know the error and it returned the following error message

Syntax error, malformed JSON

can anyone verify my ajax code enclosed in javascript function and spot the error ive done.

Below is the script code

 <script>
 function callphp(){
 var dataa = {};
 dataa.dateipone = jQuery("#dateInputone").val();
 dataa.dateiptwo = jQuery("#dateInputtwo").val(); 
 dataa.ino = jQuery("#ino").val();
 dataa.submit = "submit";
 alert("Hello")
 $.ajax({
    url : "six-cuf.php",
    type: 'POST',
    data :JSON.stringify(dataa),
    contentType : "application/json; charset=utf-8",
    success:function(data)
    {
        if(data){ 
          alert(data);   
          //console.log("Data from Server"+JSON.stringify(data));
        }
        else{
          console.log("Data is empty");
        } 
    },
    error: function(xhr) {
        alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
        //console.log('Error!  Status = ' + xhr.status + " Message = " +  xhr.statusText);
    }
   });
 }

Below is six-cuf.php

  $data = json_decode($_POST['dataa']);
  print_r($data);

  switch (json_last_error()) {
   case JSON_ERROR_NONE:
         echo ' - No errors';
         break;
   case JSON_ERROR_DEPTH:
         echo ' - Maximum stack depth exceeded';
          break;
   case JSON_ERROR_STATE_MISMATCH:
         echo ' - Underflow or the modes mismatch';
          break;
   case JSON_ERROR_CTRL_CHAR:
         echo ' - Unexpected control character found';
          break;
   case JSON_ERROR_SYNTAX:
         echo ' - Syntax error, malformed JSON';
          break;
   case JSON_ERROR_UTF8:
         echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
          break;
   default:
         echo ' - Unknown error';
         break;
   }

   if(isset($_POST["dateipone"],$_POST["dateiptwo"],$_POST["ino"],$_POST["options"],$_POST["dateone"],$_POST["datetwo"],$_POST["submit"]))
{

    //php code
}

in dateipone,dateiptwo and dateone,datetwo...value of one this pair will be empty.

There's no need to encode anything into json before passing to server. You can pass plain javascript object, browser will do the rest:

var dataa = {};
dataa.dateipone = jQuery("#dateInputone").val();
dataa.dateiptwo = jQuery("#dateInputtwo").val(); 
dataa.ino = jQuery("#ino").val();
dataa.submit = "submit";
$.ajax({
    url : "six-cuf.php",
    type: 'POST',
    data: dataa,
    // no content-type here!
    success:function(data) {

    }
});

On server side check $_POST array as usual:

print_r($_POST);
// you will have keys: `dateipone`, `dateiptwo`, `ino` and `submit` in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM