1

I want to make a dropdown list with the values from inside the xml file. The dropdown is present but it is blank on the web. Why? Is there something i've missed?

I have the following code:

<form method="post" action="">
    <?php
    echo "<select>";
        $xml = simplexml_load_file('curs.xml');
            foreach ($xml->item as $item)
            {
                echo "<option value='".$item->name."'></option>";
            }
    echo "</select>";
    ?>
</form>

The code for the xml file:

<prod>

<item>
    <name>Cheese</name>
    <price>4.25</price>
</item>
<item>
    <name>Milk</name>
    <price>8.12</price>
</item>
<item>
    <name>Egg</name>
    <price>0.81</price>
</item>

</prod>
2
  • You don't have anything in the option. `<option value="foo">Foo</option> Commented Jun 22, 2014 at 16:36
  • Works here: codepad.org/iEug9Lc4 You must have a path problem to the xml file. Use the absolute path, e.g. /home/sites/www/resources/curs.xml (or whatever it is). EDIT: I've just noticed you're not printing the values within the <option> tags either, so they'll appear blank. See: codepad.org/XArgKuSq Commented Jun 22, 2014 at 16:38

1 Answer 1

2

Try this

foreach ($xml->item as $item)
{
   echo "<option value='".$item->name."'>" . $item->name . "</option>";
}

The visible part of an <option> is what you put between <option> and </option>

Sign up to request clarification or add additional context in comments.

4 Comments

That's so right, i've missed this part and I thought something is wrong with the code. I am newbie. Thank you!
php.net/htmlspecialchars exists for a reason ;) And take care that simplexml returns UTF-8, that should be also the encoding of the HTML file then.
I have no idea what he is on about
I guess it is about compatibility between the xml and html... just saying!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.