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

Dani AI

Generated

Short answer: the AJAX response is available only inside the callback (the result parameter). In this thread pointed that out and the real blocker was a variable-name mismatch in the callback; after fixing that reported it worked.

Checklist and best practices to avoid similar problems:

  • Return structured data (JSON) from the server rather than a delimited string. It avoids fragile parsing and trailing-delimiter bugs.
  • Tell jQuery what to expect (dataType: "json"), and let it parse the response.
  • Clear the target <select> before adding new options (.empty()), and create options with $( "<option>" ).val(...).text(...) to avoid HTML injection.
  • Watch for invisible output from PHP (whitespace, BOM, or warnings). Extra output breaks parsing; use error_reporting(0) only for debugging, and check the raw response in DevTools.
  • Use the Network and Console tabs to inspect the AJAX response body and any JavaScript errors. A console.log(result) inside the callback quickly shows what you actually received.

If you need the AJAX value outside the immediate callback, use the promise returned by $.ajax/$.post or move dependent logic into the callback (or trigger a custom event when data is ready). Example patterns:

$.ajax({ url: "uzmi_podkategorije.php", method: "POST", data: { nk: val }, dataType: "json" })
  .done(function(data){ /* populate select here */ })
  .fail(function(xhr){ console.error(xhr.statusText); });
header("Content-Type: application/json; charset=utf-8");
$rows = /* fetch names into an array */;
echo json_encode($rows, JSON_UNESCAPED_UNICODE);

For reference on options and settings see the jQuery.ajax docs: jQuery.ajax.

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.