2012-03-29 47 views
0

我從simplemodal的代碼開始,並進行修改以適應我的需要。這非常好。我最後一個問題是獲取一些單選按鈕來發布。我會跳過我認爲不必要的代碼塊,只顯示我認爲相關的內容。我嘗試了幾十次來自php.net的解決方案嘗試,但似乎沒有任何工作。無法獲得單選按鈕值在PHP中發佈

HTML

<label for='PayPlatform'>Are you willing to pay for a trading platform?</label> 
<input type='radio' name='PayPlatform' value='Yes' tabindex='1001' />Yes 
<input type='radio' name='PayPlatform' value='No' tabindex='1002' />No 

此處,我不能得到的價值,我的嘗試有兩個企圖在下面的代碼塊,它們當然不會試圖在同一時間的。

else if ($action == "send") { 
// other elements form values are retrieved fine, just not the below 
$PayPlatform = isset($_POST['PayPlatform']) ? ' checked="checked"' : ""; 
    //the ternary above just submits 'checked="checked" no matter which radio is checked 
    //another attempt 
    $PayPlatform = isset($_POST["PayPlatform"]); 
    //this just submits "1" weather yes or no is checked 
$token = isset($_POST["token"]) ? $_POST["token"] : ""; 

// make sure the token matches 
if ($token === smcf_token($to)) { 
    smcf_send($name, $email, $subject, $phone, $message, $PayPlatform); 
    echo "Your message was successfully sent, you can close this window"; 
} 
else { 
    echo "Unfortunately, your message could not be verified."; 
} 
} 
+4

的var_dump($ _ POST)是你的朋友 – sdjuan 2012-03-29 20:16:33

+0

@sdjuan - 我做了轉儲$ _ POST,則顯示「是」爲我檢查是每個值,但我可以不要讓它提交是的...見下面 – 2012-03-29 20:20:43

+0

你可以發佈你的var_dump嗎?也許這將有助於解決這個問題。呃,沒關係,我認爲科爾賓是對的。 – Gohn67 2012-03-29 20:25:39

回答

4
$p = (isset($_POST['PayPlatform']) && $_POST['PayPlatform'] == 'Yes'); 

Boolean如果是無線電檢查,否則爲false。

假設你有以下幾點:

$a = 'Yes'; 
isset($a); //true 
$b = 'No'; 
isset($b); //true 

字符串被視爲集,並作爲PsyCoder說,你會收到值,而不是布爾值。

另外,在一個不同的(chastise-y)筆記中,這個問題可以通過一些更好的調試完全避免。我會做的第一件事就是var_dump($ _ POST),看看它的值是什麼。然後你會看到$ _POST ['PayPlatform']總是是或否,你可能已經意識到isset在字符串中總是如此。

+0

確定使用上面如果他們檢查是「2」,如果他們檢查是「2」對我來說是可以接受的,不幸的是,接受這個的人是個白癡,想看到「是」或「否」,你會怎麼做?我想獲得他們選擇的輸入的實際值 – 2012-03-29 20:17:25

+0

然後erm .. ..使用$ _POST ['PayPlatform'] ???(雖然我會檢查以確保它已設置,並且它是有效的選項之一) – Corbin 2012-03-29 20:17:59

+0

@DirtyBirdDesign,您應該得到'是'或'否' m不知道爲什麼你得到1,尤其是2給出了上面的信息 – Gohn67 2012-03-29 20:18:58

1

你不會得到價值勾選或者......而您會收到在HTML中指定的value='No'value='No'參數值...

+0

你的意思是value ='Yes'或value ='No'是吧? – 2012-03-29 20:13:47

+0

是的......我的意思是...... – 2012-03-29 20:15:05

0
if($_POST["PayPlatform"] == "Yes") { 
    $PayPlatform = true; 
} 

保持簡單:)

+0

保持簡單?那麼爲什麼在這個世界上有strip_tags呢?! – Corbin 2012-03-29 20:16:28

+0

編碼習慣,我永遠不會相信用戶提交的內容:)在這種情況下可能不需要,但我總是把它放在例子:) – ArendE 2012-03-29 20:19:30

+1

雖然它是100%毫無意義的。事實上,我會認爲它是錯的。

是的

不應該=='是的'但在你的例子中它會。如果有人篡改我的表單值,我喜歡知道它,不要忽略它:)。 HTML轉義(或刪除)僅在HTML上下文中有意義。當你比較平等時,除非用戶有意提交HTML,否則它不再是'html'。 htmlspecialentities的html被打印是必不可少的,但是這看起來好像你已經採取了那麼一點點。 – Corbin 2012-03-29 20:20:15