【发布时间】:2012-10-17 17:16:10
【问题描述】:
我有一个表单,其中显示了一个选择项和另外两个不可见的输入,其中包含一些变量值,我需要在按下“操作”按钮时将它们传递到同一页面。但是我遇到的问题是这些变量是像“New York”这样的城市名称,所以名称中有一个空格,并且在传递变量时只有“New”被传递;空间消失后什么都没有。我已经读过这些变量中不应该有任何空格,那么我应该怎么做呢?
这是我的示例代码:
// at the beginning of the code I get this variables pass from other pages
$pais=$_GET['pais'];
$name=$_GET['nombre'];
// this is how I query my table to populate my select item
$SN=mysql_real_escape_string($name);
$SP=mysql_real_escape_string($pais);
$estadoquery = "SELECT * FROM `".$SP."` ORDER BY Estado ASC";
$estadoresult = mysql_query($estadoquery);
// this is how I make my form
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="HIDDEN" name=nombre value="<? echo $SN ?>">
<input type="HIDDEN" name=pais value="<? echo $SP ?>">
<th scope="col" align="center" valign="baseline" size="18px">
<select name=estado id="estado" style="font-size:24px">
<option value="">Todos</option>
<?
while($rowp = mysql_fetch_array($estadoresult)) {
$estado = $rowp['Estado'];
$Se=mysql_real_escape_string($estado);
?>
<option value=<?php echo $Se ?>>
<?php echo $Se ?></option>
<? } ?>
</select>
</th>
<th scope="col" align="center" valign="baseline">
<input type="SUBMIT" name="ACTION" value="IR">
</th></form>
// and this is what the button action does
if(@$_POST['ACTION']=='IR')
{
$pais = $_POST['pais'];
$name = $_POST['nombre'];
$estado = $_POST['estado'];
$pais = mysql_escape_string($pais);
$name = mysql_escape_string($name);
$estado = mysql_escape_string($estado);
// this next echos are just to check my variables.
echo $pais;
echo $name;
echo $estado; // so here I can tell that this variable is not complete
}
我已经读到变量不能用空格传递,为什么我的$pais 变量“United States”可以正确传递中间的空格?
有人可以告诉我如何实现这一目标或如何转换我的 <option value=<?php echo $Se ?>> 以便它可以通过空间吗?
【问题讨论】:
-
变量的值可以有空格,名字不能。例如,
$pais可以包含“New York”,但变量不能称为$pais name。有意义吗? -
问题是您没有在选项值周围加上引号。像这样更改它:
<option value="<?php echo $Se ?>">,你会很高兴的。 -
@cale_b 这是解决方案,谢谢!