[英]PHP / JavaScript: How to pass variable from one page to another
I am new to PHP and hope someone can help me with this. 我是PHP新手,希望有人可以帮助我。
I have a page ( index.php
) where users can select a language. 我有一个页面(
index.php
),用户可以在其中选择一种语言。 If no language is selected than the page URL is just index.php which defaults to English. 如果没有选择语言,则页面URL仅是index.php,默认为英语。 If they select a language than the page gets reload and the variable
?lang=xy
is added to the URL which defines the page language. 如果他们选择一种语言,则页面将被重新加载,并且变量
?lang=xy
被添加到定义页面语言的URL中。 Eg for German the page URL would then be index.php?lang=de
. 例如德语,则页面URL为
index.php?lang=de
。
So far everything works as intended. 到目前为止,一切都按预期进行。
Now I have a navbar on the index page that allows to navigate to other pages which are all saved as separate php files (like page1.php, page2.php, page3.php
etc.). 现在,我在索引页面上有一个导航栏,该导航栏允许导航到所有另存为单独的php文件的页面(例如
page1.php, page2.php, page3.php
等)。
How can I manage that if someone has selected a language on the index page and then navigates to another page that the language variable is passed along to all other pages ? 如果有人在索引页面上选择了一种语言,然后导航到另一个语言变量传递到所有其他页面的页面, 我该如何管理 ? Eg for German the other pages shoud be
page1.php?lang=de, page2.php?lang=de, page3.php?lang=de
etc. 例如,对于德国的其他页面是768,16
page1.php?lang=de, page2.php?lang=de, page3.php?lang=de
等。
I know I can use $_GET["lang"]
on each page to fetch the variable but couldn't find a way to pass this on from the index page to other pages using PHP or JavaScript/jQuery, ideally in a way that I can set this more general instead of separately for every single link. 我知道我可以在每个页面上使用
$_GET["lang"]
来获取变量,但是找不到使用PHP或JavaScript / jQuery将其从索引页面传递到其他页面的方法,理想情况下,我可以将其设置为更通用,而不是为每个链接单独设置。
Note: 注意:
I am using undordered lists with standard links to create my navbar, eg: 我使用带有标准链接的无序列表来创建导航栏,例如:
<li><a href="page1.php">Page1</a></li>
Can someone help me with this ? 有人可以帮我弄这个吗 ?
Many thanks in advance. 提前谢谢了。
You can use cookies to do so 您可以使用Cookie来这样做
setcookie(name, value, expire, path, domain, secure, httponly);
ie: 即:
setcookie('language', 'german', 60000,"/");
and then check this wherever with 然后在任何地方检查
$_COOKIE["language"]
http://php.net/manual/en/features.cookies.php reference http://php.net/manual/zh/features.cookies.php参考
you can use sessions to pass variables to any page,it's more secure than cookies as it's a server side, 您可以使用会话将变量传递到任何页面,因为它是服务器端,所以它比cookie更安全,
example: 例:
After Posting Data: 发布数据后:
<?php
session_start();
if(isset($_POST)){
$var_name=$this->db->real_escape_string($_POST['input_name']);
$_SESSION['var1_sess']=true;
$_SESSION['var1_sess']=$var_name;
}
?>
note :session_start() must be in the first line of the file. 注意 :session_start()必须在文件的第一行。
If you are using to select languages, just add onchange event to then submit the form to index.php 如果您要选择语言,只需添加onchange事件,然后将表单提交给index.php
<form action="index.php">
<select name="lang" onchange="javascript:this.form.submit()">
<option value="en">English</option>
<option value="xy">XY</option>
</select>
</form>
and in the same page 并在同一页面
<?php
$lang = $_GET['lang'];
if(empty($lang)){
$lang = "en";
}
?>
Then pass the $lang to where you want to 然后将$ lang传递到您想要的位置
<li><a href="page1.php?<?php echo $lang; ?>">Page1</a></li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.