<form action="" method='POST'>
<input type='submit' name='voted' value='Like'>
<input type='submit' name='votes' value='Dislike'>
</form>
<?php
$connect = mysqli_connect ("localhost", "root", "muarem1") or die ("Couldn not connect!");
mysqli_select_db ($connect, "likesystem") or die ("Couldn't find db");
$result = mysqli_query($connect,"SELECT * FROM fyci WHERE id=1");
while ($row = mysqli_fetch_assoc($result)) {
$voted = $row["voted"];
}
if (isset($_POST['voted'])){
$votes = $voted+1;
$update = mysqli_query($connect,"UPDATE fyci SET voted='$votes' WHERE id=1");
}else
if (isset($_POST['votes'])){
$dislike = $voted-1;
$update = mysqli_query($connect,"UPDATE fyci SET voted='$dislike' WHERE id=1");
}
echo "Likes: $voted";
?>
提交表格并保持同一页面? 谢谢!
如果点击提交,我想留在同一页面上。
应该怎么做?
答案 0 :(得分:0)
如果您想留在页面上,请使用AJAX通话。有关示例,请参阅jQuery.post()。
答案 1 :(得分:0)
您可以使用$ .post函数作为jquery
的选项提供程序之一像
$.post('name_of_your_file.php', $('form').serialize());
或
$.post('<?php echo $_SERVER["PHP_SELF"];?>', $('form').serialize());
答案 2 :(得分:0)
使用AJAX你可以做到。我修改了一些HTML代码。添加了表单名称。
<强> HTML:强>
<form name="form1" action="vote.php" method='POST'>
<input type='submit' name='voted' value='Like'>
<input type='submit' name='votes' value='Dislike'>
</form>
<强> JS:强>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
var selector = $('form[name=form1]');
selector.submit(function(e){
e.preventDefault();
$.ajax({
url : selector.attr('action'),
data: selector.serialize(),
type: selector.attr('method'),
success: function(data){
//alert(data);
}
});
});
</script>
<强> vote.php:强>
<?php
$connect = mysqli_connect ("localhost", "root", "muarem1") or die ("Couldn not connect!");
mysqli_select_db ($connect, "likesystem") or die ("Couldn't find db");
$result = mysqli_query($connect,"SELECT * FROM fyci WHERE id=1");
while ($row = mysqli_fetch_assoc($result)) {
$voted = $row["voted"];
}
if (isset($_POST['voted'])){
$votes = $voted+1;
$update = mysqli_query($connect,"UPDATE fyci SET voted='$votes' WHERE id=1");
}else
if (isset($_POST['votes'])){
$dislike = $voted-1;
$update = mysqli_query($connect,"UPDATE fyci SET voted='$dislike' WHERE id=1");
}
echo "Likes: $voted";