从 PHP 的下拉列表中获取所选文本


Get selected text from dropdown in PHP

我是PHP的新手,事实上我这样做的原因是自定义一个wordpress插件,以便它可以满足我的需求。到目前为止,我有一个默认表单,我现在正在做的是添加一个国家/地区下拉列表。这是我添加它的方法

<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="malaysia@email.com">Malaysia</option>
            <option value="indonesia@email.com">Indonesia</option> 
        </select>   
        <span class="help-inline"></span>
    </div>  
</div>

到目前为止,我只能检索所选项目的值

$cscf['country'];

在这种情况下,如何获取显示文本,即国家/地区名称?

您可以使用隐藏字段,并且使用 JavaScript 和 jQuery,当下拉列表的选定值更改时,您可以将值设置为此字段。

<select id="itemType_id" name="cscf[country]" class="input-xlarge">
  <option value="malaysia@email.com">Malaysia</option>
  <option value="indonesia@email.com">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>

然后,当您的页面提交时,您可以使用

$_POST["country"]
<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'];

试试这个

<?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="malaysia@email.com">Malaysia</option>
            <option value="indonesia@email.com">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> 

像这样尝试,

<?php
   if(isset($_POST['save']))
   {
      //Let $_POST['cscf']['country']= malaysia@email.com
      // 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="malaysia@email.com">Malaysia</option>
            <option value="indonesia@email.com">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>