2014-12-15 18 views
6

Sto creando un dati JSON con un evento click. quindi sto cercando di inviare i dati json al mio script php tramite ajax e avvisare una risposta. Ma non sono in grado di inviare i dati JSON al mio script php. il suo ritorno NUll.impossibile inviare dati json tramite ajax

script di jQuery:

var jsonObj = []; 
$("#additembtn").click(function(event){ 
event.preventDefault(); 
var obj = {}; 
obj["medicine_name"]=parsed.medicine_name; 
obj["quantity"]=unit; 
obj["price"]=price; 
jsonObj.push(obj); 
console.log(jsonObj); 
}) 

$("#order").click(function(event){ 
event.preventDefault(); 
$jsonObj=JSON.stringify(jsonObj) 
$.ajax({ 
url: "../siddiqa/function/ordermedicine.php", 
type: "POST", 
//dataType: "json", 
data: jsonObj, 
success:function(data, textStatus, jqXHR) 
     { 
     alert(data); 

     }, 
error: function(jqXHR, textStatus, errorThrown) 
     { 
      //if fails 

     } 
}) 


}) 

script PHP

<?php 
require_once('../configuration.php'); 
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname); 
if (mysqli_connect_errno($con)) { 
    die('The connection to the database could not be established.'); 
} 
$obj = json_decode($_POST['jsonObj']); 

echo $obj['medicine_name']; 


?> 

Impossibile ottenere i dati di utilizzo su script PHP e il php ritorno reponse NULL

+1

qual è l'errore? niente su console? scheda di rete? –

+0

Impossibile ottenere i dati di utilizzo sullo script php e il php che restituisce la risposta NULL – oyshee

+1

Do 'print_r ($ _ POST);' nel tuo script PHP, questo dovrebbe mostrarti il ​​problema. Stai provando ad accedere a '$ _POST ['jsonObj']', quando non c'è la chiave jsonObj nei tuoi dati post. – Seventoes

risposta

5

Il problema è che si stanno cercando di inviare un array e devi inviare un object :

$.ajax({ 
    url: "../siddiqa/function/ordermedicine.php", 
    type: "POST", 
    data: { data: jsonObj }, 
    success:function(data, textStatus, jqXHR) { alert(data); }, 
    error: function(jqXHR, textStatus, errorThrown) { } 
}); 

Poi nel vostro lato PHP si potrebbe ottenere la scrittura valore: $obj = $_POST['data'];

+0

Grazie mille, ora il suo returing i dati. Esso restituzione dei dati come segue Array ( [dati] => Array ( [0] => Array ( [medicine_name] => nappa supplementare [quantità] => 2 [prezzo] => 22 ) [1] => Array ( [medicine_name] => minio 10 [quantità] => 2 [prezzo] => 10 ) ) ) ora come ottenere il nome del medicinale dall'array? echo $ _POST ['medicine_name']; non funziona – oyshee

+0

Non ho dimestichezza con PHP ma è necessario accedere a medicine_name tramite $ obj var ... probabilmente scrivendo qualcosa come $ obj [0] ['medicine_name'] – ianaya89

+0

$ obj = json_decode ($ _ POST ['data ']); returing Attenzione: json_decode() si aspetta che il parametro 1 sia stringa, matrice data in /home/windsys/public_html/siddiqa/function/ordermedicine.php sulla riga 7 – oyshee

0

JSON oggetto deve essere un oggetto, non array. Faresti meglio a fare qualcosa del genere.

$jsonObj = {array: jsonObj}; 
$jsonObj = JSON.stringify(jsonObj); 
Problemi correlati