我将ajaxForm更改为ajaxSubmit并使用了e.preventDefault();
我正在尝试建立一个社交网络,但我一直陷于这个问题。 当我按下Enter键时,在执行ajaxForm之前,将调用processing.php并刷新页面,然后重新加载页面,而不是将新的div添加到页面中。
$(document).ready(function() {
$(".imagem_news").click(function(){
var id = this.id;
var valor = this.id.match(/\d+/);
rpbox(id, valor);
});
});
function rpbox(value, valor) {
$("#"+value).keydown(function(evt) {
var message = $("#"+value).val();
if (evt.keyCode == 13 && !evt.shiftKey) {
if (message != ''){
if (document.getElementById("reply_box"+valor).style.display == "none") {
document.getElementById("reply_box"+valor).style.display = "table";
document.getElementById("l"+valor).style.display = "none";
}
$("#myForm"+valor).trigger("submit", valor);
$("#myForm"+valor).resetForm();
}
evt.preventDefault();
}
});
}
$("form").submit(function(e, i){
var form = $(this);
//console.log(form);
$("#myForm"+i).ajaxSubmit({
url: "processing.php",
type: "POST",
success: function(result){
$("#reply_box"+i).append(result);
alert("oi");
},
error: function() {
alert("Ocorreu um erro ao carregar os dados.");
},
});
e.preventDefault();
});
<?php
include("config.php");
if ($_POST['editor1'] != ''):
$uid = $_SESSION['userId'];
$postId = $_POST["formValue"];
if((isset($_POST["editor1"])) ? $_POST["editor1"] : ''){
$posting = (isset($_POST["editor1"])) ? $_POST["editor1"] : '';
$date = date("Y-m-d H:i:s");
$sql = "INSERT INTO comments (id_c, post_id, author_id , text, date) values ('', '$postId', '$uid', '$posting', '$date')";
mysqli_query($conn,$sql);
$proc = mysqli_insert_id($conn);
$query4 = mysqli_query($conn,"SELECT * FROM `profile` WHERE `id` = '$uid'") or die(mysqli_error($conn));
$user = mysqli_fetch_array($query4);
echo '<div id="comm'.$proc.'" class="comment_box">
<span>'.$posting.'</span><div class="cimg_box"><img class="comment_img" src="css/images/profile_photo/'.$user['photo'].'"/></div>
</div>';
};
else:
echo 'Error!';
endif;
?>
我应该怎么做才能使此代码起作用?而且,如果可能的话,我想知道如何向大家展示最新评论。谢谢