3

I have a form where I display a select item and two other invisible inputs with some variable values that I need to pass to the same page at the press of the "Action" button. But I'm having the problem that those variables are city names like "New York", so there is a space inside the name, and at the moment of passing the variable only the "New" gets passed; nothing after the space goes with it. I have read that there shouldn't be any spaces in those variables, so how should I do this?

Here is my sample code:

// 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

}

And I have read that variables cannot be passed with spaces, why does my $pais variable "United States" gets passed with the space in between correctly? Can someone tell me how to achieve this or how to transform my <option value=<?php echo $Se ?>> so it can pass the space?

4

7 回答 7

4

使用urlencode()。它将转换%20成功传输的空间..您可以使用urldecode()对其进行解码。

<option value=<?php echo urlencode($Se); ?>>
于 2012-10-27T20:28:38.490 回答
4

根据我上面的评论:

问题是选项值周围没有引号。像这样改变它:

<option value="<?php echo $Se ?>"> 

你会很高兴的。

于 2012-10-27T20:45:10.827 回答
3

I believe this has to do with URL Encoding. A URL can't have spaces in it. However, there are special characters that can be used to represent a space in a url. The GET Method uses the URL.

I believe this may help; http://php.net/manual/en/function.urlencode.php

于 2012-10-27T20:28:25.573 回答
1

对要传入 get 的变量使用函数 urlencode。请参阅http://php.net/manual/en/function.urlencode.php以获取参考。

<input type="HIDDEN"  name=pais  value="<? echo urlencode($SP); ?>">

然后,当您想再次使用该变量时,您可以使用 urldecode 对其进行解码。http://php.net/manual/en/function.urldecode.php

编辑后:

<option value=<?php echo $Se ?>>

应该:

<option value="<?php echo urlencode($Se); ?>">

使用 qoutes 和 urlencode。

于 2012-10-27T20:29:21.613 回答
1
<option value="<?php echo htmlspecialchars($Se); ?>">
于 2012-10-27T20:53:00.423 回答
1

关于 Gung Foo 的回应。urlencode() 不会将空格转换为 %20 而是转换为 + 字符。使用 rawurlencode() 将空格转换为 %20。

您也可能想阅读以下内容:https ://stackoverflow.com/a/2678602/4481831

于 2016-05-18T21:17:49.567 回答
0

根据 HTML 标准,您应该在值周围使用 "(双引号),而且 - 您必须正确编码它们,因此您的代码必须如下所示:

<option value="<?php echo htmlspecialchars($rowp['Estado']); ?>">

注意 - 没有使用 mysql_real_escape_string,因为它转义了不同的符号,htmlspecialchars 也没有

于 2013-10-09T19:40:39.350 回答