Hi, I'm working on one sites and having troubles with these:

I have tables catecogires and subcategories in database. When user add article, he
select categorie from select tag and under that tag appears another select tag
with subcategories of selected category. Code in jquery:

$("#ka").change(function(){
		function PostaviPodatke(podaci){
			$("#pka1").show();
			 var niz=podaci.split("+");
			 for(var i=0; i<niz.length; i++){
				 $('#pka').append(new Option(niz[i], niz[i]));
			 }
		}
		var nadkat=$("#ka").val();
		$.post("uzmi_podkategorije.php", { nk: nadkat },  function(result){   
			PostaviPodatke(result);
		});
	});

Code of uzmi_podkategorije.php:

<?php

require_once ("includes/connection.php");
require_once ("includes/functions.php");

$nk1=trim(string_spreman_za_mysql($_POST['nk']));	
$upit="SELECT p.naziv AS naziv
	 FROM podkategorije p, kategorije k
	 WHERE k.naziv='$nk1' AND k.id=p.nadkategorija";
$rez=mysql_query($upit);
$podkategorije="";
for($i=0; $i<mysql_num_rows($rez); $i++){
	$red=mysql_fetch_array($rez);
	$kat=stripslashes($red['naziv']);
	$podkategorije = $podkategorije . $kat . "+";
}
echo $podkategorije;

?>

But this doesn't works. I tested script uzmi_podkategorije.php and it works fine, so
problem is in ajax.

How can I solve this?

Thanks in advance,

Amer

Recommended Answers

All 5 Replies

try:

$("#ka").change(function(){
	var nadkat=$("#ka").val();
	$.post("uzmi_podkategorije.php", { nk: nadkat },  function(result){   
			$("#pka1").show();
			var niz=podaci.split("+");
			var i=-1,limit=niz.length;
			while(++i<limit){
				$('<option value="'+niz[i]+'">' + niz[i] + '</option>').appendTo('#pka');
			}
	});
});

This doesn't work either.

I think that result of ajax call cannot be assigned to variable.
I tried this earlier on simple examples and it didn't work.

So question is: How to manipulate with result of ajax call in jquery?
In examples that I saw, result of ajax call is always set as html of some element, but never assigned to variable.

Is there any solution?

I think that result of ajax call cannot be assigned to variable.

Wrong, you can. In your case, it is in the "result" variable within the post callback function. You can simply take what's in it and assign it so whatever other variable you want.
Do you have a link to your page? There might be some incorrect reference to an element somewhere, but can't know for sure without looking at your markup.

This works...thank you again :)

Glad to help.

Regards,
Hielo

PS: Don't forget to mark the thread as solved!

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.