本文介绍了Ajax调用成功,但无法从$ _POST获取广播值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的网站完全是异步的,大多数html在按钮上被创建和销毁,并且它们中的每一个都阻止导航。 在这一部分中,我生成一个带有1到10数字框数组的表单,使用jQuery.ajax()发布它并发送它以处理它的位置回声(现在)或回声没有选择任何东西。 这是表格, <?php < form id =surveyFormaction =processSurvey.phpmethod =post> < h3> Alimentos< / h3> < h4> Sabor< / h4> < div class =form-group>'; for $($ i = 0; $ i echo'< span class =lead form-options>'。'< /跨度> < label class =radio-inline> < input type =radioname =saborid =saborRadio'。$ i。'value ='。$ i。'>'。 $ i。'< / label>'; } echo'< / div> < div class =form-group> < button class =btn btn-default surveyForm-btntype =submit> Enviar< / button> < / div> < / form> ?> 这是javascript: console.log(点击.surveyForm-btn); $ b($ body')。 $ b var data = $('#surveyForm')。serialize(); console.log(data); $ .ajax({ method:POST, url:processSurvey.php, data:data, success:function(result){ console.log(Ajax调用processSurvey成功); $ (#surveyForm)。clearForm(); console.log(result); console.log(data); }); 返回false; }); 这是php的过程:<?php if(isset($ _ POST ['sabor']))//如果检查了任何选项 echo $ _POST ['sabor ]; // echo the choice else echo没有选择任何东西。 print_r($ _ POST); ?> 这是控制台点击提交所选择的radiobox后: 点击#surveyForm [EMPTY LINE] Ajax调用processSurvey成功没有被选中。 [EMPTY LINE] 这表示提交成功,但表单数据为空。我一直在试图从昨天开始就发现问题,我很确定我传递的数据是错误的,但在Google中没有找到任何我没有尝试过的数据。 编辑:添加了大多数的sugestions,问题依然存在。也许html结构是错误的?编辑2:我发现了一些非常奇怪的东西,最终的代码似乎有一个额外的结束标记,像这样 < form id =surveyFormaction =processSurvey.phpmethod =post>< /形式> < h3> Alimentos< / h3> < h4> Sabor< / h4> 我不知道这是从哪里来的,但肯定是问题所在。解决方案这里有很多笔记 1-您会对 form id ='surveyForm'和 button class ='surveyForm'所以最好将它改为一点按钮类='surveyForm_btn' 2-我认为你应该序列化表单而不是按钮 var data = $('#surveyForm')。serialize(); //不.surveyForm 3- ID必须是唯一的 4- $(#surveyForm)。clearForm(); //不.surveyForm 终于查看所有评论 使用 $('body')。on('submit','#surveyForm',function(){}); 编辑答案: 1-请在每一步后检查所有内容 < form id =surveyFormaction =processSurvey.phpmethod =post> < h3> Alimentos< / h3> < h4> Sabor< / h4> < div class =form-group> < button class =btn btn-default surveyForm-btntype =submit> Enviar< / button> < / div> < / form> in js var data = $(this).serialize(); $ .ajax ({ method:POST, url:processSurvey.php, data:data, success:function(result){ console.log (result); } }); return false; }); <?php echo'连接成功'; ?> 这段代码会输出在控制台中成功连接..如果这项工作添加for循环并进行检查再次 My site is fully asynchronus, most of the html gets created and destroyed on button presses and every one of them prevents navigation. At this part I produce a form with a "rate 1 to 10" array of radioboxes, post it using jQuery.ajax() and send it to process where it's either echoed back (for now) or echo "nothing was selected.".This is the form, <?php<form id="surveyForm" action="processSurvey.php" method="post"> <h3>Alimentos</h3> <h4>Sabor</h4> <div class="form-group">'; for ($i = 0; $i <= 10; $i++) { echo ' <span class="lead form-options">' .'</span> <label class="radio-inline"> <input type="radio" name="sabor" id="saborRadio'. $i .'" value="'. $i .'">'. $i.' </label>'; } echo ' </div> <div class="form-group"> <button class="btn btn-default surveyForm-btn" type="submit">Enviar</button> </div></form>?>This is the javascript:$('body').on('click', '.surveyForm', function(){ console.log("Clicked on .surveyForm-btn"); var data = $('#surveyForm').serialize(); console.log( data ); $.ajax({ method: "POST", url: "processSurvey.php", data: data, success: function(result){ console.log("Ajax call to processSurvey success"); $("#surveyForm").clearForm(); console.log(result); console.log( data ); } }); return false; });And this is the process php:<?phpif (isset($_POST['sabor'])) // if ANY of the options was checked echo $_POST['sabor']; // echo the choiceelse echo "nothing was selected.";print_r($_POST);?>This is the console after clicking submit WITH a selected radiobox:Clicked on #surveyForm[EMPTY LINE]Ajax call to processSurvey successnothing was selected.[EMPTY LINE]This means the submit is successful, but the form data is empty. I've been trying to find the problem since yesterday, I'm pretty sure I'm passing the data wrong but can't find anything in google that I haven't tried.EDIT: Added most sugestions, problem persists. Maybe the html structure is wrong? The form and the submit don't seem to be connected.EDIT 2: I found something very strange, on the final code there seems to be an extra closing tag, like this <form id="surveyForm" action="processSurvey.php" method="post"></form> <h3>Alimentos</h3> <h4>Sabor</h4>I have no idea where is that coming from, but is defenitely the problem. 解决方案 there are a lot of notes here1- you will get confused with form id='surveyForm' and button class='surveyForm' so its better to change it a little bit to button class='surveyForm_btn'2- I think you should serialize the form not the button var data = $('#surveyForm').serialize(); // not .surveyForm3- IDs must be unique4- $("#surveyForm").clearForm(); // not .surveyFormfinally check all comments and Its better to use$('body').on('submit', '#surveyForm', function(){});Edited answer:1- please check everything after each step<form id="surveyForm" action="processSurvey.php" method="post"> <h3>Alimentos</h3> <h4>Sabor</h4> <div class="form-group"> <button class="btn btn-default surveyForm-btn" type="submit">Enviar</button> </div></form>in js$('body').on('submit', '#surveyForm', function(){ var data = $(this).serialize(); $.ajax({ method: "POST", url: "processSurvey.php", data: data, success: function(result){ console.log(result); } }); return false; });in php<?phpecho 'Connected successfully';?>this code will output Connected successfully in console .. if this work add your for loop and make a check again 这篇关于Ajax调用成功,但无法从$ _POST获取广播值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 05:21