[英]Redirecting to another page conditionally and sending parameters through POST in PHP
I have a page which takes input data from users trying to create a new account on my website. 我有一个页面,它从用户尝试在我的网站上创建一个新帐户获取输入数据。 Although I want to check the values of the data before the page is redirected to send the input data to the database.
虽然我想在重定向页面之前检查数据的值,以将输入数据发送到数据库。 I have created a verification form which redirects back to the same page when the user clicks submit, so as to check the values, but if all the values are okay, then I want to send the parameters inputted to another page which will input them in the database.
我创建了一个验证表单,当用户点击提交时重定向回同一页面,以便检查值,但是如果所有值都没问题,那么我想将输入的参数发送到另一个页面,然后输入它们数据库。 Is there a way to do this conditionally in PHP?
有没有办法在PHP中有条件地执行此操作? I've tried to implement it with a simple boolean checking if any of the parameters inputted are incorrect, but don't know how to send the params along with the redirection.
我试图用一个简单的布尔检查来实现它,如果输入的任何参数是不正确的,但不知道如何发送params以及重定向。
Here is a shortened version of the code with just some of the forms: 这是一个缩短版的代码,只有一些形式:
<?php
// Connection to database
include_once("createConnection.php");
$hasErrors = false;
$errorFName = $errorLName = $errorEmail = "";
$fName = $lName = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fName"])) {
$errorFName = "This field is required.";
$hasErrors = true;
}
else {
$name = validation($_POST["fName"]);
//name cannot contain any symbols or numbers
if (!preg_match("/^[a-zA-Z ]*$/",$fName)) {
$errorFName = "Only letters and white space allowed";
$hasErrors = true;
}
}
if($hasErrors==false){
**???**
}
function validation($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
?>
<form class="loginForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table class="loginTable">
<tr>
<td><label for="newDetail">First Name: </label>
<input type="text" class="formBox" value="" id="newDetail" name="fName">
<?php echo $errorFName;?>
</td>
</tr>
<tr>
<td><label for="newDetail">Password: </label>
<input type="text" class="formBox" value="" id="newDetail" name="pword">
</td>
</tr>
<tr>
<td><button type="submit" class="loginButton" name="submitNew">Submit</button></td>
</tr>
</table>
</form>
Thanks! 谢谢!
You could store the $_POST variables in sessions. 您可以在会话中存储$ _POST变量。
$_SESSION["firstname"] = $_POST["fname"];
Then redirect to the required page 然后重定向到所需的页面
header("location: welcomepage.php");
Then grab the variables from the session and process, maybe insert to database for future use. 然后从会话和进程中获取变量,可以插入数据库以备将来使用。
$firstname = $_SESSION["firstname"];
您可以使用<meta http-equiv="refresh">
手动重定向:
echo '<meta http-equiv="refresh" content="0, welcomepage.php?params=params">';
I'm not quite sure why you would like to pass parameters to other page - it qould be much easier to process them right here. 我不太确定你为什么要将参数传递给其他页面 - 在这里处理它们要容易得多。 If it is a general messines of the code you dislike - you can pass data to a function or a method from an included file.
如果它是您不喜欢的代码的一般信息 - 您可以将数据传递给包含文件中的函数或方法。
However, if it is really necessary to save data from a separate page I would recommend sending them via cURL
: 但是,如果确实需要从单独的页面保存数据,我建议通过
cURL
发送它们:
$post = array(/*put all your variables here*/);
$params = http_build_query($post);
$url = 'http://your.domain.com/receiver.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($curl);
This way anything you would output on the receiving page will end up in $result
, so you can confirm to a user that his/her data was saved. 这样,您在接收页面上输出的任何内容都将以
$result
结尾,因此您可以向用户确认他/她的数据已保存。
NB: whatever you would end up doing - do NOT pass the data to the receiving page as GET. 注意:无论你最终做什么 - 不要将数据作为GET传递给接收页面。 It is insecure and every time someone does this a puppy dies!
这是不安全的,每当有人这样做,一只小狗就会死!
More info on curl: http://php.net/manual/en/book.curl.php 有关curl的更多信息: http : //php.net/manual/en/book.curl.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.