我正在关注this教程,在我的网站上使用多种语言,但是我试图在POST后切换语言,但实际上在点击POST按钮两次时会发生语言切换。
此代码在页面开头调用。
<?php
if (!isset($_SESSION['lang'])){
$_SESSION['lang'] = 'ENG';
require "lang/ENG.php";
}
else {
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "EST":
require "lang/EST.php";
break;
case "ENG":
require "lang/ENG.php";
break;
default:
require "lang/ENG.php";
}
}
?>
提交表单时调用此代码:
if(isset($_POST['selectlang'])){
$lang = $_POST['country'];
//Change language??
}
如果我单击两次POST按钮,则会切换语言,但不会在第一次尝试时切换。我需要改变什么?
编辑#2:现在根据答案添加修改后的HTML:
<?php
include('connection.php');
?>
<?php
if (!isset($_SESSION['lang'])){
$_SESSION['lang'] = 'ENG';
require "lang/ENG.php";
}
else {
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "EST":
require "lang/EST.php";
break;
case "ENG":
require "lang/ENG.php";
break;
default:
require "lang/ENG.php";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title><?php echo $lang['reklamatsioon_title'];?></title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/logo-nav.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="reklamatsioon.php">
<img src="logo.png" alt="">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="#"><?php echo $lang['link1'];?></a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<form action="" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label for="selector" class="col-sm-3 col-lg-3 control-label">Pick language</label>
<div class="col-sm-9 col-lg-9">
<span id="selector" class="input-group-btn">
<select id="country" name="riik" class="form-control">
<option <?php if($_POST['riik'] == "EST"){ echo "selected='selected'"; }?> value="EST">Eesti</option>
<option <?php if($_POST['riik'] == "ENG"){ echo "selected='selected'"; }?> value="ENG">English</option>
<option <?php if($_POST['riik'] == "FIN"){ echo "selected='selected'"; }?> value="FIN">Suomi</option>
<option <?php if($_POST['riik'] == "SWE"){ echo "selected='selected'"; }?> value="SWE">Rootsi</option>
<option <?php if($_POST['riik'] == "NOR"){ echo "selected='selected'"; }?> value="NOR">Norra</option>
</select>
</span>
<span class="input-group-btn">
<button class="btn btn-primary" name="selectlang" type="submit">Choose language!</button>
</span>
</div>
</div>
</form>
</div>
<?php
if(isset($_POST['selectlang'])){
$lang = $_POST['riik'];
$_SESSION['lang'] = $lang;
//Show code form and terms and conditions url!
echo "<div class='row'>";
echo "<form action='' method='post' class='form-horizontal' role='form'>";
echo "<div class='form-group'>";
echo "<label for='code' class='col-sm-3 control-label'>"."Code"."</label>";
echo "<div class='col-sm-6'>";
echo "<input type='number' id='code' name='code' placeholder='Code' class='form-control' autofocus>";
echo "<span class='help-block'></span>";
echo "</div>";
echo "</div>";
echo "<div class='form-group'>";
echo "<div class='col-sm-6 col-sm-offset-3'>";
echo "<button type='submit' class='btn btn-primary btn-block' name='gobutton'>Continue to form</button>";
echo "</div>";
echo "</div>";
echo "</form>";
echo "</div>";
}
if(isset($_POST["gobutton"])){
$codeVal = $_POST['code'];
$brokenCode = false;
if(strlen($codeVal) < 9){
$codeQuery = mysql_query("SELECT clientNumber FROM client WHERE clientNumber = '$codeVal'",$connection) or die(mysql_error());
if (mysql_num_rows($codeQuery) == 1){
header('location: ankeet.php?code='.$codeVal);
}
else {
$brokenCode = true;
}
}
else {
$brokenCode = true;
}
if ($brokenCode){
echo "<div class='row'>";
echo "<form action='' method='post' class='form-horizontal' role='form'>";
echo "<div class='form-group'>";
echo "<label for='code' class='col-sm-3 control-label'>"."Code"."</label>";
echo "<div class='col-sm-6'>";
echo "<input type='number' id='code' name='code' placeholder='Code' class='form-control' autofocus value='".$codeVal."'>";
echo "<span class='help-block'></span>";
echo "</div>";
echo "</div>";
echo "<div class='alert alert-danger col-sm-6 col-sm-offset-3'>Such code does not exist!</div>";
echo "<div class='form-group'>";
echo "<div class='col-sm-6 col-sm-offset-3'>";
echo "<button type='submit' class='btn btn-primary btn-block' name='gobutton'>Continue to form</button>";
echo "</div>";
echo "</div>";
echo "</form>";
echo "</div>";
}
}
?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
答案 0 :(得分:0)
首先你需要:
if (isset($_POST['country'])) {
$_SESSION['lang'] = $_POST['country'];
}
所以语言可以切换。
然后你需要加载你的设置。
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "DE":
require "lang/de.php";
break;
case "ENG":
default:
require "lang/eng.php";
}
没有什么,你不想要的事情会发生
您可以优化其他一些内容,但现在应该可以解决您的问题。
您应该从不根据网站用户可以设置的变量要求或包含文件!
编辑:
我现在已经看到了您发布的代码:它无法工作,因为您先包含文件并稍后切换会话值。那时文件已经加载。你需要按正确的顺序做事,只需这样想:
1)初始化请求
2)进行配置
3)输出html代码
再次修改:
您的代码中存在许多不良做法,我实际上不知道从哪里开始。
1)不要在SQL查询中包含未经过滤的用户输入,这样您就可以被知识最少的任何人攻击。人们会尝试 - 相信我。您必须转义在SQL查询中使用的值。使用参数绑定会好得多,请google“php mysql pdo parameter binding”
2)在渲染你的选项时,你应该考虑使用循环来避免重复自己,请谷歌“干不要重复自己”
3)打印输出后,您无法发送http标头。由于http协议的性质,这不起作用。首先将标题发送到浏览器,然后是内容,基本上它看起来像:
HeaderLineOne: HeaderValue
HeaderLineTwo: HeaderValue
Content
3)您应该先处理请求,然后再输出代码。所以把你的PHP逻辑放在文件的顶部(句柄输入,配置等),并在最后输出你的响应。这样你就可以做出正确的反应,不要把你的观点与你的逻辑混淆。基本上就像:
<?php
// bootstrap application
require_once "connection.php";
require_once "other_include.php";
// configure
$language = "ENG";
if (isset($_SESSION['language'])) {
$language = $_SESSION['language'];
}
// allow user override for language
if (isset($_POST['country'])) {
$language = $_POST['country'];
// make change persistent
$_SESSION['language'] = $language;
}
// include translation labels
switch($language) {
case "DE":
require "lang/de.php";
break;
case "ENG":
default:
require "lang/eng.php";
}
// handle user request
if (isset($_POST['someValue'])) {
// handle user input, make database calls etc
// this is where your logic goes
}
// finally render response
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?= $lang['title'] ?></title>
</head>
<body>
<h1><?= $lang['title'] ?>
</body>
</html>