[ASK] passing php value to jquery ajax

Hi,

I am new to Jquery and want to know why this script is not working.

getfield.php :



$data['value1'] = "this is value 1";
$data['value2'] = "this is value 2"; 	

echo "$data[value1] <|> $data[value2]";


the jquery script:


$.get('getfield.php',
  function(result){	
	alert("Data Loaded: " + result);
  });

  //split the returned value
  var extracted=result.split("<|>"); 					
  $("#val1").text(extracted[0]);
  $("#val2").text(extracted[1]);


the alert window showing : (as expected)



Data Loaded: this is value 1 <|> this is value 2


but if I change the getfield.php so value1 and value2 is get from a record of a database :


.. //get record from database, where $rec is an array of a record
$data['value1'] = $rec[name];
$data['value2'] = $rec[address];

echo "$data[value1] <|> $data[value2]";


the alert window showing :



Data Loaded: <|> 


I want to fill the result to :



 <div id="val1"></div> 
 <div id="val2"></div> 


I found a solution using getJSON but my php version is 5.1 and json is not installed.

I try call the getfield.php through browser and a correct string is displayed, either direct variable value or get value from database.


this is value 1 <|> this is value 2

Jhon Doe <|> United States

this is confuse me, browser result is correct but callback from $.get returning invalid.

help please…

call your file in browser not trough jquery, so u can see if there are any errors.

No reason for your way to not work.

You should be using JSON as interchange format rather than some user-defined string return value that you have to parse using odd constructed separators for data separation.
There’s also low-level implementation in jQuery for remote calls called $.ajax so you should look into that one rather than using $.get or $.post for the task you’re set out to do.

This problem is solved.
It was my mistake, passing value with $_GET.

Thanks Trilli for the idea. :slight_smile: