954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to display Json Data as variable

Evening, I am trying to grab information from a PHP script using JQuery when the user enters a value into a text input field. The text input box has an auto suggest function so that when the user begins tying they can see what matches from the database.
If the user selects one of the options, JQuery's .change() function then uses AJAX to fetch the data from the database and return the values as JSON.
How do I display the JSON information in a format so that i can use it throughout the rest of the page, i.e. populate form fields?

Here is my jQuery/AJAX;

$(document).ready(function()	{

$('#kinaseEntry').change(function ()	{

$('#waiting').show(500);
$('#message').hide(0);
alert("We have a change");

$.ajax({
type : 'POST',
url : 'post.php',
datatype: 'json',
data:	{
	kinaseEntry : $('#kinaseEntry').val()
},
success : function(data)	{
//SUCCESS CODE IN HERE
},
error : function(error)	{
	alert("Oops, there was an error!");
}
});
return false;
});
});


and the php script;

<?php
//Include connection to databse
require_once 'connect.php';

if (isset($_POST['userInput']))	{

	$input = mysql_real_escape_string ($_POST["userInput"]);
	
	$findInput = "SELECT * FROM table where item = '" .$input. "' ";
	
	if ($result = mysql_query($findInput))	{
	
		$row = mysql_fetch_array($result);
		
		$item1 = $row['item1'];
		$item2 = $row['item2'];
		$item3 = $row['item3'];
		$item4 = $row['item4'];
		$item5 = $row['item5'];
		
		
		/* JSON ROW */
		$json = array ("item1" => $item1, "item2" => $item2, "item3" => $item3, "item4" => $item4, "item5" => $item5, ...etc);
		
	} else	{
	
		/* CATCH ANY ERRORS */
		$json = array('error' => 'Mysql Query Error');
	}
	
	/* SEND AS JSON */
	header("Content-Type: application/json", true);
	
	/* RETURN JSON */
	echo json_encode($json);
	
	/* STOP SCRIPT */
	exit;
}
?>
jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Is there a specific need to show it as JSON? Also, if you want to display it as HTML output, why not just make your headers output it as HTML?

minitauros
Junior Poster
109 posts since Apr 2011
Reputation Points: 36
Solved Threads: 17
 

have updated my ajaxSubmit, but cannot seem to get a result back

$(document).ready(function()	{

$('#kinaseEntry').change(function ()	{

var kinaseEntry = $("#kinaseEntry").val();
var dataString = 'kinaseEntry' + kinaseEntry;

$('#waiting').show(500);
$('#message').hide(0);
alert(kinaseEntry);

//Fetch list from database
$.ajax({
type : 'POST',
url : 'post.php',
datatype: 'json',
data: dataString,
success : function(datas)	{
//Put list into Div
	myDivList = datas.result;
	$('#message').html(myDivList.join())
},
error : function(error)	{
	alert("Oops, there was an error!");
}
});
return false;
});
});


When I run the debugger, I get an error saying "throw Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE)".

Can anyone help?

jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Thanks for the reply minitauros. When the AJAX call is made, the data has to be in JSON/JSONP format, just a spec.

jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

If you put

alert('datas: ' + datas);


(or console.log, whatever you like) in your AJAX'success: ... part, does it output the correct data?

minitauros
Junior Poster
109 posts since Apr 2011
Reputation Points: 36
Solved Threads: 17
 

I get a crazy error message
"uncaught exception: [Exception... "prompt aborted by user" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: resource:///components/nsPrompter.js :: openTabPrompt :: line 462" data: no]"

If i run the php script by itself, i get a list of the expected output.

jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

datatype: 'json', should be dataType

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: