Javascript 使用 php 获取所选选项的文本

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

get the text of the selected option using php

phpjavascripthtml

提问by Max_Salah

in the following:

在下面的:

<form  action="test.php"  method="POST">  
    <select id="test" name="test">
     <option value="1">Test One</option>
     <option value="2">Test Two</option>
    </select>
</form>

in test.php, I can get 1 or 2 as follow:

在 test.php 中,我可以得到 1 或 2 如下:

$result=$_POST['test'];

How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php

如何使用 php 获取所选选项的文本(即“测试一”或“测试二”)

回答by Ben D

This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the valuefor the selectedoption that is posted). You can use javascript to alter a hidden inputfield with the text contents of a selected option, and this will be included in the $_POSTarray:

这不是单独通过 PHP 可以完成的。PHP 脚本只能“查看”发布的信息(value对于发布的selected选项)。您可以使用 javascript 使用inputselected 的文本内容更改隐藏字段option,这将包含在$_POST数组中:

<form  action="test.php"  method="POST">  
    <select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
     <option value="1">Test One</option>
     <option value="2">Test Two</option>
    </select>

<input type="hidden" name="test_text" id="text_content" value="" />
</form>

This will make the $_POST['test_text']available with the selected index (but you should also force the onchange()function when the page loads so that it will be populated even if the user leaves the select field at the default value.

这将使$_POST['test_text']所选索引可用(但您还应该onchange()在页面加载时强制使用该功能,以便即使用户将选择字段保留为默认值,它也会被填充。

回答by powerbuoy

Only the valueof the form control will be sent to the server. An easy way to send both is to simply include bothin the the value:

只有value表单控件的 将被发送到服务器。发送两者的一种简单方法是简单地将两者都包含value:

<select name="test">
    <option value="1|Test one">Test one</option>
    <option value="2|Test two">Test two</option>
</select>

And then:

进而:

$test = explode('|', $_POST['test']);

Then you'll end up with $test[0]being "1" and $test[1]being "Test one".

然后你最终会$test[0]成为“1”并$test[1]成为“测试一”。

回答by Ignacio Vazquez-Abrams

You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.

你不能;该信息不会发送回服务器。您首先需要查看如何生成 HTML 并从那里获取文本。

回答by ThiefMaster

It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.

它不会发送,因此获取它的唯一方法是在 PHP 代码中将值映射到标题。

回答by Mark Stout

This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.

这是我可以使用的解决方案,但我已经有了 onChange 调用函数以根据语句中选定的建筑类型显示隐藏块。

I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.

我有需要用户选择的建筑类型。一些建筑类型有一组独特的问题,例如银行分行、数据中心、法庭等。但有许多建筑类型具有相同的一组问题,我称之为其他类型。每个选项都有其关联的值,例如银行分行的值为“Bank_Branch”,数据中心的值为“Data_Center”,而其他类型的值为“Other_Type”。“Other_Type”的文本因建筑类型而异,例如会议中心、博物馆、表演艺术等。因此,在使用文本时,我需要值“Other_Type”来“显示”“Other_Type”DIV 块中的问题值在电子邮件中发送标识建筑物的类型,例如会议中心、博物馆、表演艺术等。

Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML

有什么方法可以使用PHP来获取所选项目的文本值?我已经在 HTML 中使用了以下内容

   var sele = document.getElementById('building_type');
   var seleVal = sele.options[sele.selectedIndex].value;
   var seleTxt = sele.options[sele.selectedIndex].text;
   document.getElementById("other_text").innerHTML = seleTxt;

I'm not seeing a way to do this.

我没有看到这样做的方法。

SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.

已解决:我可以简单地创建隐藏的 div,并在检查其他类型时为隐藏的 div 设置 innerHTML。

回答by user3476261

Im not a experienced php programer, but u can check the value selected in SELECT tag with php. i dont know why people say u can not.

我不是一个有经验的 php 程序员,但你可以用 php 检查在 SELECT 标签中选择的值。我不知道为什么人们说你不能。

if ( $_POST['test'] == 1) 
{ do something } 
else 
{ option value 2 is selected, do something else }

Im sure this will work.

我确定这会奏效。

回答by MaxEcho

Try it if 1, 2 not needed. you will get required text as value

如果不需要 1、2,请尝试。您将获得所需的文本作为值

<form  action="test.php"  method="POST">  
    <select id="test" name="test">
     <option value="Test One">Test One</option>
     <option value="Test Two">Test Two</option>
    </select>
</form>

回答by scott.korin

The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìdinto the text (through a switch statement, or by querying a database, etc.)

所选选项的文本不会通过 HTML 表单传递。如果需要文本,则必须将其存储在页面上隐藏的 HTML 输入中,或者需要在 PHP 代码中添加一些业务逻辑以将 的值ìd转换为文本(通过 switch 语句,或通过查询数据库等)

回答by Stephen Cioffi

Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?

不幸的是,当您提交表单和变量时,它只需要一个参数,即它的值。你需要创造价值。测试一个值,以便将其传递给 PHP 脚本。value="1" 的目的是什么,因为您可能可以在不同的属性中使用它?