Hello,

I'm trying to get a var from php into my jquery code, but I'm having no luck.

Jquery:

$.ajax({
			
			type : "POST",
			url : "sortProp.php",
			data : "f="+type+"&s="+filter,
			success : function(data){
			
				$('#propHold').html(data);
				$('#propHold .prop:first').addClass('active');
				
				alert(data.title);
			
			}
		
		});

Php:

$title = array("title" => "$propName");
json_encode($title);

I can't seem to get the json_encoded var to come up in my .ajax alert. Any help on doing this would be great!

Thanks
-BaSk

Recommended Answers

All 9 Replies

add dataType:'json' to your $.ajax() options. Also, make sure that your PHP file does NOT return anything after that json_encode statement - try:

<?php
$propName="test";
$title = array("title" => "$propName");
json_encode($title);
exit;
?>

if I change the data type to json, will i need to encode all data?

I'm echoing out some content from sql before i make the $title array.

I can get the content from sql to show if no datatype is set. If I set it to datatype nothing will come up.

if I change the data type to json, will i need to encode all data?

Yes, and that is the reason for your troubles.
For this to work: alert(data.title); the data you are returning must be:
a. ALL in json format
OR
b. you must parse the data parameter of your success function and YOU convert it explicitly.

To clarify on point b, let's say you have:

<?php
echo 'Hello';
$propName="test";
$title = array("title" => "$propName");
json_encode($title);
exit;
?>

that would end up sending the following to the browser: Hello{"title":"test"} since it is NOT a properly-encoded json string, it will NOT be "auto-converted" to a json object by jquery, so within success() , data is a string NOT a json object - that's why you cannot use data.title.

so could you show me how you would make it work for

<?php
echo 'Hello';
$propName="test";
$title = array("title" => "$propName");
json_encode($title);
exit;
?>

Thanks for the help so far!

well, in that specific example I know that the properly encoded string appears after Hello, which consists of 5 characters, so I would skip the first five characters and use $.parseJSON() to convert the remainder of the string to an object:

success: function( data )
{
  var myobj=$.parseJSON(data.substring(5));
alert(myobj.title);
}

You also need to echo json_encode()...; , and you can also send a particular character (or character sequence) where you can split the json string from everything else that has already been send. To clarify, it sounds like you have: ...blah......blah...{"title":"test"} Let's assume that your ...blah......blah... does NOT contain (and never will contain) "BaSk". So if you send that character sequence right before you json string, on the javascript end you can split it at that delimiter(character sequence):

<?php
echo 'Hello';
$propName="test";
$title = array("title" => "$propName");
echo 'BaSk';
echo json_encode($title);
exit;
?>
$.ajax({
			
			type : "POST",
			url : "sortProp.php",
			data : "f="+type+"&s="+filter,
			success : function(data){
			  var temp=data.split("BaSk");

                          //temp[0] should have Hello
                          //temp[1] should have the STRING {"title":"test"}

                          //convert it to an object
                          temp[1]=$.parseJSON(temp[1]);
				$('#propHold').html( temp[0] );
				$('#propHold .prop:first').addClass('active');
				
				alert(temp[1].title);
			
			}
		
		});

That worked, just who i wanted it to.

So why must you use data.split?

because if your script is sending Hello{"title":"test"} , only {"title":"test"} is a JSON string. Read about JSON on the json.org website.

To clarify further, if you are familiar with xml, you would know that this IS Valid XML:

<root>
 <title>Test</title>
</root>

but NOT this:

Hello
<root>
 <title>Test</title>
</root>

The same concept applies to what you are sending. By introducing the "BaSk" delimiter in my previous post, if you split it at that character sequence/delimiter, you would be able to separate the json string (which you are sending at the end of your output) from the leading non-json encoded output.

Thanks a lot for the help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.