2013-01-04 44 views
1

有誰知道爲什麼選中的複選框的值未被回顯?單選按鈕不通過AJAX傳遞值

Javascript代碼:

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

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

這是呼應:

$ _GET:陣列()選擇:

任何幫助表示讚賞

丹尼爾

附:這是我得到JavaScript代碼的地方http://malsup.com/jquery/form/

我需要任何額外的JavaScript代碼來使它工作嗎?

回答

0

看來你在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可能看起來像您的聯繫得到了一個。除了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> 

至於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); 

希望有所幫助。