1,105,578 Community Members

How to display Json Data as variable

Member Avatar
jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
?>
Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 6 [?]
 
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?

Member Avatar
jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 6 [?]
 
0
 

If you put

alert('datas: ' + datas);

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

Member Avatar
jpknoob
Junior Poster in Training
71 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
ko ko
Practically a Master Poster
673 posts since Jan 2009
Reputation Points: 97 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 1 [?]
 
-1
 

datatype: 'json', should be dataType

Question Answered as of 2 Years Ago by minitauros and ko ko
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: