2013-10-07 93 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']; 
相关问题