How to use jquery ajax json

I have a main.php that uses ajax in this manner:
$.ajax({
  url:'getCustomerInfo.php', 
  data: "c=" + $("#customerList :selected").val(), 
  dataType: 'json',
success: function(data)     
  {
    var id = data;            
    $('#customerInfo').html(id); 
  },
 error: function(XMLHttpRequest, textStatus, errorThrown) { 
   alert("Status: " + textStatus); alert("Error: " + errorThrown); 
}       

Open in new window

getCustomerInfo.php looks like this:
...
$row = $result->fetch_assoc();
$theID = $row["ID"];
$theCompany = $row["customerCompany"];
...
echo json_encode($theCompany);
//echo json_encode($theContact1FirstName);

Open in new window


When this is executed my div (#customerInfo) shows the company name ($theCompany).
If I uncomment out $theContact1FirstName I get a parsererror unexpected string error.

How can I get and parse all 12 fields from this recordset?
Sheldon LivingstonConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
You have to return the whole array and then process it in the success section of your Ajax call:

getCustomerInfo.php
...
$row = $result->fetch_assoc();
echo json_encode($row);
...                                  

Open in new window


main.php
$.ajax({
  url:'getCustomerInfo.php', 
  data: "c=" + $("#customerList :selected").val(), 
  dataType: 'json',
  success: function(data)     
  {
	$( v ).each( function ( a, b )
	{
            $('#theCompany').text(b['theCompany']); 
            $('#theContact1FirstName').text(b['theContact1FirstName']); 
	} );
  },
 error: function(XMLHttpRequest, textStatus, errorThrown) { 
   alert("Status: " + textStatus); alert("Error: " + errorThrown); 
}       

Open in new window


Notice that in this expression

            $('#theCompany').text(b['theCompany']);

you have to replace the $('#theCompany') with the selector of the actual div/input where you want to put the info; also you have to replace b['theCompany'] with the database column name: b['column_name'].

Json is useful because it allows you to pass array between php and javascript so you must echo an array within the php script. You could always don't use json, use dataType: 'html', then process all result in your php script and finally echoing a string which holds the html you want to print in the page but this ends with a messy code where data and markup are merged together and it can become very difficult to mantain. The correct way is the one you started with and I suggest to follow that way and apply my code to process php script output.
Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sheldon LivingstonConsultantAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.