2016-07-08 62 views
0

我想做一個HTML/PHP電子郵件的形式,其中包括多個文本字段,4個切換功能的按鈕,並提交按鈕。點擊後,我已經成功地獲得按鈕來改變狀態,但我無法弄清楚如何檢查按鈕是否實際上是在PHP中按下的。我如何使切換功能的HTML按鈕張貼到PHP

這是我所有按鈕的html代碼。它們被包裹在一個包含提交按鈕和其他文本字段的表單中。我也試着改變按鈕標籤來輸入標籤。

<section> 
    <button class="btn service-button" name="app" value="Web App" type="button">Web App</button> 
    <button class="btn service-button" name="brand" value="Branding" type="button">Branding</button> 
    <button class="btn service-button" name="design" value="Web Design" type="button">Web Design</button> 
    <button class="btn service-button" name="other" value="Other" type="button">Other</button> 
</section> 

我只是按鈕的PHP:

if(isset($_POST['app'])) { 
    $service .= $_POST['app']; 
} 
if(isset($_POST['brand'])) { 
    $service .= ", " . $_POST['brand']; 
} 
if(isset($_POST['design'])) { 
    $service .= ", " . $_POST['design']; 
} 
if(isset($_POST['other'])) { 
    $service .= ", " . $_POST['other']; 
} 

我嘗試了vardump($ _ POST);但他們顯然甚至沒有添加到$ _POST。 任何想法我可以嘗試讓他們實際提交數據到$ _POST?我想這樣做是這樣的:

if(class_exists('active-button')) { 
    $service .= //Set data here somehow. 
} 

,但我不能肯定我會怎麼做,既然有四個不同的按鈕和它們的任意組合可以選擇。

而且,應該不會影響任何東西,我不覺得,但這裏是我的jQuery改變的按鈕:

$(".btn").click(function() { 
    //Add/remove active-button and service-button. Replaces one with the 
    //other depending on the current state. 
    $(this).toggleClass("active-button").toggleClass("service-button"); 
}); 

主動式按鍵和服務按鍵類的CSS只是使用將按鈕的樣式更改爲

+1

爲什麼不使用單選按鈕來完成這項工作? – Iceman

+0

不知道如果也許我只是誤導,但我想我的按鈕樣式的某種方式,我一直無法用單選按鈕做到這一點。這是他們目前的樣子: http://puu.sh/pUgU3/05c17e88cc.png http://puu.sh/pUgUI/6df75bebf2.png –

+0

你可以像你想要的樣式的單選按鈕,但一個javascript基於解決方案也是可能的。 – Iceman

回答

1

添加隱藏輸入,並且每當您單擊按鈕時更改隱藏值並提交隱藏字段。

HTML

<section> 
<input class="selected_button" name="selected_button" value="" type="hidden"> 
    <button class="btn service-button" name="app" value="Web App" type="button">Web App</button> 
    <button class="btn service-button" name="brand" value="Branding" type="button">Branding</button> 
    <button class="btn service-button" name="design" value="Web Design" type="button">Web Design</button> 
    <button class="btn service-button" name="other" value="Other" type="button">Other</button> 
</section> 

jQuery的

//On button click 
$(".btn").click(function() { 
    //The string to add to the selected-button value 
    var toAdd = $(this).attr("value"); 
    //Add/remove active-button and service-button. Replaces one with the 
    //other depending on the current state. 
    $(this).toggleClass("active-button").toggleClass("service-button"); 
    //Checks if the button has already been added in case the user deselects the button 
    if($(".selected_button").val().indexOf(toAdd) >= 0) { 
     //Removes the comma and text 
     $(".selected_button").val($(".selected_button").val().replace((', ' + toAdd), '')); 
    //If the button is being added for the first time 
    } else { 
     //Adds the comma and text 
     $(".selected_button").val($(".selected_button").val() + ", " + toAdd); 
    } 
}); 

PHP

<?php 
$selected = $_POST['selected_button']; 
if(isset($_POST['selected_button'])) { 
    $service .= $selected; 
} 
?> 

希望這將有助於

或 注:

you use radio buttons by using css style you can display like button 
+0

這正是我所期待的。非常感謝你。如果再次點擊該按鈕,我做了一些更改以使其刪除屬性值。我會暫時編輯你的文章給別人,與我有同樣的問題。 –