3

I get a strange problem when extracting the selected option of an html drop down list embedded in a php script, from another PHP script. If the words of the option values are separated with spaces. PHP echos only the first word of the selected option value. What I'm doing wrong here?

list.php

<body>
<form action="extract.php" method="post"> 
<?php


 $one = "one one";
 $two = "two two";
 $three = "three three";

 echo '<select name="selected_item">';
 echo '<option value='.$one.'>'.$one.'</option>';
 echo '<option value='.$two.'>'.$two.'</option>';
 echo '<option value='.$three.'>'.$three.'</option>';
 echo '</select>';


?>
<input type="submit" class="button" value="submit" name="submit" />

</form>
</body>
</html>



    extract.php

     <?php

      if (isset($_POST['submit'])) {

        $item_name = $_POST['selected_item'];
        echo $item_name;

      }else{
        echo "item not selected";
      }

     ?>

If I select "three three" in the list PHP echos only "three". Why is that?

sheng
  • 61
  • 2
  • 6
  • errr one thing your form action says tp.php as the target whereas you are listing extract.php in the example... do you think that may be causing you trouble? tp.php may have a bug in it? :) – Ahmed Masud Nov 19 '11 at 09:28
  • @Ahamed. It was a mistake. corrected as form action="extract.php" – sheng Nov 19 '11 at 10:09

4 Answers4

9

It's because you didn't quote your HTML attributes. When you have this code:

$three = "three three";
echo '<option value='.$three.'>'.$three.'</option>';

This will come out as HTML:

<option value=three three>three three</option>

The PHP string quotes are gone in the output. And only the first three will become attribute value. The other will just be trailing garbage.

You need a better echo:

echo '<option value="'.$three.'">'.$three.'</option>';

Even better might be a HEREDOC string in such cases. And don't forget htmlspecialchars() for non-static content.

Dharman
  • 30,962
  • 25
  • 85
  • 135
mario
  • 144,265
  • 20
  • 237
  • 291
3

If I select "three three" in the list PHP echos only "three". Why is that?

That's because the HTML contains the information "three" as value for the selected element:

<option value=three three>

Example: An option tag with a value=three and a three (with no value) attribute.

The browser then sends "three" to the server. PHP only gets what the browser sends.

It's highly likely that you just made an error in your HTML form output. Enclose HTML tags attribute values in quotes then your browser will submit "three three" as the value, because the quotes will preserve the space inside your option value:

<option value="three three">

Example: An option tag with one value="three three" attribute.

That's just how HTML works, the browser need to parse the tag, it's attributes and attribute values. It's most easy explained in HTML 2 how that works: http://www.w3.org/MarkUp/html-spec/html-spec_3.html#SEC3.2.4

Additionally ensure that you properly encode the values for HTML by using htmlspecialchars. The following example code makes use of an array for all options so the code is more modular:

$options = array("one one", "two two", "three three");

echo '<select name="selected_item">';
foreach ($options as $option)
{
    printf('<option value="%1$s">%1$s</option>', htmlspecialchars($option));
}
echo '</select>';
hakre
  • 193,403
  • 52
  • 435
  • 836
0

Your @sheng only one correction dont use direct space use instead of Html_entity space,,

reset the variable its enaugh...

  <?php 
     $one = "one&nbsp;one";
     $two = "two&nbsp;two";
     $three = "three&nbsp;three";
     ?>

U get Your expected out put

K6t
  • 1,821
  • 1
  • 13
  • 21
  • 1
    I guess the user had a simple space, not a non-breaking space here, so the suggestion is a bit wrong. Try ` ` instead. – hakre Nov 19 '11 at 09:49
-1

That is what quotes are for.

In fact, your question has nothing to do with PHP.
It is only basic HTML that your code has a relation to.

Take a habit of watching the actual HTML code produced by your PHP.
Most of time you will be able to spot the problem by yourself. Or at least address it more precisely. (as an HTML problem in your case, not PHP)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345