Here is a json encoded string:
{
"user": [{
"id": "45",
"name": "Raj",
"loc": "kizhakood",
"dist": "wayanad",
"phone": "9815678421",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3c2f243d2f262f202d2f0e29232f2722602d">[email protected]</a>",
"gender": "male",
"proofid": "2",
"sslc": "SSLC",
"plus2": "PLUS 2",
"degree": "DEGREE",
"pg": ""
}]
}
When attempting to JSON.parse(responcetext), an error occurs:
SyntaxError: JSON.parse: unexpected character at line 1 column 4 of the JSON data
This code snippet includes JavaScript functionality:
<script type="application/javascript">
function loadup(str){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var jsontext = xmlhttp.responseText;
document.getElementById("mydiv").innerHTML=jsontext;
var userobj=JSON.parse(jsontext);
document.getElementById("myd").innerHTML=userobj.user[0].name;
}
}
xmlhttp.open("GET","plain.php?edit="+str,true);
xmlhttp.send();
}
</script>
The PHP file that generates the JSON response:
<?php
include"connection.php";
$id = $_GET['edit'];
$sql = "SELECT * FROM form WHERE id=$id";
$result = $conn->query($sql);
$row=$result->fetch_assoc();
$json_res=array();
$json_res = array('user'=>array($row));
echo json_encode($json_res);
$conn->close();
?>