retrieving ajax results and displaying those results

ok, I’m not a complete noob with jquery, but i’m not that great either. So far I’ve only ever used it to display results from another page and I know I don’t need to do that.

What i’m doing is having the user select a upc code, then using that upc to query a db table to get the rest of the information. I figure I can probably do it with json, but I’ve never used it and am really confused atm. I need to change the values of quite a few text boxes (that I can do with append or attrib).

This is the extent of my jquery script:

[code][/code]and this is the form (any empty values are going to replaced)

[code]



































Select UPC:<?=$upc?> Content Type: <?=$content?> Label Name: Artist Name:
Content Title: Media Type: <?=$mcontent?> Genre: Format: <?=$fcontent?>
Store Date: Type: <?=$tcontent?> Instant Grat: Group:
Week 1 National Forecast: Projected Wk 1 Market Share: iTunes Week 1 Forecast: Cumulative New Rel. Sales
Sales History
Description
Developing Artist Cost: LP/Extras Marketing Drivers
[/code]

your code now does a request with the following params

_:1390085948750[/code]

You don’t need the question mark at the end of the url, and I’d change the type from GET to POST, mainly to avoid clutter in the server logs

You should also name the upc variable. When it is sent without a name and value it is automatically interpreted as the name

This is probably what you want: http://yoursite.com/?upc=1001
This is what you’re getting: http://yoursite.com/?1001

[php] [/php]

Now you can use the variable server side with $_GET[‘upc’]

Then do your db magic, rebuild your array if needed, and just json_encode it and it should work.

[php]<?php

// this should be your data from the db
$array = [
0 => [
‘title’ => ‘awesome’,
‘description’ => ‘describing awesome’
],
1 => [
‘title’ => ‘not so awesome’,
‘description’ => ‘describing not so awesome’
]
];

// json_encode converts arrays/objects into valid json
echo json_encode($array);[/php]

Output:

[{"title":"awesome","description":"describing awesome"},{"title":"not so awesome","description":"describing not so awesome"}]

now you should have an array with two objects in the result variable in javascript…

Didn’t know what it would put out, just needed to figure out how to retrieve the results so I could fill the table.

So once its encoded, how do I use the results? with parseJSON?

The jquery ajax request you have set up will parse it for you

So the success: would look something like

success: function(result) {
  	$("#labelname").val(label);
	$("#artistame").val(artist);
}

Use your browsers debugging tool to break js execution inside the success function. Then you can just type in result and hit enter in the console and see what the result variable contain.

If you return a simple array with keys-values then you should probably use result.label and result.artist

I got it, thanks for the help. I just wasn’t echoing the json_encode.

I do have one more question. I’m at the point where I need to submit all this information to be inserted into the database table. Do I need to build the entire json string, like what I had to do with the upc, or will it will build it for me if I use serialize() to grab all the values?

This is what I had planned on using to submit the data, not all that different from what I use on my other forms.

[code]function submit_info() {
var data = $("#genit").serialize();
var paid = $("#package_id").val();

url: 'ajax_submit_iTmarket.php',
	data: data,
	type: "POST",
	cache: false,
			
	success: function(result) {
		$("#labelname").attr('value', '').val('');
		$("#artistname").attr('value', '').val('');
		$("#genre").attr('value', '').val('');
		$("#contenttitle").attr('value', '').val('');
		$("#submit_suc").html(paid + " has been added");
	}
});

}[/code]

In the first example you are transmitting a normal GET request, in the second a normal POST request. In the first one you say you are expecting a JSON response from the server, not the other way around.

In the second example you need a way to tell javascript if the insert was successful or not. You could again use json, or http status codes.

forget about the stuff above, theyre for 2 different things.

the first one is for getting information to fill in some blank fields. this one is for taking that information (and some other info) and submitting that info to a db table.

Sponsor our Newsletter | Privacy Policy | Terms of Service