简体   繁体   English

Ajax调用成功,但是无法从$ _POST获取单选值

[英]Ajax call successful, but can't get radio values from $_POST

My site is fully asynchronus, most of the html gets created and destroyed on button presses and every one of them prevents navigation. 我的网站是完全异步的,大多数HTML都是在按钮按下时创建和销毁的,并且每一个都阻止导航。

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.". 在这一部分,我生成一个带有“比率1到10”单选框数组的表单,使用jQuery.ajax()发布该表单,然后将其发送到回显(暂时)或回显“未选择任何内容”的过程。

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: 这是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: 这是PHP的过程:

<?php
if (isset($_POST['sabor']))   // if ANY of the options was checked
  echo $_POST['sabor'];    // echo the choice
else
  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 success
nothing 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. 自昨天以来,我一直在尝试查找问题,我很确定自己传递的数据有误,但是在Google中找不到我没有尝试过的任何内容。

EDIT: Added most sugestions, problem persists. 编辑:添加了大多数建议,问题仍然存在。 Maybe the html structure is wrong? 也许html结构是错误的? 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 编辑2:我发现很奇怪,在最终代码上似乎有一个额外的结束标记,像这样

<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 here 这里有很多笔记

1- 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' 1-您将对form id='surveyForm'button class='surveyForm'感到困惑,因此最好将其更改为button class ='surveyForm_btn'

2- I think you should serialize the form not the button 2-我认为您应该序列化表格而不是按钮

var data = $('#surveyForm').serialize(); // not .surveyForm

3- IDs must be unique 3- ID必须是唯一的

4- $("#surveyForm").clearForm(); // not .surveyForm 4- $("#surveyForm").clearForm(); // not .surveyForm $("#surveyForm").clearForm(); // not .surveyForm

finally check all comments 最后检查所有评论

and Its better to use 而且更好用

$('body').on('submit', '#surveyForm', function(){});

Edited answer: 1- please check everything after each step 编辑答案:1-请在每一步之后检查所有内容

<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 在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 在PHP中

<?php
echo 'Connected successfully';
?>

this code will output Connected successfully in console .. if this work add your for loop and make a check again 此代码将在控制台中成功输出Connected ..如果此工作添加了您的for循环并再次进行检查

Try to write your htm like that : 尝试这样写你的htm:

    <h3>Alimentos</h3>

    <h4>Sabor</h4>
    <div class="form-group">
<?php
        for ($i = 0; $i <= 10; $i++) {
         ?>
                <span class="lead form-options"></span>
                <label class="radio-inline">
                    <input type="radio" name="sabor" id="saborRadio<?=$i ?>" value="<?= $i ?>" /><?= $i?>
                </label>
<?php
        } ?>
    </div>

    <div class="form-group">
        <button class="btn btn-default surveyForm-btn" type="submit">Enviar</button>
    </div>

</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM