简单的Ajax表单提交!?(看不懂)



我将尝试简单地解释我的代码。基本上我得到了 2 个不同的文件:

fun.php
class.fun.php

我想使用 ajax 发布我的表单,所以它不会刷新页面。 在class.fun.php,我为每个帖子都得到了reportForm

<!-- REPORT MODAL -->
<div class="modal fade report_post_<?php echo $post['id'];?>" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<b><center><div class="modal-header">Report Post</div></center></b>     
<form class="horiziontal-form" id="reportForm" action="../Pages/fun.php?action=reportPostForm" method="post">                     
<center><textarea name="report" style="width:80%; height:200px; margin-top:20px; resize:vertical;" placeholder="Please describe your Report!"></textarea></center>
<input type="hidden" name="addedby" class="form-control col-md-7 col-xs-12" value="<?php echo $myRow['id']; ?>" />
<input type="hidden" name="image_id" class="form-control col-md-7 col-xs-12" value="<?php echo $post['id']; ?>" />
<div class="modal-footer"> <input type="submit" name="submit" value="Submit Form" /></div>                                  
</div>               
</div>
</form> 
</div>
<!-- END OF REPORT MODAL -->

在表单之后,我得到了 ajax 函数:

<script> 
$("#reportForm").submit(function(event){
event.preventDefault(); //prevent default action 
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = $(this).serialize(); //Encode form elements for submission
$.ajax({
url : post_url,
type: request_method,
data : form_data
}).done(function(response){ //
$("#server-results").html(response);
});
});
</script>       

当我单击按钮submit我想将表单数据发送到fun.php

这就是我在fun.php中接收数据的方式

if(isset($_POST['reportPostForm'])){
$image_id = strip_tags($_POST['image_id']);
$report = strip_tags($_POST['report']);
$addedby = strip_tags($_POST['addedby']);
$fun->reportPost($image_id,$report,$addedby);       
}   

并将它们发送到class.fun.php中的其他功能

但此时此刻什么也没发生。我一直在循环拖很多 教程,不明白如何使这项工作。我是新手 JavaScript。我有工作赞成/反对的脚本,我只通过post_id并且它有效。

我有有效的赞成票脚本:

$("#upvote_<?php echo $post['id'];?>").click(function(){
$.ajax(
{ url: "fun.php?upvote-btn=true?action=select&image_id=<?php echo $post['id'];?>", 
type: "get",
success: function(result){
$('#upvote_<?php echo $post['id'];?>').load(document.URL +  ' #upvote_<?php echo $post['id'];?>');
$('#downvote_<?php echo $post['id'];?>').load(document.URL +  ' #downvote_<?php echo $post['id'];?>');
$('#comment_<?php echo $post['id'];?>').load(document.URL +  ' #comment_<?php echo $post['id'];?>');
$('#share_<?php echo $post['id'];?>').load(document.URL +  ' #share_<?php echo $post['id'];?>');
$('#report_<?php echo $post['id'];?>').load(document.URL +  ' #report_<?php echo $post['id'];?>');
$('#report_btn_<?php echo $post['id'];?>').load(document.URL +  ' #report_btn_<?php echo $post['id'];?>');
document.getElementById("result-box").innerHTML = result;
}
});

}(;

fun.php您正在检查$_post['reportPostForm']但这不会通过此 ajax 调用的帖子发送。 你的公司没有这个时间,这就是为什么什么都没有发生。试试if(isset($_POST['report'])

更改此设置:

if(isset($_POST['report'])){
$image_id = strip_tags($_POST['image_id']);
$report = strip_tags($_POST['report']);
$addedby = strip_tags($_POST['addedby']);
$fun->reportPost($image_id,$report,$addedby); 
echo "Done";
}  

此外,将<div id='server-results'></div>添加到表单中,以便可以显示资源。

<div class="modal-dialog modal-lg">
<div class="modal-content">
<b>
<center>
<div class="modal-header">Report Post</div>
</center>
</b>
<form class="horiziontal-form" id="reportForm" action="../Pages/fun.php?action=reportPostForm" method="post">
<center>
<textarea name="report" style="width:80%; height:200px; margin-top:20px; resize:vertical;" placeholder="Please describe your Report!"></textarea>
</center>
<input type="hidden" name="addedby" class="form-control col-md-7 col-xs-12" value="<?php echo $myRow['id']; ?>" />
<input type="hidden" name="image_id" class="form-control col-md-7 col-xs-12" value="<?php echo $post['id']; ?>" />
<div class="modal-footer">
<input type="submit" name="submit" value="Submit Form" />
</div>
</div>
</div>
<div id='server-results'></div>
</form>
</div>

目前还不清楚什么不起作用。 文件正确。

唯一的问题是乐趣.php就像你一样:

if(isset($_POST['reportPostForm'])){
$image_id = strip_tags($_POST['image_id']);
$report = strip_tags($_POST['report']);
$addedby = strip_tags($_POST['addedby']);
$fun->reportPost($image_id,$report,$addedby);
} 

但是您发送的是 _POST 美元["添加者"]、_POST 美元["image_id"] 和 _POST 美元["报告"]。

您正在检查的那个 $_POST['报告PostForm'] 不存在。

尝试针对要传递的三个值之一执行 if。

最新更新