Reputation: 458
i am having trouble with decoding the ajax reponse ,here i am sending the organisation,location and building as the inputs which in return gives 2 keys as entrance/exit and key ,now my ajax call is working fine and i can alert the response ,now my reqiurement is to decode the json array i have got and then take the value of entrance/exit into a form field called entrance/exit in the form and type into type field in the form .I have tried to decode the json in the php phage which is executed when the ajax is called and stored the 2 values into sessions but it was not showing up in the form fields when i give the value =$_SESSIN[type] and $_SESSION[entrance/exit] after that i have tried to decode the json script with javascript using console can any one figure out what i a doing wrong. the code upto this is
//ajax
function ajax()
{
var org=document.getElementById('category_id').value;
alert(org);
var loc=document.getElementById('category_id1').value;
alert(loc);
var bui=document.getElementById('category_id2').value;
alert(bui);
var req;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "ajax.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);
req.send();
req.onreadystatechange=function(){
if(req.readyState==4&&req.status==200){
//$(".error").hide();
result=req.responseText
alert(result);
var strJSON = 'result';
var objJSON = eval("(function(){return " + strJSON + ";})()");
alert(objJSON.name);
alert(objJSON.type);
}
}
}
<form name="theForm" method="post" action="addmachine.php" enctype="multipart/form-data" onSubmit="return validate();">
<label for="orgname">Organisation Name</label>
<select style="width: 305px;text-align:left ;" name="category_id" id="category_id" onchange="OrganisationName(this);">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<p>
<label name="location">Location</label>
<select style="width: 305px;" name="category_id1" id="category_id1" onchange="LocationName(this);" >
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<p>
<label for="building">Building</label>
<select style="width: 305px" name="category_id2" id="category_id2" onchange="BuildingName(this);" onchange="ajax(this);">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<label for="entr/exi">Entrance/Exit</label>
<input type="text" name="ent" id="ent" value="objJSON.name" placeholder="enter entrance/exit"/>
<p>
<label for="type">Type</label>
<input type="text" name="type" value="objJSON.type" placeholder="enter your work station"/>
<label for="name">Name</label>
<input type="text" id="workstnname" name="workstnname" placeholder="enter your work station" onblur="return name();" onkeypress="return onKeyPressBlockNumbers(event);">
<label for="description">Description</label>
<textarea name="description" style="height:150px;width:300px;"></textarea>
<label for="machinetype">Machine Type</label>
<select style="width: 305px;text-align:left;" name="machinetype">
<option value="">Select</option>
<option value="kiosk">kiosk</option>
<option value="workstation">workstation</option>
</select>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
</div>
i am not getting the value of the keys entance or exit and type the json iam getting in response and is alerted is
[{"name":"Default Entrance + Exit","type":"both"}]
i dont know whether i have did some bluders in the code or not,as i have only started with javascript thank you
Upvotes: 2
Views: 27897
Reputation: 7756
Try this,
var objJSON = JSON.parse(result);
alert(objJSON.name);
alert(objJSON.type);
Upvotes: 2
Reputation: 2108
Please try this one
JSONObject obj = new JSONObject(result);
List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("interestKey"));
}
using org.json library
Upvotes: 0
Reputation: 386
For the security and workflow reasons it's better to parse json by JSON.parse
var objJSON = JSON.parse(strJSON);
not by eval
Upvotes: 7