douou6807
2013-12-09 15:00
浏览 75

使用jQuery和$ .ajax将变量传递给PHP

I have a jQuery php script which accesses a database and echoes back html to fill a div on my page. It uses the following code.

function myCall() {
    var request = $.ajax({
        url: "ajax.php",
        type: "GET",
        dataType: "html"
    });

    request.done(function (msg) {
        $("#divholder").html(msg);
    });

    request.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });
}

What I need to do is also send a variable through to the script so it knows what to look for in the database. I have been searching but I can't seem to find quite the right answer. Any help greatly appreciated.

Regards.

图片转代码服务由CSDN问答提供 功能建议

我有一个jQuery php脚本访问数据库并回显html来填充我页面上的div。 它使用以下代码。

  function myCall(){
 var request = $ .ajax({
 url:“ajax.php”,
 type:“GET”,
 dataType  :“html”
}); 
 
 request.done(function(msg){
 $(“#divholder”)。html(msg); 
}); 
 
 request.fail(  function(jqXHR,textStatus){
 alert(“Request failed:”+ textStatus); 
}); 
} 
   
 
 

我需要做什么 还将变量发送到脚本,以便它知道在数据库中查找的内容。 我一直在寻找,但我似乎无法找到正确的答案。 非常感谢任何帮助。

问候。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duankuiyuant3940 2013-12-09 15:03
    已采纳

    You can send the parameter like this :

    function myCall() {
        var request = $.ajax({
            url: "ajax.php?param1=" + 'parameter',
            type: "GET",            
            dataType: "html"
        });
    
        request.done(function(msg) {
            $("#divholder").html(msg);          
        });
    
        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    }
    

    And get the parameter with $_GET

    Or you can send the parameter with POST (look at data : Ajax jQuery)

    已采纳该答案
    打赏 评论
  • dou72260 2013-12-09 15:03

    With a get request just append it to the url.

    $.ajax({
        url : 'ajax.php?someVar=20',
        type : 'GET',
        dataType : 'html'
    )}
    

    You can also add it to the data property and jQuery will url encode your data and append it to the url string.

    $.ajax({
        url : 'ajax.php',
        type : 'GET',
        dataType : 'html',
        data : {
            someVar : 20
        }
    )}
    
    打赏 评论

相关推荐 更多相似问题