如何发布表单数据,收集数据生成,然后重定向到页面


How post form data, collect data generate and then redirect to a page?

我有一个表格,里面有 POST 的数据,然后我需要收集这些数据,我知道如何使用

$billingaddress = $post['firstname']

然后在收集帖子数据后,我需要重定向到设置的URL

header(Location: www.google.com);

我只是不确定哪些文件需要包含我需要收集数据并同时重定向的代码(它将转到 sagepay)

这就是你需要使用它的方式。

//Posted data stored in $_POST global array
$billingaddress = $_POST['firstname'];
//Do what you want.
//Need to add quotes around the parameter
//Need to use full URL started with the schema (http|https).
header("Location: http://www.google.com");
//Terminate the script after redirect.
exit;

注意:您确定要将名字收集到名为账单地址的变量中吗?这是一种不好的做法。调用你的变量它所包含的内容,例如:$firstName

我不确定这是否是你要问的,但是从互联网空白处的另一个问题中,我设法找到了一个很好的表单设置。您需要 3 个文件:nameofform.phpnameofredirectpage.html 和存储数据的位置。这是我的一个网站的示例。

表单页面

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";
    /* This section determines the error messages if there is a blank field. */
    if(empty($_POST['formSteam']))
    {
        $errorMessage .= "<li>You need to put your Steam ID!</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>Please put your first and/or lastname!</li>";
    }
    if(empty($_POST['formAge']))
    {
        $errorMessage .= "<li>Please tell us your age! This is just for reference.</li>";
    }
    if(empty($_POST['formPos']))
    {
        $errorMessage .= "<li>You need to give us what position you want!</li>";
    }
    if(empty($_POST['formApplication']))
    {
        $errorMessage .= "<li>Do I have to explain this? Please explain why you want to be apart of our staff!</li>";
    }
    /* This section just labels the variables. */
    $varSteam = $_POST['formSteam'];
    $varName = $_POST['formName'];
    $varAge = $_POST['formAge'];
    $varApplication = $_POST['formApplication'];
        $varPos = $_POST['formPos'];
   /* This is where the magic happens. It's saying that if there is no errors and all the fields are filled in, it will open [or create] the file mydata.csv, which then writes each variable; `$varSteam`, `$varName`, `$varAge`, and so on. Grabbing what the fields content were, it will post the data in the specified order on the [newly created] file, then close the document. Finally, it redirects the user to the selected page, in this case "success.html". */
    if(empty($errorMessage)) 
    {
        $fs = fopen("mydata.csv","a");
        fwrite($fs,$varSteam . ", " . $varName . ", " . $varAge . ", " . $varPos . ", " . $varApplication . "'n");
        fclose($fs);
        header("Location: success.html");
        exit;
    }
}
?> 
<html>
<body>
    <?php
        if(!empty($errorMessage)) 
        {
            echo("<p>There was an error with your form:</p>'n");
            echo("<ul>" . $errorMessage . "</ul>'n");
        } 
    ?>
    /* This is the field ; change accordingly! */
    <form action="application.php" method="post">
        <p>
            Whats your Steam ID? Find out <a href="http://www.steamidfinder.com">here!</a><br>
            <input type="text" name="formSteam" maxlength="50" value="<?=$varSteam;?>">
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="50" value="<?=$varName;?>">
        </p>                
        <p>
                How old are you?<br>
                <input type="text" name="formAge" maxlength="2" value="<?=$varAge;?>">
        </p>
        <p>
                What position are you applying for?<br>
                <select name="formPos" value="<?=$varPos;?>">
                            <option value="">Pick an Option</option>
                            <option value="moderator">Moderator</option>
                            <option value="coder">Coder</option>
                            <option value="admin">Administrator</option>
                            <option value="other">Other</option>
                        </select>
        </p>
        <p>
            Why do you want to apply for PossessedGaming Staff? *NOTE: If the above option was "OTHER", please include that "OTHER" position.<br>
            <textarea name="formApplication" maxlength="3000" style="width:325px;height:150px;" value="<?=$varApplication;?>"></textarea>
        </p>
        <input type="submit" name="formSubmit" value="Submit">
    </form>
</body>
</html>

重定向的页面

<html>

  <body>
        <center>
          <h1>Success!</h1>
          <p>Your application will be reviewed by the staff ASAP. Thanks for applying!</p>
      </body>
    </html>

使用的示例是我网站的员工申请表;您可以相应地修改它。同样,不确定这是否是您想要的,但是...是的。