单选按钮不通过AJAX传递值

时间:2022-09-25 16:00:13

Does anyone know why the value of the selected checkbox is not being echoed?

有谁知道为什么没有回显所选复选框的值?

Javascript code:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
<!--
// wait for the DOM to be loaded 
$(document).ready(function()
{
    // bind 'vgsForm' and provide a simple callback function 
    $('#vgsForm').ajaxForm(function()
    {
        $('#Suggestion').load('process_answers.php');
    }); 
});

HTML Form

<div id="Questions">
<form id="vgsForm" action="process_answers.php" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less than 16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16 or more" />16 or more</label>
</div>

process_answers.php

echo('$_GET: '.print_r($_GET, true));

//Get Question 1
if (isset($_GET['q1option'])) 
{
    $q1option = $_GET['q1option'];
} 
else 
{
    $q1option = NULL;
}

echo("Selected: ".$q1option);

This is echoed:

这是回应:

$_GET: Array ( ) Selected:

$ _GET:Array()选中:

Any help is appreciated

任何帮助表示赞赏

Daniel

P.S. This is where I got the JavaScript code http://malsup.com/jquery/form/

附:这是我获得JavaScript代码的地方http://malsup.com/jquery/form/

Do I need any additional JavaScript code to make it work?

我是否需要任何其他JavaScript代码才能使其正常工作?

1 个解决方案

#1


0  

It seems you have problems in both HTML and inline JS code. Try the code below for your form:

您似乎在HTML和内联JS代码中都存在问题。请尝试以下代码填写表单:

<div id="Questions">
<form id="vgsForm" action="process_answers.php" method="POST" >
   <label><input type="radio" name="q1option" value="Less than 16" />Less than 16</label><br />
   <label><input type="radio" name="q1option" value="16 or more" />16 or more</label>
   <input type="submit" value="Submit" />
</form>
</div>

HTML may look like the one you got from the link. Besides jquery, you have to include the script from http://malsup.github.com/jquery.form.js, which implements the form ajax upgrade.

HTML可能看起来像是从链接中获得的HTML。除了jquery,你还必须包含来自http://malsup.github.com/jquery.form.js的脚本,它实现了ajax升级的形式。

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'vgsForm' in order to upgrade form to use ajax 
            $('#vgsForm').ajaxForm(function() { 
                alert("Form was sent successfully."); 
            }); 
        }); 
    </script> 
</head> 

As for process_answers.php, make sure you are reading from the POST global array, since we've set it as the method in the form:

至于process_answers.php,请确保您正在从POST全局数组中读取,因为我们已将其设置为表单中的方法:

echo('$_POST: '.print_r($_POST, true));    
//Get Question 1
if (isset($_POST['q1option'])) 
{
    $q1option = $_POST['q1option'];
} 
else 
{
    $q1option = NULL;
}

echo("Selected: ".$q1option);

Hope that helps.

希望有所帮助。

#1


0  

It seems you have problems in both HTML and inline JS code. Try the code below for your form:

您似乎在HTML和内联JS代码中都存在问题。请尝试以下代码填写表单:

<div id="Questions">
<form id="vgsForm" action="process_answers.php" method="POST" >
   <label><input type="radio" name="q1option" value="Less than 16" />Less than 16</label><br />
   <label><input type="radio" name="q1option" value="16 or more" />16 or more</label>
   <input type="submit" value="Submit" />
</form>
</div>

HTML may look like the one you got from the link. Besides jquery, you have to include the script from http://malsup.github.com/jquery.form.js, which implements the form ajax upgrade.

HTML可能看起来像是从链接中获得的HTML。除了jquery,你还必须包含来自http://malsup.github.com/jquery.form.js的脚本,它实现了ajax升级的形式。

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'vgsForm' in order to upgrade form to use ajax 
            $('#vgsForm').ajaxForm(function() { 
                alert("Form was sent successfully."); 
            }); 
        }); 
    </script> 
</head> 

As for process_answers.php, make sure you are reading from the POST global array, since we've set it as the method in the form:

至于process_answers.php,请确保您正在从POST全局数组中读取,因为我们已将其设置为表单中的方法:

echo('$_POST: '.print_r($_POST, true));    
//Get Question 1
if (isset($_POST['q1option'])) 
{
    $q1option = $_POST['q1option'];
} 
else 
{
    $q1option = NULL;
}

echo("Selected: ".$q1option);

Hope that helps.

希望有所帮助。