[英]How to replace attributes with HTML Element content(plain text) with PHP
I making a form that will take contact info and send it to an email. 我制作了一份表格,其中包含联系信息并将其发送至电子邮件。 It works expect the cities which I have more than 2000 options. 它的工作期望我有超过2000种选择的城市。 The cities drop down sends the value, which is just a number. 城市下拉发送值,这只是一个数字。 I want the form to send the city, which is HTML element content not the value number in the attribute. 我希望表单发送城市,这是HTML元素内容而不是属性中的值编号。 I do not want to replace the 2000 different values with the cities. 我不想用城市替换2000个不同的值。 I know a loop would the way to go but I don't know how to create it. 我知道循环会走的路,但我不知道如何创建它。
HTML - HTML -
<option value="2" >Los Angeles - CA</option>
PHP - PHP -
$city = $_REQUEST['city'] ;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
mail( "$webmaster_email", "Submission", "$city,"From:$email" );
header( "Location: $thankyou_page" );
Use an array! 使用数组! Example: 例:
<?php
$cities = array(
'0' => 'New York - NY',
'1' => '...'
'2' => '...'
);
if (isset($cities[$_REQUEST['city']])) {
$city = $cities[$_REQUEST['city']];
} else {
$city = $_REQUEST['city'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
mail( "$webmaster_email", "Submission", "$city,"From:$email" );
header( "Location: $thankyou_page" );
}
?>
I believe you want to send the text and not the value from the option
. 我相信你想发送文本而不是option
的值 。 Right? 对?
I suggest you to create this element on your form: <input type='hidden' id='city_name' name='city_name' />
to hold the select
value with JavaScript when user select his desired option. 我建议你在表单上创建这个元素: <input type='hidden' id='city_name' name='city_name' />
当用户选择他想要的选项时,用JavaScript保存select
值。 Then, create and event on the select
like this: 然后,在select
上创建和事件,如下所示:
<select onchange="document.getElementById('city_name').value=this.options[this.selectedIndex].text;">
So you'll have the $_POST['city_name']
available on your PHP code. 因此,您的PHP代码上将提供$_POST['city_name']
。
Presumably you produced that <option>
list with something on the server, eg an array: 据推测,您在服务器上生成了<option>
列表,例如数组:
$locations = array(
...
2 => 'Los Angeles - CA'
...
);
then you'd simply have 那么你就是这样
$place = $locations[$_REQUEST['city']];
to get the text for the option. 获取选项的文本。
Alternatively, you embed the full city name in the option: 或者,您可以在选项中嵌入完整的城市名称:
<option value="Los Angeles - CA">Lost Angeles - CA</option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.