stublackett Posted October 23, 2009 Share Posted October 23, 2009 Hi Guys, Just working through WROX's Beginning PHP 5 Book and go to producing an web page that uses Arrays to differentiate countries and pair them to their capital. The form seems to function but doesnt echo the countries capital at all... Why would it do this? Pretty sure I've followed it word for word too.. Code is as follows..... <html> <head><title>Capitals</title></head> <body> <?php if (isset($_POST['posted'])) { $country_capital = array(0 => "London", "Paris", "Moscow", "Lisbon", "Madrid", "Stockholm", "Copenhagen", "Washington", "Montreal", "Brisbane", "Tokyo", "Rome", "Mexico City", "Prague", "Berlin", "Dublin"); for ($counter=0; $counter<16; $counter++) { if($_POST['hiddencountry'][$counter]==$_POST['country']) { echo "The capital of $_POST[country] is <b>$country_capital[$counter]</b><hr> "; } } } ?> <form action="capitals.php" method="POST"> <input name="posted" type="hidden" value="true"> What Country do you want to know the capital of? <select name="country"> <?php $capitals_of_countries = array(1 => "England", "France", "Russia", "Portugal", "Spain", "Sweden", "Denmark", "USA", "Canada", "Australia", "Japan", "Italy", "Mexico", "Czech Republic", "Germany", "Ireland"); for ($counter = 1; $counter <16; $counter++) { echo "<option>$capitals_of_countries[$counter]</option>"; } echo "</select><br><br>"; for ($counter = 1; $counter <16; $counter++) { echo "<input type='hidden' name'hiddencountry[]' value='$capitals_of_countries[$counter]'>"; } ?> <input type="submit" value="Find Capital"> </form> </body> </html> Thanks in advance Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 23, 2009 Share Posted October 23, 2009 Your submit button needs a name. In this case, it should be 'posted' (without the single quotes) because that's what you're checking to see is set before echoing out the result. You also need to ensure that all non-variable array keys are in quotes. Also, when echoing arrays in a string, I find it useful to use curly brackets, like so: echo "The capital of {$_POST['country']} is <b>{$country_capital[$counter]}</b>"; When you get into more advanced code, like retrieving values from a database, you'll save yourself some headaches doing it that way. Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 23, 2009 Share Posted October 23, 2009 Hi stublackett, While what Nightslyr has said is true, the reason the form isn't working is because you have just missed the = on the name attribute for the hidden input field. Change it to: echo "<input type='hidden' name='hiddencountry[]' value='$capitals_of_countries[$counter]'>"; Once you make this change your form will work as expected As an additional point, change every instance of $counter<16 to read $counter<17 instead, otherwise "Ireland" does not appear in the list. Hope this helps. Quote Link to comment Share on other sites More sharing options...
purpleshadez Posted October 23, 2009 Share Posted October 23, 2009 Not being funny, but that method doesn't make much sense to me. I'd do it like this: <html> <head><title>Capitals</title></head> <body> <?php $capitals_of_countries = array("England", "France", "Russia", "Portugal", "Spain", "Sweden", "Denmark", "USA", "Canada", "Australia", "Japan", "Italy", "Mexico", "Czech Republic", "Germany", "Ireland"); $country_capital = array("London", "Paris", "Moscow", "Lisbon", "Madrid", "Stockholm", "Copenhagen", "Washington", "Montreal", "Brisbane", "Tokyo", "Rome", "Mexico City", "Prague", "Berlin", "Dublin"); if (isset($_POST['posted'])) { echo "The capital of " . $capitals_of_countries[$_POST['country']] . " is <b>" . $country_capital[$_POST['country']] . "</b><hr />"; } ?> <form action="test.php" method="POST" /> <input name="posted" type="hidden" value="true" /> What Country do you want to know the capital of? <select name="country"> <?php $tot = sizeof($capitals_of_countries); for ($counter = 0; $counter < $tot; $counter++) { echo '<option value="' . $counter . '">' . $capitals_of_countries[$counter] . '</option>'; } ?> </select><br /><br /> <input type="submit" value="Find Capital" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
stublackett Posted October 23, 2009 Author Share Posted October 23, 2009 Thanks for the replies guys. Nightslyer that tip sounds like it will come very much in handy, Thanks All works now, gonna take a look at that method too Purpleshadez Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 23, 2009 Share Posted October 23, 2009 An even better solution would be to simply crate an associative array of capitols to country: $capsToCountries = array("London" => "England", "Paris" => "France", /* etc */); foreach($capsToCountries as $cap => $country) { echo "The capitol of $country is $cap"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.