从<选项获取文本>使用PHP标记 [英] Get Text From <option> Tag Using PHP

查看:109
本文介绍了从<选项获取文本>使用PHP标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在< option> 标签内获得文本及其值。

strong>示例

 < select name =make> 
< option value =5>文字< /选项>
< / select>

我用 $ _ POST ['make']; ,我得到的值 5 ,但我想要获取值和文本

我该如何使用PHP?

解决方案

我认为这是最好的解决方案,因为您已将字段分隔到每个数据。



这里面的HTML:



<$ p $只有一个隐藏字段会在每次更改时更新并避免硬编码映射。 p> < select name ='make'onchange =setTextField(this)>
< option value =''selected>无< / option>
< option value ='5'>文字5< / option>
< option value ='7'> Text 7< / option>
< option value ='9'>文字9< / option>
< / select>
< input id =make_texttype =hiddenname =make_textvalue =/>
< script type =text / javascript>
函数setTextField(ddl){
document.getElementById('make_text')。value = ddl.options [ddl.selectedIndex] .text;
}
< / script>

这里面的PHP:

 <?php 
$ value = $ _POST [make];
$ text = $ _POST [make_text];
?>


I want to get text inside the <option> tags as well as its value.

Example

<select name="make">
<option value="5"> Text </option>
</select>

I used $_POST['make']; and I get the value 5 but I want to get both value and the text.

How can I do it using PHP?

解决方案

What about this? I think it's the best solution because you have separated fields to each data. Only one hidden field which is updated at each change and avoids hardcoding mappings.

This inside HTML:

<select name='make' onchange="setTextField(this)">
<option value = '' selected> None </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<input id="make_text" type = "hidden" name = "make_text" value = "" />
<script type="text/javascript">
    function setTextField(ddl) {
        document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
    }
</script>

This inside PHP:

<?php
$value = $_POST["make"];
$text = $_POST["make_text"];
?>

这篇关于从&lt;选项获取文本&gt;使用PHP标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆