【发布时间】:2017-07-03 09:38:09
【问题描述】:
我在 ajax 函数中传输 php 数据时遇到问题。 我想要的是发送一个 html 表单,其中我有来自 mysqli_fetch_array 的 php 值。该值是帖子的编号,这是我的主键。 作为javascript的新手,我真的不知道该怎么做。 这是我的 onclick 函数代码:
<script>
function foo () {
$.ajax({
url:"vote.php?no_post=<?php $post ['no_post'] ?>", //what I'd like to do
type: "POST", //request type
success:function(result)
{
alert(result);
}
});
}
</script>
在 php 页面中,我有这个 fetch 数组,这里我们关注名为“up”的按钮:
if(!isset($_POST['more']))
{
while($post = mysqli_fetch_array($res))
{
echo
'<p>'.
'<center>'.
'<img src="image/'.$post['image_post'].'">'.
'</center>'.
'<br/>'.
'<label>'.$post['desc_post'].'</label>'.
'<p>'.
'<a id="darkBlue" href="account.php?no_post='.$post['no_post'].'">'.'@'.$post['login'].'</a>'.
'<p>'.
'<label>'.$post['time'].'</label>'.
'<p>'.
'<label>'.$post['vote_post'].' points'.'</label>'.
'<footer>';
if (isset($_SESSION['login']))
{
echo '<button class="btn btn-success" name="up" id="up" onclick="foo()">+</button>'.
' '.
'<button class="btn btn-danger" name="down" id="down">-</button>'.
' '.
'<a href="commentaire.php?no_post='.$post['no_post'].'" class="btn btn-warning">Comment</a>'.
'<hr>';
}
编辑***
最后是执行 sql 请求的我的 php 页面:
<?php
if (isset($_POST['up'])) // 2 buttons on the first html FORM
{
$req="UPDATE post SET vote_post=vote_post+1
WHERE no_post=".$_GET['no_post'].""; //picking up the value from my url
$res=mysqli_query($con,$req);
}
else if (isset($_POST['down']))
{
$req2="UPDATE post SET vote_post=vote_post-1
WHERE no_post=".$_GET['no_post'].""; //picking up the value from my URL
$res2=mysqli_query($con,$req2);
}
else
echo 'Error';
?>
感谢您的帮助。
【问题讨论】:
-
将
echo放入url:"vote.php?no_post=<?php echo $post['no_post'] ?>", //我想做的事 -
如果您不在正文中发送任何数据,为什么还要使用 POST?
-
如果您要发送这样的数据:url:"vote.php?no_post=",这是您使用 GET post 发送数据的方式,它在 URL 中可见.. 为什么你使用方法作为 POST?首先决定你真正想要使用的方法。
-
我用 POST 发送的是 ,它是我数据库中的项目编号,它可以让我识别我想要的帖子被赞成。我只使用 PHP 来设置我的投票系统与
-
这个问题的状态如何? @Julien 下面的答案解决了吗?如果是这样,则应通过接受将其中一个标记为正确的。