PHP 获取下拉值和文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6670002/
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-26 00:57:31  来源:igfitidea点击:

PHP get dropdown value and text

phpdrop-down-menu

提问by Sasindu H

<select id="animal" name="animal">                      
<option value="0">--Select Animal--</option>
<option value="1">Cat</option>
<option value="2">Dog</option>
<option value="3">Cow</option>
</select>

if($_POST['submit'])
{
$animal=$_POST['animal'];
}

I have a dropdown like this. What I want, I want to get selected value and text in button submit using PHP. I mean if it's selected 1st one. I want to get both 1and Cat

我有一个这样的下拉菜单。我想要什么,我想使用PHP在按钮提交中获取选定的值和文本。我的意思是如果它被选中第一个。我想同时获得1Cat

Please help. thank you.

请帮忙。谢谢你。

回答by Swift

Is there a reason you didn't just use this?

你有什么理由不只是使用这个吗?

<select id="animal" name="animal">                      
  <option value="0">--Select Animal--</option>
  <option value="Cat">Cat</option>
  <option value="Dog">Dog</option>
  <option value="Cow">Cow</option>
</select>

if($_POST['submit'] && $_POST['submit'] != 0)
{
   $animal=$_POST['animal'];
}

回答by john

$animals = array('--Select Animal--', 'Cat', 'Dog', 'Cow');
$selected_key = $_POST['animal'];
$selected_val = $animals[$_POST['animal']];

Use your $animals list to generate your dropdown list; you now can get the key & the value of that key.

使用您的 $animals 列表生成下拉列表;您现在可以获取密钥和该密钥的值。

回答by Bill Heller

You will have to save the relationship on the server side. The value is the only part that is transmitted when the form is posted. You could do something nasty like...

您必须在服务器端保存关系。该值是表单发布时唯一传输的部分。你可以做一些讨厌的事情,比如......

<option value="2|Dog">Dog</option>

<option value="2|Dog">狗</option>

Then split the result apart if you really wanted to, but that is an ugly hack and a waste of bandwidth assuming the numbers are truly unique and have a one to one relationship with the text.

如果您真的想要,然后将结果分开,但假设数字确实是唯一的并且与文本具有一对一的关系,那么这是一个丑陋的黑客和浪费带宽。

The best way would be to create an array, and loop over the array to create the HTML. Once the form is posted you can use the value to look up the text in that same array.

最好的方法是创建一个数组,并遍历该数组以创建 HTML。表单发布后,您可以使用该值在同一数组中查找文本。

回答by Srinivasa Reddy

you can make it using js file and ajax call. while validating data using js file we can read the text of selected dropdown

您可以使用 js 文件和 ajax 调用来制作它。在使用 js 文件验证数据时,我们可以读取所选下拉列表的文本

$("#dropdownid").val();   for value
$("#dropdownid").text(); for selected value

catch these into two variables and take it as inputs to ajax call for a php file

将这些捕获到两个变量中,并将其作为 ajax 调用的输入来调用 php 文件

$.ajax 
   ({
     url:"callingphpfile.php",//url of fetching php  
     method:"POST", //type 
     data:"val1="+value+"&val2="+selectedtext,
     success:function(data) //return the data     
     {

}

}

and in php you can get it as

在 php 中你可以得到它

    if (isset($_POST["val1"])) {
    $val1= $_POST["val1"] ;
}

if (isset($_POST["val2"])) {
  $selectedtext= $_POST["val1"];
}