我有一个用于向页面添加评论的表单;
<?php if(app('current_user')->role != 'user'): ?>
<div class="leave-comment">
<div class="control-group form-group">
<h5><?= trans('leave_comment'); ?></h5>
<div class="controls">
<textarea class="form-control" id="comment-text"></textarea>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<button class="btn btn-success" id="comment">
<i class="fa fa-comment"></i>
<?= trans('comment'); ?>
</button>
</div>
</div>
</div>
<?php else: ?>
<p><?= trans('you_cant_post'); ?></p>
<?php endif; ?>
这使用ajax加载以下情况;
case "postComment":
echo app('comment')->insertComment(ASSession::get("user_id"), $_POST['comment']);
break;
哪些负载;
public function insertComment($userId, $comment)
{
$userInfo = $this->users->getInfo($userId);
$datetime = date("Y-m-d H:i:s");
$this->db->insert("as_comments", array(
"posted_by" => $userId,
"posted_by_name" => $userInfo['username'],
"comment" => strip_tags($comment),
"post_time" => $datetime,
"FK_DappID" => 1
));
return json_encode(array(
"user" => $userInfo['username'],
"comment" => stripslashes(strip_tags($comment)),
"postTime" => $datetime
));
}
注释用在名为view.php的页面上,我有一个变量,它是从名为$ _GET ['ID']的htis页面的URL设置的。
SO ..
如何更改“FK_DappID”=&gt; 1是要插入到数据库中的数组,值为$ _GET ['ID']?
我尝试过使用“FK_DappID”=&gt; $ _GET ['ID']但它不起作用..
任何帮助都会非常适合。
谢谢,B。
编辑:Index.js
$(document).ready(function () {
//comment button click
$("#comment").click(function () {
//remove all error messages
asengine.removeErrorMessages();
var comment = $("#comment-text"),
btn = $(this);
//validate comment
if($.trim(comment.val()) == "") {
asengine.displayErrorMessage(comment, $_lang.field_required);
return;
}
//set button to posting state
asengine.loadingButton(btn, $_lang.posting);
$.ajax({
url: "ASEngine/ASAjax.php",
type: "POST",
data: {
action : "postComment",
comment: comment.val()
},
success: function (result) {
//return button to normal state
asengine.removeLoadingButton(btn);
try {
//try to parse result to JSON
var res = JSON.parse(result);
//generate comment html and display it
var html = "<blockquote>";
html += "<p>"+res.comment+"</p>";
html += "<small>"+res.user+" <em> "+ $_lang.at +res.postTime+"</em></small>";
html += "</blockquote>";
if( $(".comments-comments blockquote").length >= 7 )
$(".comments-comments blockquote").last().remove();
$(".comments-comments").prepend($(html));
comment.val("");
}
catch(e){
//parsing error, display error message
asengine.displayErrorMessage(comment, $_lang.error_writing_to_db);
}
}
});
});
});
编辑:Ajax.php
include_once'AS.php';
$ action = $ _POST ['action'];
开关($ action){ 案例'checkLogin': app('login') - &gt; userLogin($ _ POST ['username'],$ _ POST ['password']); 打破;
case "registerUser":
app('register')->register($_POST['user']);
break;
case "resetPassword":
app('register')->resetPassword($_POST['newPass'], $_POST['key']);
break;
case "forgotPassword":
$result = app('register')->forgotPassword($_POST['email']);
if ($result !== true) {
echo $result;
}
break;
case "postComment":
echo app('comment')->insertComment(ASSession::get("user_id"), $_POST['comment']);
break;
case "updatePassword":
app('user')->updatePassword(
ASSession::get("user_id"),
$_POST['oldpass'],
$_POST['newpass']
);
break;
case "updateDetails":
app('user')->updateDetails(ASSession::get("user_id"), $_POST['details']);
break;
case "changeRole":
onlyAdmin();
$result = app('user')->changeRole($_POST['userId'], $_POST['role']);
echo ucfirst($result);
break;
case "deleteUser":
onlyAdmin();
$userId = (int) $_POST['userId'];
$users = app('user');
if (! $users->isAdmin($userId)) {
$users->deleteUser($userId);
}
break;
case "getUserDetails":
onlyAdmin();
respond(
app('user')->getAll($_POST['userId'])
);
break;
case "addRole":
onlyAdmin();
respond(
app('role')->add($_POST['role'])
);
break;
case "deleteRole":
onlyAdmin();
app('role')->delete($_POST['roleId']);
break;
case "addUser":
onlyAdmin();
respond(
app('user')->add($_POST)
);
break;
case "updateUser":
onlyAdmin();
app('user')->updateUser($_POST['userId'], $_POST);
break;
case "banUser":
onlyAdmin();
app('user')->updateInfo($_POST['userId'], array('banned' => 'Y'));
break;
case "unbanUser":
onlyAdmin();
app('user')->updateInfo($_POST['userId'], array('banned' => 'N'));
break;
case "getUser":
onlyAdmin();
respond(
app('user')->getAll($_POST['userId'])
);
break;
default:
break;
}
function onlyAdmin()
{
if (! (app('login')->isLoggedIn() && app('current_user')->is_admin)) {
exit();
}
}
答案 0 :(得分:1)
您正在使用type: "POST"
提交ajax,以便在$_POST
变量中的PHP中提供数据。
由于您似乎正在添加评论,因此这是正确的请求动词,因此请在插入方法中更改为"FK_DappID" =>$_POST['ID']
,并确保将ID
添加到{{1}你在你的AJAX中发帖:
您应该创建一个隐藏的表单输入data
,然后从此输入中获取值以发送您的AJAX POST请求。
<input type="hidden" id="post_id" name="post_id" value="<?= $_GET['ID'] ?>" />