Automatic selection in drop-down menu

Asked

Viewed 32 times

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.

  • 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]

  • 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.

  • 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.

1 answer

0

You can just take the and your logic that will be solved. Remove this excerpt:

<option value="0">Selecione cidade</option>

Will stay like this:

<?php
$arr = ["Caldas da Rainha", "Lisboa", "Peniche" ];
?>

<form name="f" id="a" method="post" action="">
<select id="city" name="city" onchange="this.form.submit()" >                      
  <?php
   foreach ($arr as $a){
        echo "<option value='{$a}'>$a</option>";
   }

   ?>
  
   </select>
</form>

  • 1

    Thank you very much Felipe! I removed the <option value="0">Select city</option> and the thing got better because now the menu shows the first city. There is still a problem: the forecast for this city does not start automatically. To see the forecast of the first city I have to click on another city and then return to the first city to show the forecast. Is there any way around this problem? Once again special thanks Philip, for his response has been so quick and so helpful.

  • 1

    Problem solved. To be fully automatic you only needed to add $city = $arr[0] before if( $_POST['city']. Thank you very much once again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.