简体   繁体   中英

HTML change site using select tag and php

I try to make a website and I'd like to make a select tag where you can change your language. I mean there will be some options which you can change. For example the default site language is English but you're want to change it to German, you can just select the german from the list. After that you click the SAVE button and the page will redirect you to indexgermany.html I tried to make a code but I am new in php so I don't know how to do this. By the way here is my code:

 <?php if(isset($_POST['submit'])) { if(isset($_POST['option'])) { if($_POST['option'] == Deutsch - DE) { header('Location: indexgerman.php'); } } } ?>
 <form method="POST" action="changelang.php"> <select select name="languages" id="langs"> <option for="english" name="english">English - EN</option> <option for="germany" name="germany">Deutsch - DE</option> </select><br> <input type="submit" class="langsave" name="submit" value="SAVE"> </form>

I`ll guess your scrict does not redirect you, if you changed the "lang".

If you use the following code it should work:

<?php
    if(isset($_POST['submit'])) {
        if(isset($_POST['option']))
        {
            if($_POST['option'] == 'Deutsch - DE') {
            header('Location: indexgerman.php');
            }
        }
    }
?>

You forgot to wrap the name of your option in quotes. This is required because you want to compare 2 strings.

You are missing value in HTML and $_POST needs to be equal to string, otherwise it just undefined value.

EDIT:

Also $_POST['option'] does not exist, I renamed select tag name to options.

<?php
if(isset($_POST['submit'])) {
  if(isset($_POST['option'])){
    if($_POST['option'] == "DE") {
       header('Location: indexgerman.php');
    }
  }
}
?>

<form  method="POST" action="changelang.php">
  <select select name="options" id="langs">
    <option for="english" name="english" value="EN">English - EN</option>
    <option for="germany" name="germany" value="DE">Deutsch - DE</option>
  </select>
  <input type="submit" class="langsave" name="submit" value="SAVE">
</form>
<?php

if(isset($_POST['submit'])) {
    $selected_lang = $_POST['languages'];
    if($selected_lang == "Deutsch - DE") {
        header('Location: indexgerman.php');
    }else{
        header('Location: indexenglish.php');
    }
}    
?>

First thing is when u get the options from Select tag, U put name associated with select tag not with option tag And U missed "" while comparing to string Values(Deutsch - DE).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM