我想使用单个DIV部分,让远程PHP脚本根据其调用方式发送不同的UI,然后让AJAX(jQuery)相应地更新DIV。
作为一个新手,在JavaScript / AJAX部分,我真的不知道如何检查用户当前正在使用哪个UI,我需要知道能够使用正确的参数调用PHP脚本。
这是一些伪代码:
<?php
print "<div id='target'>";
if($authenticated) {
//Check status of user in DB
if($subscribed) {
print "<input type='button' id='mybutton' value='Unsusbcribe'/>";
else {
print "<input type='button' id='mybutton' value='Susbcribe'/>";
}
} else {
print "Username <input type='button' id='username' value=''/>";
print "Password <input type='password' id='password' value=''/>";
print "<input type='button' id='mybutton' value='Logon'/>";
}
print "</div>";
?>
<script type="text/javascript" src="<?php echo $JQUERY['jquery.js']; ?>"></script>
<script type='text/javascript'>
//All buttons point to this event
$(function() {
$(':button').click(function() {
//How were we called?
switch($(#mybutton))
{
//Remote server sends new UI, and AJAX updates DIV locally
case 'Subscribe':
$(#target).html = ajax.post("index.php?action=subscribe");
case 'Unsuscribe':
$(#target).html = ajax.post("index.php?action=unsubscribe");
case 'Logon':
$(#target).html = ajax.post(username : $username, password: $password; "index.php?action=logon");
}
});
});
</script>
感谢您的任何提示。
编辑:对于那些想要使用相同方法来POST数据的人:
var username = $("#username").val();
$("#target").load("myscript.php", {username : username});
答案 0 :(得分:0)
{
case 'Subscribe':
$("#" + targetId).load("index.php?action=subscribe");
break;
case 'Unsubscribe':
$("#" + targetId).load("index.php?action=unsubscribe");
break;
....如需了解更多信息,请参阅:http://api.jquery.com/load/