2014-07-17 64 views
-3

是否有可能在ajax调用中传递变量?是否可以在ajax调用中传递变量?

$(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); 
        } 
       }); 
      }); 

在ajax.php,我有fuction.I想调用结果函数在ajax.php 我没有得到生存。

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']; 
     } 
    } 
} 
+0

请参阅http://stackoverflow.com/questions/18413969/pass-variable-to-function-in-jquery-ajax-success-callback –

+0

我假设您想要$ template为var'template'? – t3chguy

+0

现在这个问题还不是很清楚。结果是一个PHP函数吗? –

回答

2
$(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); 
        } 
       }); 
      }); 
+0

我没有得到回应。我认为有一些错误。 – user9293

0

是...你可以这样做:

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

});

请检查的jQuery的文档Ajax请求:http://api.jquery.com/jquery.ajax/

0

如果你只是想传递的$template的值称为result一个PHP函数中使用,你可以这样做:

// 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); 
     } 
    }); 
}); 

这将JS函数result($template)的结果传递给url。对于GET请求,jQuery将序列化data属性并形成一个查询字符串,该字符串将附加到该URL。如果你愿意,你可以只是做自己通过改变url属性

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

,而不是指定data属性。

无论哪种方式,服务器上的$_GET['function']都会包含您的数据。

相关问题