1

I'm new to PHP, HTML & web development in general.

I was trying to get a listbox to read entries in from a associative array, which I managed to do. The problem is that, I can't seem to access the correct value, when an option is selected. It always seems generate the same response ("London"), regardless of which option is chosen. I tried replacing $city with ["listBox"], but the output was 'selected="selected"', $city seemed closer to what I was wanting, so I changed it back.

I've spent a lot of time trying to figure out the reason why, if somebody can help me with this, I would appreciate it, thank you in advance.

The code is below:

<!DOCTYPE html lang="en"/>`    
<html>
    <head>
        <title>Array Section: ex5.php</title>
    <charset = "utf-8">
    </head>
    <body>
        <?php
        // Create an associate array 
        $countriesWithCities = array(
            "Japan" => "Tokyo",
            "Mexico" => "Mexico City",
            "USA" => "New York City",
            "India" => "Mumbai",
            "South Korea" => "Seoul",
            "China" => "Shanghai",
            "Nigeria" => "Lagos",
            "Brazil" => "Sao Paulo",
            "Egypt" => "Cairo",
            "England" => "London"
        );
        ?>
        <form action="ex5b.php" method="POST" />
        <h1>Ex5b.php </h1>
        <h3>"listBox">Please choose a country from the list box. </h3>
        <select name="listBox" id="listBox" size="9" >
            <?php foreach ($countriesWithCities as $individualCountry => $city) { ?>
                <option value= <?php $city; ?> selected="selected">
                    <?php echo $individualCountry;
                    >
                } ?></option>
        </select>

        <input type="submit" name="submitButton" id="submitButton" value="Submit form" />
        <form>
            <?php
            if (isset($_POST["submitButton"])) {
                echo "You chose " . $city;
            }
            ?>
    </body>
</html>
3
  • You indent code with four spaces to format it. I've fixed it for you. Remember this for the future. Commented Mar 28, 2013 at 23:29
  • What does $_POST contains? Commented Mar 28, 2013 at 23:30
  • @Jocelyn, when I did it originally with $city, the response was "London" (no matter what option I chose). I changed "$city" to ["listBox"], and it only displayed 'selected="selected" '. Thanks for your question. Commented Mar 29, 2013 at 1:01

3 Answers 3

1

You were missing a echo before $city, and an extra closing > after $individualCountry. As well you had the closing </option> tag after the closing brace (}) in the foreach loop.

<select name="listBox" id="listBox" size="9" > 
<?php 
    foreach($countriesWithCities as $individualCountry=>$city)
    {
?>
    <option value="<?php echo $city; ?>" selected="selected"> <?php echo $individualCountry; ?>
    </option> 
<?php } ?>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

Was going to say you were missing quotes around the city, but you seem to have noticed that ;) Now you can has a +1. Although I'd probably still htmlspecialchars both those vars in case he puts anything weird in there (like quotes or angle brackets).
Gerbenny, I used Control + K, for each line, and it still didn't come out right. Did you use Control + K for each line, or just once (and paste the entire code into the speech marks?).
0
<option value= <?php $city; ?> selected="selected">

Change to

<option value= <?php echo $city; ?> selected="selected">

2 Comments

If you ask me I would remove the selected attribute. Just saying
Ejay and Gerbenny, the code you pasted works fine. Thanks to everyone who stopped to help & therefore improve my understanding.
0

correct code

<!DOCTYPE html lang="en">
<?php
$countriesWithCities = array(
   "Japan" => "Tokyo",
   "Mexico" => "Mexico City",
   "USA" => "New York City",
   "India" => "Mumbai",
   "South Korea" => "Seoul",
   "China" => "Shanghai",
   "Nigeria" => "Lagos",
   "Brazil" => "Sao Paulo",
   "Egypt" => "Cairo",
   "England" => "London"
);
?>
<form action="ex5b.php" method="POST">
<h1>Ex5b.php </h1>
<h3>Please choose a country from the list box. </h3>
<select name="city" id="listBox">
   <?php foreach($countriesWithCities as $individualCountry=>$city){?>
   <option value=<?php echo $city; ?>> <?php echo $individualCountry; ?></option>
   <?php } ?>
</select>
   <input type="submit" name="submitButton" id="submitButton" value="Submit form" />
</form>

<?php if(isset($_POST["submitButton"]))
{
   echo "You chose " . $_POST['city'];
}
?>
</body>
</html>

corrections made

  1. you probably copied this code from a book (PDF?) so starting PHP tag after doctype was missing along with array name.
  2. Form was missing closing tag
  3. <select> was missing name attribute
  4. you were looking for $city in PHP code instead of $_POST['city']

and a lot happened after I copied code from your question or I just copied the incomplete code :D

7 Comments

Ejay the code was not copied, I did it myself, borrowed lines from various sources, hence the many mistakes. Thanks for taking the time to correct it.
ah, sorry for my assumption :) the "ex5b.php" part drove me off :D
you got this to work without the selected attribute? Now I'm puzzled, the books I've read all say you need it. tried to access country by using $_POST['individualCountry'], that failed too. So I'm again puzzled how it worked for $_POST['city']?
<select name="city" id="listBox"> the browser posts the value of selected <option> to the server-side script, and it is available as an index of $_POST array in PHP with key name equal to name attribute of the <select> (almost same is the case with all HTML elements), hence you get $_POST['city']
thanks for the explanation, it makes more sense to me now. So why do the books keep on stressing use the select attribute to ensure that the choice is registered? The 5b, is because I did 5, it wasn't working, I took a copy to see if I could get it to work. Hence the name 5b. I was practising what I had learned. I do not (yet) have an assignment to submit. Just trying to improve my understanding, nothing else.
|

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.