[英]Select box fails to fetch option value with white spaces after POST
I have a select box which fetches the values from the database.the options in the database are: 我有一个选择框,它从数据库中获取值。数据库中的选项是:
Newyork city 纽约市
Jeju island 济州岛
the select box shows the exact values from the database. 选择框将显示数据库中的确切值。 I have a submit button which POSTs the values to another page. 我有一个提交按钮,将值发布到另一个页面。 My problem is when I echo the selected option value the words after the white spaces are not shown ie it shows only 'Newyork'. 我的问题是,当我回显选定的选项值时,没有显示空格后的单词,即仅显示“ Newyork”。 Here is my code: 这是我的代码:
<select name="users_desc" id="hometexts">
<option selected='selected' value='' disabled='disabled' >Select Name</option>";
<?php
$result = mysql_query("SELECT public_desc,public_id FROM table");
while ($row = mysql_fetch_array($result))
{
if ($_GET['users_desc']==$row["public_id"])
{
echo '<option selected="selected" value='.$row["public_desc"].'>'.$row["public_desc"].'</option>';
}
else
{
echo '<option value='.$row["public_desc"].'>'.$row["public_desc"].'</option>';
}
}
?>
</select>
code for POSTing the option value to another page. 用于将选项值发布到另一个页面的代码。
if(isset($_POST['login_btn']))
{
$desc=$_POST['users_desc'];
echo $desc;
header("location:publicUserReports.php");
}
I just want to get the complete value. 我只想获得完整的价值。
You need to quote the values 您需要引用值
value="'.$row["public_desc"].'"
So 所以
echo '<option selected="selected" value="'.$row["public_desc"].'">'.$row["public_desc"].'</option>';
When you look at the HTML it must show 当您查看HTML时,它必须显示
<option value="Jeju island">
instead of 代替
<option value=Jeju island>
Just for more information, there is a great difference between " and ' if you are going to out a string/text with no variables to concatenate you should always use single quote ('). 只是为了获得更多信息,如果要输出没有变量来连接的字符串/文本,则“和”之间会有很大的区别,您应该始终使用单引号(')。
But if you have some variables to concatenate, you should use(optional) the double quote ("). Personally i preferred using it because it is more readable. 但是,如果有一些要连接的变量,则应该使用(可选)双引号(“)。我个人更喜欢使用它,因为它更具可读性。
Examples: 例子:
if im just going to output, lets say: i love coding. 如果我只是要输出,可以说:我喜欢编码。 then i use: 然后我用:
echo 'I love Coding';
but when i have some variables to concatenate, i use: 但是当我有一些变量要连接时,我使用:
echo "I $action Coding";
now based on your problem: 现在根据您的问题:
echo "<option selected='selected' value='$row[public_desc]'>$row[public_desc]</option>";
for me this is more readable and less prone to error :) 对我来说,这更具可读性,更不容易出错:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.