I'm a long-time lurker, first-time poster on Stack Overflow (which has saved me in quite a few programming projects before). I have a unique problem that I haven't been able to find by searching the site, so I'll describe it here.
I am creating an online order form where users can select a food category. That category is sent to another page of code that runs a query on the database and returns all items within that category to a drop-down box named ItemXSelect (where X is the item number). I have not had trouble with this. However, I am having trouble using the value that the user chooses for ItemXSelect in another AJAX Query. Specifically, the AJAX request does not send at all (according to Firebug). I'm hoping it's something simple like a syntax error, but I've been at it for days.
The code below is Jquery/AJAX written inside of a PHP Echo statement and has certain characters escaped. $itemNum determines the value of the X in ItemXSelect.
\$(\"#Item" . $itemNum . "Select option:selected\").change(function() {
var sel = \$(\"#Item" . $itemNum . "Select option:selected\").text());
\$.ajax({
type: \"POST\",
url: \"sizearray.php\",
data: { category: \"Cold_Sandwiches\" , selection: sel },
dataType: \"text\"
}).done(function(result) {
\$(\"#Item" . $itemNum . "Size\").html(result);
});
\$(\"#Item" . $itemNum . "Size\").show();
});
This has been troubling me for days. My thanks go to anyone who can help me find what's wrong here (or even at least try to).
=====================================================================================
EDIT 1
Thanks to your help, I've cleaned up the code by wrapping all the JQuery in a PHP EOD statement instead. That way, I don't have to escape the whole thing. Below is the updated code. The suggestions below have not worked so far to answer the main question, but at least the syntax is much neater.
jQuery("#Item{$itemNum}Select option:selected").change(function() {
var sel = jQuery("#Item{$itemNum}Select option:selected").text());
jQuery.ajax({
type: "POST",
url: "sizearray.php",
data: { category: "Cold_Sandwiches" , selection: sel },
dataType: "text"
}).done(function(result) {
jQuery("#Item{$itemNum}Size").html(result);
});
jQuery("#Item{$itemNum}Size").show();
});