开发者

Passing AJAX variables to a Jquery ui dialog on the same page

Right now I've got a 开发者_如何学Pythonui dialog on a page, and I need some way to pass a javascript variable to the php within the dialog via AJAX. Here's my code:

$('.user').click(function(){
    var user = getID($(this).attr('id'),'User');
    $.ajax({   
        type: "POST",   
        url: "test.php",   
        data: 'user=' + user,
        success: function(){
            $("#dialog").dialog('open');
        }
    });
});

and the beginning of the PHP:

    <?php

        if(isset($_POST['user'])){
            echo '<center><b>User: '.ucfirst($_POST['user']).'</b></center><br />';
        }

The problem is, it's just not getting passed. I'm very new with Ajax so I'm sure I'm messing something up.


This is not working because you don't specify the return variable, see your code with correction:

$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({   
    type: "POST",   
    url: "test.php",   
    data: 'user=' + user,
    success: function(data){ // Here you specify the callback variable from the AJAX call
        alert(data); // Here will show '<center><b>User: UserExample </b></center><br />';
        $("#dialog").dialog('open');
    }
   });
});


You can make your dialog content my loading some page like Userpage.php and then pass User as a url parameter

$('.user').click(function(){
    var user = getID($(this).attr('id'),'User');
    $.ajax({   
        type: "POST",   
        url: "test.php",   
        data: 'user=' + user,
        success: function(){
            $("#dialog").load('/userpage.php?User=' + user).dialog('open');
        }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜