我在学习如何成功使用ajax时遇到了很多困难。在我正在进行的学习这个项目中,我在图表上的各种人之间进行选择。当我选择这个人时,我会单击他们的按钮并更新数据库。然而,我需要能够填充一个模态,以便向那个人发送消息。我一直在尝试使用ajax来实现这一点,因为我只需要选择一个$userid变量。
我的代码如下,如果有一种更简单的方法可以在不使用ajax的情况下做到这一点,那也太好了!
php
<?php if (isset($_POST['usrid'])) {
$whatineed = $_POST['usrid'];
?>
div modal stuff
<?php>
js
$(".selectLink").click(function(e){
e.preventDefault();
var _self = this;
$.post(
site_url("user/update"),
{
usrid: $(this).attr("usrid")
}, function(){
$('#Contact').modal({show:true, backdrop: 'static', keyboard: false});
}
)
return false;
});
如有任何帮助,我们将不胜感激!
我没有机会检查代码,但我会尽力给你一个完整的画面:
update.php:
<?php
if (isset($_POST['usrid'])) {
$whatineed = "The updater have an user ID " . $_POST['usrid'];
} else {
$whatineed = "The updater have nothing to do, because an user ID have not been received.";
}
echo($whatineed);// this is the message TO modal
?>
persons.html
<a userid="abc1" class="selectLink" href="#">click me to start update</a>
<div id="Contact" title="I have a message from the update scrtipt">
<p></p>
</div>
个人.js
$(".selectLink").click(function (e) {
var userid = $(e.target).attr("userid");
var _self = this;
$.post(site_url("user/update.php"), {
usrid: usrid
}, function (data) { //message FROM update.php
var dlg = $('#Contact'),
txt = $('#Contact p');// or txt = dlg.find("p");
txt.html(data);
dlg.dialog({
resizable: false,
height: 150,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
}
}
});
})
return false;
});
再来一次——这只是一个要点。这些部分必须在整个代码中进行调整和调试。