Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a code in php which returns me JSON of a single row.
PHP
public function getEmploymentDetailsForEdit($id){
		$query="select * from js_employment_details where email_id='".$_SESSION['username']."'";
		$res=mysql_query($query);
		$row=mysql_fetch_array($res);
		echo json_encode($row);
	}


the function is called from below jquery function
JavaScript
function editEmploymentDetails(id){
		$.ajax({
			type:"POST",
			url:'getEmploymentDetailsForEdit.php',
			data:{id:id},
			success:function(response){
				console.log(response);
				$("#expDesignation").val(response[0]['designation']);
				$("#txtOrganization").val(response["organization"]);
				//$("").val(response["designation"]);
				$("#startDate").val(response["starts_date"]);
				$("#endDate").val(response["ends_date"]);
				$("#txtJobProfile").val(response["job_profile"]);
				$("#btnEmployment").val("Update");
			}
		});
	}


when the function enters success the response is coming proper but it the value is not set to the textbox.
Posted
Comments
xszaboj 18-Aug-15 7:41am    
don't use:
$query="select * from js_employment_details where email_id='".$_SESSION['username']."'";

it is really not a good idea ;), your code is vulnerable to sql injection attack. use parameters instead

JavaScript
function editEmploymentDetails(id){
		$.ajax({
			type:"POST",
			url:'getEmploymentDetailsForEdit.php',
            dataType:"json",
			data:{id:id},
			success:function(response){
				console.log(response);
				$("#expDesignation").val(response[0]['designation']);
				$("#txtOrganization").val(response["organization"]);
				//$("").val(response["designation"]);
				$("#startDate").val(response["starts_date"]);
				$("#endDate").val(response["ends_date"]);
				$("#txtJobProfile").val(response["job_profile"]);
				$("#btnEmployment").val("Update");
			}
		});
	}


Forget to write dataType:"json"
 
Share this answer
 
I think you have to use JSON.parse to parse the string into an object
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900