是否可以在ajax调用中传递变量? [关闭]

Is it possible to pass variable in ajax call?

$(document).on('click','#Quote_create_value',function(){ 
            $template = $(this).val();
                $.ajax({
                    type : 'GET',
                    url : '../../../protected/config/ajax.php',     
                    data:"function=result($template)",
                    success : function(response){
                        $("#Quote_template_value").html(response);
                    }
                });
           });

In ajax.php, I have that fuction.I want to call result function in the ajax.php I am not getting the respose.

if(isset($_GET['function'])) {
    if($_GET['function'] == 'templateDropDown') {
        $query = "select * from quote where template IS NOT NULL";
        $result = mysql_query($query, $con);
        while ($row = mysql_fetch_assoc($result)) {
            echo '<option value="'.$row['template'].'">' . $row['template'] . '</option>';
        }
        mysql_free_result($result);
    } 
    elseif($_GET['function'] == 'result($template)') {
        $query = "select * from template where templateName=$template";
        $result = mysql_query($query,$con);
        while ($row = mysql_fetch_assoc($result)) {
            echo $row['unitcost'];
        }
    }
} 
$(document).on('click','#Quote_create_value',function(){ 
            $template = $(this).val();
            var functionVal = result($template)
                $.ajax({
                    type : 'GET',
                    url : '../../../protected/config/ajax.php',
                    data:"function=result("+functionVal+")",
                    success : function(response){
                        $("#Quote_template_value").html(response);
                    }
                });
           });

Yes... you can do this:

$.ajax({
   type: "POST",
   url: "some.php",
   data: { name: "John", location: "Boston" }
}).done(function( msg ) {
      alert( "Data Saved: " + msg );

});

Please check the documentation of jQuery for ajax requests: http://api.jquery.com/jquery.ajax/

If you just want to pass the value of $template to be used in a PHP function called result, you can do this:

// no advantage to using 
// $(document).on('click','#Quote_create_value'
$('#Quote_create_value').on('click', function() {  
    var $template = $(this).val();   // no longer a global variable        
    $.get({                          // shorthand for AJAX GET request
        url : '../../../protected/config/ajax.php',     
        data : { function : result($template) },
        success : function(response) {
            $("#Quote_template_value").html(response);
        }
    });
});

This passes the result of the JS function result($template) to url. For a GET request, jQuery will serialise the data attribute and form a query string which will be appended to the URL. If you wanted, you could just do this yourself by changing the url attribute to

url : '../../../protected/config/ajax.php&function=' + result($template)

and not specifying a data attribute.

Either way, $_GET['function'] on the server will contain your data.