简体   繁体   中英

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? 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.

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.

$_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 :

$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.

NB: whatever you would end up doing - do NOT pass the data to the receiving page as 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM