0
In a program for weather forecast by city selection I am using a Dropbox that works well, but the system is not fully automatic. When it starts, I need to select a city to show the forecast. What I intended is that the list did not have "Select city" and opened soon with the forecast for the first city (Caldas da Rainha). Thank you very much for any hint (I am beginner!) :-)
The site is this [1]: http://www.meteocaldas.com/previsao3.php
The code is this: `
<?php
$arr = ["Caldas da Rainha", "Lisboa", "Peniche" ];
if( $_POST['city']){
$city=$_POST['city'];
}
?>
<form name="f" id="a" method="post" action="">
<select id="city" name="city" onchange="this.form.submit()" >
<option value="0">Selecione cidade</option>
<?php
foreach ($arr as $a){
if($a == $city){
echo "<option value='{$a}' selected >$a</option>";
}else{
echo "<option value='{$a}' >$a</option>";
}
}
?>
</select>
</form>
But then you should already open the page with the data of "Caldas da Rainha". Your form makes a POST. It makes no sense to make an automatic POST when opening the page. You can already show at the top of the page the data you want.
– Sam
Thank you very much, Sam. I understand what you said about not making an automatic post when opening the page, but on my site [link] http://www.meteocaldas.com/previsao.php [/link] there is this case similar and it works. I will try to analyse it further and find out why it is not displaying data when it opens the page [link] http://www.meteocaldas.com/previsao3.php [/link]
– MeteoCaldas
Looking further, it seems to be because $city that is needed to show the data is only available after the first click on one of the cities. Will there be a way for $city to be available as soon as the page opens in the first city and without having to click? Thank you very much.
– MeteoCaldas
Problem solved. To be fully automatic and show the forecast when you open the page you only need to add $city = $arr[0] before if( $_POST['city']. Thank you very much once again.
– MeteoCaldas