2010-11-18 29 views
1

我有一個相當基本的表單,它爲單個單選按鈕設置了輸入字段。表單字段上的單選按鈕始終提交爲ON/TRUE

有一個php腳本處理表單並將值插入到mysql數據庫中。出於某種原因,每當我填寫表格時,無論我是否選擇了收音機,結果都始終爲ON/TRUE。

這裏是我的輸入字段:

<input type="radio" name="news" id="news" class="radio" value="yes" /> 

這裏是應該處理的PHP代碼的一部分:

if(isset($_POST['news'])) 
{ 
$news = "Yes"; 
} 
else 
{ 
$news = "No"; 
} 

有人能告訴我是什麼興田問題從代碼或我需要發佈更多嗎?

這裏是完整形式的代碼:

<form method="POST" id="ipad" name="ipad" action="list.php"> 
<input type="text" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" value="First Name" id="fname" name="fname" class="inputtext"> 


<input type="text" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" value="Last Name" id="lname" name="lname" class="inputtext"> 

<input type="text" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" value="Email" id="email" name="email" class="inputtext"> 

<input type="text" onfocus="if (this.value == 'Code') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Code';}" value="Code" id="code" name="code" class="inputtext"> 


<input type="radio" value="" class="radio" id="news" name="news"> 
<div class="radiotext">sign up?</div> 

    </form> 

注意,單選按鈕輸入沒有JavaScript的施加到其上。我嘗試用YES或NO填充value =「」或將其留空。

回答

1

您需要測試單選按鈕的。該變量將始終存在,因此isset()必定始終返回true。

if(!empty($_POST['news']) and ($_POST['news'] == 'yes')) 
+0

抱歉沒有工作。無論如何,答案始終都是相同的。 – gteh 2010-11-23 15:33:12

+0

編寫腳本的人說我的頁面上有javascript,它始終將單選按鈕設置爲ON,但事實並非如此。我那裏唯一的JavaScript是jquery文件。這讓我瘋狂 – gteh 2010-11-23 15:34:13

+0

@gteh你能展示完整表單的HTML代碼嗎? – 2010-11-23 15:36:38

1
if (isset($_POST['news']) && $_POST['news'] == 'yes'){ 
    $news = "Yes"; 
} 
else 
{ 
    $news = "No"; 
} 
+0

測試,但似乎沒有工作..有同樣的問題。只有這一次答案總是NO而不是YES。 – gteh 2010-11-23 15:30:38

+0

1.在提交之後和之前打印$ _POST ['news']的值(if(isset($ _ POST ['news'])&& $ _POST ['news'] =='yes'))condition)。所以我們來了解$ _POST ['news']的價值。 2如果你正在嘗試使用news =''這個條件,那麼它會給你結果「否」 – 2010-11-24 05:35:22

0

有關jQuery代碼,javascript中並不需要在裏面的HTML工作。如果我是你,我會通過我的代碼搜索.attr(「checked」,「checked」)或.prop(「checked」);