如何将表单输入发送到文本文件和/或数据库


How do I send my form inputs to a text file and/or to a database?

所以我做了一个"即将到来"的页面,我有一个注册时事通讯的表单,但我似乎不能得到正确的编码。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Coming Soon to a Browser Near You</title>        
        <link rel="stylesheet" type="text/css" href="style.css" />
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js'></script>
        <script type='text/javascript' src='javascripts/jquery.tipsy.js'></script>
        <script type='text/javascript'>
        $(function() {
            $('#tipsy').tipsy({fade: true, gravity: 's'});
        });
        </script>
        <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="wrapper">
<!--            <img src="images/logo.png" alt="YourLogo" title="YourLogo"/>
-->         <h1>SirAethon</h1>
            <div class="hr"></div>
            <h3>Coming Soon to a Browser Near You</h3>
            <p>Unfortunately, we’re not quite ready yet. <strong> But, you can see our progress below:</strong></p>
            <section class="progress">
                <div class="progress-bar-container" id="tipsy" title="8% Complete"> <!-- Edit this title for the tooltip pop up -->
                    <article class="progress-bar" style="width:8%"  ></article> <!-- Edit the width percentage value to indicate progress -->
                </div>
                <article class="txt-launch-day-hat"></article>
            </section>
            <div class="hr"></div>
            <section class="mailing-list">
                <h2>Want to be the first to know when we're ready?</h2>
                <form>
                    <input type="text" value="your@email.com" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">
                    <input type="submit" value="Subscribe"> 
                </form>
            </section><div class="clear"></div>
            <div class="hr"></div>
            <p class="credit">Designed by <a href="http://siraethon.com">SirAethon</a></p>
        </div>
    </body>
</html>

那是我的index.html页面。我希望表单部分能够接收电子邮件并确认它是一封实际的电子邮件,然后将该电子邮件放入文本文件中,以便我稍后可以接收电子邮件,以便我可以立即发送大量电子邮件。

        <section class="mailing-list">
            <h2>Want to be the first to know when we're ready?</h2>
            <form>
                <input type="text" value="your@email.com" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">
                <input type="submit" value="Subscribe"> 
            </form>
        </section>

上面是邮件列表部分以及表单。

任何帮助都是非常感谢的!提前谢谢你!)

EDIT1:所以我已经让它工作了,但现在它说"13字节写入文件",一旦你点击订阅按钮,我怎么修复它从说这个,使它说别的东西,如感谢您订阅?

我使用了这些编码:

形式:

        <form action="php/signup.php" method="POST">
            <input name="field1" type="text" value="your@email.com" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">
            <input type="submit" name="submit" value="Save Data">
        </form>

signup.php:

<?phppublic_html/the-fam.com/php/signup.php
if(isset($_POST['field1'])) {
    $data = $_POST['field1'];
    $ret = file_put_contents('emails.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');
}

EDIT2:所以我试图修复它说字节写入文件,现在它不显示任何东西,也不是写入文件:"$ret字节写入文件"我删除了那部分,然后替换了它,什么也没有发生,但我把它替换回来,仍然没有发生

如果你想查看你自己的网站它是http://the-fam.com

您的表单没有actionmethod,因此它不做任何事情。您可能需要method="post"action="some_script.php",其中some_script.php文件将处理您对数据的任何操作(验证,保存到文件,保存到数据库等)。您还需要name="email"或沿着这些行作为文本输入的属性。这样你就可以通过$_POST['email']

这应该让您开始,您将需要在服务器上运行站点,如Wamp。该页面应该提交给自己,让你继续。一旦提交,它将检查电子邮件是否有效,并将其写入文件。

<?php 
if( isset( $_POST ) )
{
  $email = test_input($_POST["email"]);
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $emailErr = "Invalid email format";
  }
  else
  {
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "$email'n";
    fwrite($myfile, $txt);
    fclose($myfile);
  }    
}
?>
<form method="post" action="" >
  <input type="text" value="your@email.com" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">
  <input type="submit" value="Subscribe"> 
</form>