因此,我有一个form
,它包含一个textarea
字段和一个submit
按钮:
<form class="popupForPosting">
<textarea id="postContent" name="postContent" rows="8" cols="80" class="postContent" placeholder="What's going on, <?php echo $firstname ?>?"></textarea>
<button id="pos" class="pos" onclick="makePost()">Post</button>
</form>
当我单击submit
按钮时,调用会作为请求发送到AJAX,而我的postContent
(在textarea
字段中输入的内容)将显示在URL中。我不希望这种事发生。
现在,我不想对此表单使用POST
方法。我想使用GET
方法,但仍然隐藏在URL中显示的参数。根据所提供的资料和细节,我怎样才能做到这一点?
编辑:
<script>
const makePost(form) {
const text = form.postContent.value;
function makePost() {
var postContent = $("#postContent").val();
if (postContent.length > 0) {
jQuery.ajax({
url:"yourposts.php",
data:{
postContent: postContent
},
type:"POST",
success:function(data){
if (data == "success") {
$(".textpostFormat").html(postContent);
}
}
});
}
}
};
document.querySelector(".popupForPosting").addEventListener("submit",function(e) {
e.preventDefault();
makePost(this); // passing the form
});
</script>
发布于 2022-01-22 05:59:45
最简单的答案
更改表单标记:action="yourposts.php" method="post"
G 215
现在,如果单击按钮,页面将保存数据,而不是只加载页面。内容测试将处理重新加载。
Ajax答案
使用AJAX不显示URL中的数据-只是
删除onclick="makePost()"
event.preventDefault()
注意,我已包装在一个load事件处理程序中。
$(function() { // when form is available
$(".popupForPosting").on("submit", function(e) {
e.preventDefault(); // always prevent submit
const postContent = $("#postContent").val().trim();
$('#noText').css({visibility: postContent === "" ? "visible" : "hidden" })
$("#popUp").toggle(postContent==="")
if (postContent) {
$.ajax({
url: "yourposts.php",
data: {
postContent: postContent
},
type: "POST",
success: function(data) {
console.log(data)
if (data == "success") {
$(".textpostFormat").html(postContent);
}
}
});
}
});
});
https://stackoverflow.com/questions/70813453
复制相似问题