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

时间:2014-11-21 14:05:33

标签: php forms redirect post

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

$billingaddress = $post['firstname']

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

header(Location: www.google.com);

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

2 个答案:

答案 0 :(得分:0)

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

//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;

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

答案 1 :(得分:0)

我不确定这是不是你要问的问题,但是在互联网无效的某个地方,我设法找到一个好的表单设置。您需要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>

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