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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19216971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:02:03  来源:igfitidea点击:

Get selected text from dropdown in PHP

phphtmlwordpressforms

提问by abiieez

I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it

我对 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="[email protected]">Malaysia</option>
            <option value="[email protected]">Indonesia</option> 
        </select>   
        <span class="help-inline"></span>
    </div>  
</div>

So far I only able to retrieve the value of selected item with

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

$cscf['country'];

How can I get the display text which is the country name in this case ?

在这种情况下,如何获得国家名称的显示文本?

回答by eyettea

You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.

您可以使用隐藏字段,并且使用 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>

Then when your page is submitted, you can get the name of the country by using

然后当你的页面被提交时,你可以通过使用获取国家的名称

$_POST["country"]

回答by aRyhan

<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>

First put a java script function on the attribute "onchange" of the select. Then, create your function that will transfer the text in a text box by using getElementById

首先在select的属性“onchange”上放一个java脚本函数。然后,创建将使用 getElementById 在文本框中传输文本的函数

<script>
  function getText(element) {
  var textHolder = element.options[element.selectedIndex].text
  document.getElementById("txt_holder").value = textHolder;
  }
</script>

Then create a temporary holder:

然后创建一个临时持有人:

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

Then assign it in a PHP variable:

然后将其分配到 PHP 变量中:

$variableName=$_POST['txt_holder'];

回答by Rohan Kumar

Try it like,

试试看,

<?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>

回答by Nathan Srivi

Try this

尝试这个

<?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>