2013-10-07 98 views
2

我是全新的PHP,實際上我這樣做的原因是自定義wordpress插件,以便它可以滿足我的需要。到目前爲止,我有一個默認表單,我現在要做的就是添加一個國家/地區下拉菜單。以下是我添加它從PHP中的下拉列表中獲取選定的文本

<div class="control-group"> 
    <label class="control-label" for="Country">Country :</label> 
    <div class="controls"> 
     <select id="itemType_id" name="cscf[country]" class="input-xlarge"> 
      <option value="[email protected]">Malaysia</option> 
      <option value="[email protected]">Indonesia</option> 
     </select> 
     <span class="help-inline"></span> 
    </div> 
</div> 

到目前爲止,我只能夠檢索所選的項目與

$cscf['country']; 

我怎樣才能顯示文本是在這種情況下,國家名稱的價值?

+0

如果你想退休的價值在其他網頁上,那麼你將需要的形式,所以你可以使用GET/POST方法從窗體發送值到另一個頁面.. – Ashish

+0

你可以處理它通過使用g如果其他?雖然不是最好的方法.. – zxc

回答

8

您可以使用隱藏字段,並使用JavaScript和jQuery,你可以將該值設置爲這個領域,當選擇的值您的下拉更改。

<select id="itemType_id" name="cscf[country]" class="input-xlarge"> 
    <option value="[email protected]">Malaysia</option> 
    <option value="[email protected]">Indonesia</option> 
</select> 
<input type="hidden" name="country" id="country_hidden"> 

<script> 
    $(document).ready(function() { 
    $("#itemType_id").change(function(){ 
     $("#country_hidden").val(("#itemType_id").find(":selected").text()); 
    }); 
    }); 
</script> 

那麼當你的提交頁面,則可以通過使用

+2

我認爲這是迄今爲止最簡單的方法。 – abiieez

1

嘗試像,

<?php 
    if(isset($_POST['save'])) 
    { 
     //Let $_POST['cscf']['country']= [email protected] 
     // you can explode by @ and get the 0 index name of country 
     $cnt=explode('@',$_POST['cscf']['country']); 
     if(isset($cnt[0]))// check if name exists in email 
     echo ucfirst($cnt[0]);// will echo Malaysia 
    } 
?> 

<form method="post"> 
    <div class="controls"> 
     <select id="itemType_id" name="cscf[country]" class="input-xlarge"> 
      <option value="[email protected]">Malaysia</option> 
      <option value="[email protected]">Indonesia</option> 
     </select> 
     <span class="help-inline"></span> 
    </div> 
    <div class="controls"> 
     <input type="submit" name='save' value="Save"/> 
     <span class="help-inline"></span> 
    </div> 
</form> 
+1

你仍然無法獲得顯示的文本,只有值。您可以將文本添加到像[email protected] |馬來西亞然後列表($ value,$ text)= explode('|',$ _POST ['cscf'] ['country']) ; echo $ text;' – Casey

+0

不需要$ $ cnt = explode('@',$ _ POST ['cscf'] ['country']);' – Casey

+0

@Casey,謝謝。如果我遵循你的建議 – abiieez

1

試試這個

<?php 
    if(isset($_POST['save'])) 
    { 
     $arrayemail = $_POST['cscf']; 
     $mail = $arrayemail['country']; 
     $explode=explode('@',$mail); 
     // print_r($explode); 
     if(isset($explode[0])) 
     echo ucfirst($explode[0]);  

    } 
?> 

<form method="post"> 
    <div class="controls"> 
     <select id="itemType_id" name="cscf[country]" class="input-xlarge"> 
      <option value="[email protected]">Malaysia</option> 
      <option value="[email protected]">Indonesia</option> 
     </select> 
     <span class="help-inline"></span> 
    </div> 
    <div class="controls"> 
     <input type="submit" name='save' value="Save"/> 
     <span class="help-inline"></span> 
    </div> 
</form> 
2
<select name="text selection" onchange="getText(this)"> 
    <option value="">Select text</option> 
    <option value="1">my text 1</option> 
    <option value="2">my text 2</option> 
</select> 

首先提上選擇的屬性「的onchange」 Java腳本函數得到國家的名字。 然後,創建你的函數,將使用的getElementById

傳輸文本框中的文本
<script> 
    function getText(element) { 
    var textHolder = element.options[element.selectedIndex].text 
    document.getElementById("txt_holder").value = textHolder; 
    } 
</script> 

然後創建一個臨時保持:

<input type="" name="txt_holder" id="txt_holder"> type should be hidden 

然後在一個PHP變量指派方式:

$variableName=$_POST['txt_holder']; 
相關問題