我有一个php页面有一个按钮(index.php),这个php页面包含变量,我想在另一个php脚本(script.php)在另一个文件中使用它们,但同时,我希望用户被重定向到一个html页面(success.html)我怎么做呢?
在index.php中设置<form action="script.php" method="post">
,并将这行代码放在script.php中的脚本末尾:
header('location: 'success.html']);
如何将变量传递给script.php?
如果将此脚本放入script.php:
foreach ($_POST as $key => $value){
$$key=$value;
}
然后它将为所有具有name=""
属性的表单元素创建变量,并为它们分配各自的值。
的例子:
<input type="text" name="firstname" value="John">
将创建变量
$firstname = 'John';
这很简单,有几种方法可以做到这一点,您可以使用会话或设置一个表单来发布结果。在index。php中可以这样写
<?php
session_start()
$_SESSION['foo'] = 'bar';
header("location:success.html");
?>
$_SESSION['foo'];
<?php
echo $_SESSION['foo']; // will output bar