带有PHP帖子到.txt的HTML表单

时间:2015-10-02 01:16:38

标签: php html forms

我知道这是重复的,但我根本不懂PHP ..并且试图在时间紧迫的情况下学习不起作用我如何发布到文本文件?这是我试过的,我得到一个内部服务器错误......

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

这是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());
}
?>

我做错了什么......

2 个答案:

答案 0 :(得分:1)

也许我的英语不好,但我会尽力向你解释是最好的。

您应该像这样更改HTML:

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

此“提交”的名称不是必需的。表格也是如此。

然后,您的PHP文件应如下所示:

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

因为发布到您服务器的数据只是“电子邮件”。

祝你好运!

答案 1 :(得分:1)

以下是修复PHP帖子到T​​XT的解决方案

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>

在你的formfix.php中应该是这样的。

<?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');
}

?>

你会得到这个结果

我已经做了一个测试并且它已经完成了

enter image description here

请记下文件位置请注意。并且json.txt路径必须位于formfix.php。

问候:)