【发布时间】:2019-07-17 09:04:04
【问题描述】:
我在使用 PHP 选择选项值时遇到问题
显示州和城市的多个选择框。 Jquery 作为数据库的 ajax 方法加载。我需要达到“期权价值”的内在价值。我将在下面的代码中显示问题。
<?php
function load_country() {
include 'includes/db.php';
$sql = "SELECT * FROM ilce ORDER BY name";
mysqli_set_charset($con, "UTF8");
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$output .= '<option name="' . $row['name'] .'" value="' .
$row["ilce_id"].'">' . $row["name"]. '</option>';
}
return $output;
}
?>
<select class="form-control" name="state" id="state">
<option>Choose State</option>
<?php
echo load_country();
?>
</select>
<?php
$state = mysqli_real_escape_string($con, $_POST['state']);
echo $state;
?>
<option value="5">New York</option>
$state is showing for me Example : "id" => 5
因此,我想到达“纽约”,内部价值。 请帮我 谢谢
【问题讨论】:
-
<option>元素的内容不会发布到服务器,只会发布它的value。我建议通过 ID 引用数据库中的名称。或者,将value设置为行的name而不是其ilce_id。 -
好吧,如果您想在“New York”被选中并使用 jQuery 时获得值 (5),您可以通过
$("selector").val()获得。可以使用$("selector").html()检索“纽约” -
它没有任何解决方案。我无法将值设置为“名称”,因为我使用的是相对数据库。然后,每个值都将被更改。 @David您说的是与jquery一起使用。但我必须使用 php 在数据库中添加这个值
-
看起来你在这里很容易受到持久性 XSS 的攻击。很确定那行应该是
$output .= '<option name="' . htmlentities(urlencode($row['name'])) .'" value="' . htmlentities(urlencode($row["ilce_id"])).'">' . htmlentities($row["name"]). '</option>';...除非您将预先htmlencoded的数据存储在数据库中? (可能但不寻常)
标签: php html-select