brigo brigo - 1 year ago 61
jQuery Question

First script using $.ajax and it doesn't work

This would be my very first attempt with jQuery and Ajax (and Jacascript in general), so don't shoot me if it's rubbish!

What I want to achieve is: having a page

where a product is displayed with a nice img and a description. At the bottom putting one
menĂ¹: when an
is clicked, a jQuery code load the code into
passing to it the
of the clicked option.
The .php script will analyze the
and, based on its value, it will send a query to extract the relative price and img. Now, before doing this, I wrote a really simple code (following the official documentation and some answers here) that should pass a variable
back to the HTML page but I guess the callback it doesn't work and I don't have enough experience in jQuery yet to find the problem and a relative solution.

Can anyone more pro explain me why this doesn't work?


<script src=""></script>
$("#menuSelect").on("click", function(){
var str = $("#menuSelect").val();
type: "POST",
url: "tester.php",
data: { value: str },
success: function(data){

<div id="txt" >

<select id="menuSelect">
<option value="0"></option>
<option value="1">OPT1</option>
<option value="2">OPT2</option>


$value = $_POST["value"];
function provaAJAX($value) {
switch ($value) {
case "0":
$return = "First option selected";
case "1":
$return = "Second option selected";
case "2":
$return = "Third option selected";
return $return;

$return = provaAJAX($value);

Answer Source

You should print the variable to be passed back to the JS :

echo $return;

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download