1

I'm trying to be able to get place as a variable to use on page.php. It will start to load options into the select box for about 5 seconds and I'll actually be able to see the options, then it all deletes and I have an empty select box. Here's the PHP code:

<?php
$xml = simplexml_load_file("file.xml");
echo '<form method="get" action="page.php"><select name="place" id="place">';
foreach($xml as $banana)
{
    if(isset($banana->id))
    {
        $id = $banana->id;
        $string = $banana->state . " - " . $banana->name;
        echo '<option value="' . $id . '">' . $string . '</option>';

    }
}
echo "</select>";
echo '<input type="submit" /></form>';

?>

I know for sure that my XML is formatted correctly, but the XML file has a little over 2500 entries that each looks something like this:

<container>
    <id>theId</id>
    <state>theState</state>
    <name>theName</name>
</container>

Any ideas why won't load?

Note: Some items have an empty id in the XML file, and I don't want to include those files, that's why I check to make sure it's set.

9
  • If instead of creating a select box you just print_r($xml); do all entries show? Commented Jan 13, 2017 at 19:00
  • @Ray Yes, they all show up with print_r($xml); Commented Jan 13, 2017 at 19:05
  • Also, if you're sticking data in HTML like that you're going to want to make it safe in case the id or name has characters that could mess up the HTML. echo '<option value="' . htmlspecialchars($id) . '">' . htmlspecialchars($string) . '</option>'; Commented Jan 13, 2017 at 19:06
  • @Ray It's web safe in the XML file so I don't need to worry about that, all content is added to the XML file by me, not users. I really do appreciate the feedback though. Commented Jan 13, 2017 at 19:09
  • Do you have any Javascript binding on to the the select box? Disable javascript and refresh, does the pure html select box still die? Commented Jan 13, 2017 at 19:10

1 Answer 1

1

A couple things

  • Disable any javascript and try again--something may be binding to overriding the select box or the id of an element in it.
  • You generally want to properly escape text you pull in before inserting it into a HTML document in case there's a <, >, or such in it

Try the following:

   echo '<option value="' . htmlspecialchars($id) . '">' . htmlspecialchars($string) . '</option>'; 
Sign up to request clarification or add additional context in comments.

Comments

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.