Skip to content
Advertisement

How could i change the location of the page in a select elment in PHP by their values?

I have a Select element with some option values that i queried from the data base as you can see in the code. Now what i want to do is whenever i select a option value i want to be able to redirect my page to another page. The problems are i have to refresh first the page to get to the desired page and when i want to return to the main page index.php i cant geet back to the location ? The code hope will explain also. Thank you for the help that you cna offer me.

       <select name="opt_lable">

            <?php foreach ($sidePosts as $post) {?>


            <option value="<?php echo $post['label'];?>"><?php echo $post['label'];?></option>
                <?php if($post['label']=='home'){
                    header('location:index.php');
                }elseif ($post['label']=='world') {
                   header('location:world.php');
                };?>


            <?php };?>

        </select>

Advertisement

Answer

You can do this with jQuery.

<select id="opt_lable" name="opt_lable">
   <?php
    foreach ($sidePosts as $post) {
      echo "<option value='{$post['label']}'">{$post['label']}</option>";
    }
   ?>
</select>

<script>
 $('#opt_lable').on('change', function() {
   if(this.value == 'home'){
      window.location.href="index.php";
   }
   else if(this.value == 'world'){
      window.location.href="world.php";
   }
 });
</script>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement