我需要将html5下拉菜单的结果存储到变量中并将其与PHP一起使用。为简化起见,我有一个包含2种语言选择的下拉列表。当我选择一种语言时,结果应存储在变量中。稍后PHP将使用该变量来显示正确的语言。
目前我已在代码中直接设置变量。见第27行[$ lang =" se&#34 ;;]。
任何直接的方法将变量从html5传递给PHP?以下是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<!--HTML form (start)-->
<div class="lang-sel">
<form action="/action_page.php">
<select name="cars">
<option id="en" value="en">English</option>
<option id="se" value="se">Swedish</option>
</select>
</form>
</div>
<!--HTML form (end)-->
<!--PHP - language selector (start)-->
<?php
$lang = "se"; /*set variable*/
if ($lang =="en") {
echo "language set to english";
/* include 'content-en.html';*/
} else if ($lang =="se")
{
echo "language set to swedish";
/*include 'content-sv.html';*/
} else {
echo "You did not set lang to either english nor swedish";
}
?>
<!--PHP - language selector (end)-->
</div>
</body>
</html>
答案 0 :(得分:0)
<form method="post" action="./app.php">
<select name="language">
<option id="en" value="en">English</option>
<option id="se" value="se">Swedish</option>
</select>
<input type="submit" />
</form>
使用$_POST['language']