jQuery/Ajax表单发布数据不起作用

编程入门 行业动态 更新时间:2024-10-17 04:54:09
本文介绍了jQuery/Ajax表单发布数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不是Jquery的专家.我已经建立了一个4步的html表单,例如

I'm not expert with Jquery. I've built a 4 steps html form like

<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>

,并在.js文件中编写了一些Jquery验证后,我试图通过ajax将数据传递到php文件中.我的formData函数如下所示:

and after writing some Jquery validation in my .js file, I've tried to pass my data to a php file through ajax. My formData function looks like this:

<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>

搜索周围,我试图构建具有以下结构的php文件来接收数据:

Searching around I've tried to build the php file receiving data with this structure:

<?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 } ?>

我已经初始化验证和ajax函数,其操作如下:

I've initialized validation and ajax functions doing something as following:

<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>

问题是,当我单击提交"时,我被重定向到URL为mypath的页面? ALL_MY_DATA_SERIALISED. 我的错误在哪里?由于我的无知,我看不到它.是在jquery/ajax函数中,在php文件中还是在我的html中? 预先感谢您的帮助.

The problem is that when I click on submit I got redirected to a page where the url is mypath? 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? Thank you in advance for your help.

推荐答案

您只需要在事件监听器的顶部执行event.preventDefault():

You just need to do event.preventDefault() in the top of your event listener:

$('#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"); } } })

更多推荐

jQuery/Ajax表单发布数据不起作用

本文发布于:2023-11-26 20:42:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   不起作用   数据   jQuery   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!