简体   繁体   中英

HTML Form with a PHP post to .txt

I know this is repetitive but i do not know PHP at all.. And trying to learn in a time crunch is not working how can i post to a text file? This is what i tried and i get an internal server error...

    <div id="signupform" class="sb-search clearfix">
    <form method="post" id="contact" class="clearfix" action="/comingsoon/php/formfix.php" name="email">
        <input class="sb-search-input" placeholder="Enter email ..." type="text" value="" name="email">
        <input class="sb-search-submit" value="" type="submit" name="email">
        <button class="formbutton" type="submit"><span class="fa fa-envelope-o"></span></button>
    </form>
</div>

this is the PHP formfix.php...

<?php
if(isset($_POST['submit']))
{
    $email = $_POST['email'];
    $file = fopen("/comingsoon/json.txt",);
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}
?>

What am i doing wrong...

Maybe my English is not good, but I will try to explain is to you as my best.

You should alter your HTML like this:

<div id="signupform" class="sb-search clearfix">
<form method="post" id="contact" class="clearfix" action="/comingsoon/php/formfix.php">
    <input class="sb-search-input" placeholder="Enter email ..." type="text" name="email">
    <input class="sb-search-submit" type="submit" >
</form>

A name of this "submit" isn't necessary。And so is the form。

Then,your PHP file should look like this:

<?php
if(isset($_POST['email']))
{
    $email = $_POST['email'];
    $file = fopen("/comingsoon/json.txt",);
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}
?>

Because the data that post to your server is only "email".

Good Luck to you!

Here is the solution to fix your PHP post to TXT

HTML

<div id="signupform" class="sb-search clearfix">
        <form method="post" id="contact" class="clearfix" action="comingsoon/php/formfix.php"> <!-- I remove name="email"-->
            <input class="sb-search-input" placeholder="Enter email ..." type="text" value="" name="email">
            <input class="sb-search-submit" value="" type="submit" name="email1">
            <button class="formbutton" type="submit"><span class="fa fa-envelope-o"></span></button>
        </form>
    </div>

In your formfix.php should be like this.

<?php
if(isset($_POST['email']) && isset($_POST['email1'])) {
    $data = $_POST['email'] . '-' . $_POST['email1'] . "\n";
    $ret = file_put_contents('json.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

?>

And you will get this result

I already do a test and it's work

在此处输入图片说明

Take a note please be aware on file location. And json.txt path must be at formfix.php .

Regards :)

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