php - Json_encode data using JSON.parse in javascript -
php file
<?php include"connection.php"; $id = $_get['edit']; // echo $id; $sql = "select * form id=$id"; $result = $conn->query($sql); $row=$result->fetch_assoc(); $json_res=array(); $json_res = array('user'=>($row)); echo json_encode($json_res); $conn->close(); ?>
javascript file
<script type="text/javascript"> function loadup(str){ var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { // var json = '{"user":[{"id":"45","name":"dfgmohan","loc":"cherthala","dist":"alappuzha","phone":"456","email":"fgdfgh@gmail.c","gender":"male","proofid":"2","sslc":"sslc","plus2":"plus 2","degree":"degree","pg":""}]}'; var x=xmlhttp.responsetext; var json1=json.parse(x); alert(json1.user[0].id); alert(json1.user[0].name); document.getelementbyid("myd").innerhtml=json1; } } xmlhttp.open("get","plain.php?edit="+str,true); xmlhttp.send(); } </script>
why cant alert this?
alert(json1.user[0].id); alert(json1.user[0].name);
when try alert it, no alert message shown.
when alert message, shown before parsing code after not executing. code stuck on parse code line. how resolve problem?
<?php include"connection.php"; $id = $_get['edit']; // echo $id; $sql = "select * form id=$id"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $json_res = array(); array_push($json_res, $row); echo json_encode($json_res); $conn->close(); ?>
javascript file contains
<script type="text/javascript"> function loadup(str) { var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { //var json = '{"user":[{"id":"45","name":"raj mohan","loc":"cherthala","dist":"alappuzha","phone":"9846320121","email":"rajmohan6535@gmail.c","gender":"male","proofid":"2","sslc":"sslc","plus2":"plus 2","degree":"degree","pg":""}]}'; var x = xmlhttp.responsetext; var json1 = json.parse(x); document.getelementbyid("mydiv").innerhtml = "id : "+json1[0].id; document.getelementbyid("myd").innerhtml = "name : "+json1[0].name; } } xmlhttp.open("get", "plain.php?edit=" + str, true); xmlhttp.send(); } </script>
when give respomce please sure there no html contains in page of php file ... commented lines.. give response , cant parse response text careful ... when pass response php dnt add or comment html contains in php (server hitting page)
i think may u can understand answer ..if not please kindly forgive me.. communication low in english thats y ..
to check if html contains included in responce u alert responce in javascript shows html contains response php have html contains thats y cant parse .....
Comments
Post a Comment