[英]Jquery/Ajax form Post data is not working
I'm not expert with Jquery.我不是 Jquery 的专家。 I've built a 4 steps html form like
我已经构建了一个 4 步的 html 表单,例如
<form id="msform" enctype="multipart/form-data">
<fieldset id="publish1" data-check-id="1">
//some inputs
</fieldset>
<fieldset id="publish2" data-check-id="2">
//some inputs
</fieldset>
<fieldset id="publish3" data-check-id="3">
//some inputs
</fieldset>
<fieldset id="publish4" data-check-id="4">
<input type="submit" class="submit action-button pull-right top-35" value="Publish"/>
</fieldset>
</form>
and after writing some Jquery validation in my .js file, I've tried to pass my data to a php file through ajax.在我的 .js 文件中编写了一些 Jquery 验证之后,我尝试通过 ajax 将我的数据传递给一个 php 文件。 My
formData
function looks like this:我的
formData
函数如下所示:
<script>
function formData() {
var serializedValues = jQuery("#msform").serialize();
var form_data = {
action: 'ajax_data',
type: 'post',
data: serializedValues,
};
jQuery.post('mypath/insert.php', form_data); //where data should be sent
return true;
}
</script>
Searching around I've tried to build the php file receiving data with this structure:四处搜索,我尝试使用这种结构构建接收数据的 php 文件:
<?php
if (isset($_POST['data'])) {
post_things();
return true;
}
function post_things() {
$title = trim($_POST['form_title']);
// where form_title is the input[name] of what I want get, serialised into jquery serializedValues variable
//other similar inputs
//do something with $title and other $variables
}
?>
I've initialized validation and ajax functions doing something as following:我已经初始化了验证和 ajax 函数,如下所示:
<script>
$(document).ready(function () {
msform_init(); //this validate form step by step (it's working!)
$('#msform').submit(function (event) {
if (form_completeCheck() && true) { //This check if something empty
formData();
if (formData() && true) {
window.location.replace("//some redirection to success");
} else {
window.location.replace("//some redirection to failure");
}
} else {
event.preventDefault();
}
})
})
</script>
The problem is that when I click on submit I got redirected to a page where the url is mypath?问题是当我点击提交时,我被重定向到一个 url 是 mypath 的页面? ALL_MY_DATA_SERIALISED.
ALL_MY_DATA_SERIALISED。 Where is my error?
我的错误在哪里? I can't see it due to my ignorance.
由于我的无知,我看不到它。 Is in the jquery/ajax functions, in the php file or in my html?
是在 jquery/ajax 函数中,在 php 文件中还是在我的 html 中? Thank you in advance for your help.
预先感谢您的帮助。
HTML Script HTML脚本
<form id="msform" enctype="multipart/form-data">
<input type="hidden" name="action" value="do_action"/>
<fieldset id="publish1" data-check-id="1">
//some inputs
</fieldset>
<fieldset id="publish2" data-check-id="2">
//some inputs
</fieldset>
<fieldset id="publish3" data-check-id="3">
//some inputs
</fieldset>
<fieldset id="publish4" data-check-id="4">
<input type="button" id="do_action" class="submit action-button pull-right top-35" value="Publish"/>
</fieldset>
</form>
JavaScript JavaScript的
$("#do_action").click(function(){
$.ajax({
type:'POST',
data:$("#msform").serialize();
url:'<<php script url>>',
success:function(data){
alert(data);
}
});
});
php script PHP脚本
<?php
if(isset($_POST['action'])){
post_things($_POST);
return true;
}
function post_things($request){
echo "<pre>";
print_r($request);
echo "</pre>";
}
?>
You just need to do event.preventDefault()
in the top of your event listener: 您只需要在事件监听器的顶部执行
event.preventDefault()
:
$('#msform').submit(function(event){
event.preventDefault();
if(form_completeCheck() && true){ //This check if something empty
formData();
if(formData() && true){
window.location.replace("//some redirection to success");
} else {
window.location.replace("//some redirection to failure");
}
}
})
The reason you get redirected is because you are submitting the form. 重定向的原因是因为您正在提交表单。
Since in your <form id="msform" enctype="multipart/form-data">
you define no action it is submitted to itself. 由于在您的
<form id="msform" enctype="multipart/form-data">
您没有定义将其提交给自身的操作。
You must prevent form from submitting using preventDefault(). 您必须使用preventDefault()阻止表单提交。
$('#msform').submit(function(event){
event.preventDefault(); //Add this line
..............
如果您的输入字段中没有name属性,则serialize将返回空值,因此请不要忘记添加Name属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.