zvzej
zvzej

Reputation: 6336

How to pass a variable in form using GET with spaces in PHP+HTML+SQL

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?

Upvotes: 3

Views: 15245

Answers (7)

Iłya Bursov
Iłya Bursov

Reputation: 24146

According to HTML standard you should use " (double quotes) around values and encode special characters (" < > &), php has standard function for that:

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

Note - neither mysql_real_escape_string nor urlencode is used, as they escape different symbols neither htmlspecialchars and it is invalid to use them in this context

Upvotes: 0

crisc2000
crisc2000

Reputation: 1222

About Gung Foo response. urlencode() will not transform white spaces to %20 but to + character. Use rawurlencode() to transform white spaces to %20.

Also you may want to read this: https://stackoverflow.com/a/2678602/4481831

Upvotes: 0

el Dude
el Dude

Reputation: 5183

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

Upvotes: 1

random_user_name
random_user_name

Reputation: 26160

Per my comment above:

The problem is that there are not quotes around the option values. Change it like so:

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

and you'll be good to go.

Upvotes: 3

Pedryk
Pedryk

Reputation: 1663

Use the function urlencode on the variables you want to pass in get. See http://php.net/manual/en/function.urlencode.php for a reference.

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

Then when you want to use the variable again you can use urldecode to decode it. http://php.net/manual/en/function.urldecode.php

After your edit:

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

Should be:

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

With the qoutes and the urlencode.

Upvotes: 0

Gung Foo
Gung Foo

Reputation: 13558

use urlencode(). it will transform the spaces into %20 which will be transfered successfully.. you can decode it using urldecode().

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

Upvotes: 3

Martyn Shutt
Martyn Shutt

Reputation: 1706

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

Upvotes: 2

Related Questions