duan88014 2013-11-13 20:16
浏览 51
已采纳

如何将网站表单数据(php)保存到我的服务器?

I am setting up a website on my own home server using a website template. The contact form uses php and the php mail() function to email to info entered in the contact sheet, but I don't have a local smtp server to use in the mail(). So I want to alter the code to instead save the submitted info to a text file. If there is a possible way to do this can someone point me in the correct direction. my current html code and php code are as follows: HTML:

    <form action="mailer.php" id="contact_form" method="post">

            <ul class="form">               

                <li class="short">
                    <label>First Name<span class="required"></span></label>
                    <input type="text" name="first" id="first" value="First Name" class="requiredField" onblur="if(this.value == '') { this.value = 'First Name'; }" onfocus="if(this.value == 'First Name') { this.value = ''; }" />
                </li>

                <li class="short">
                    <label>Last Name<span class="required"></span></label>
                    <input type="text" name="last" id="last" value="Last Name" class="requiredField" onblur="if(this.value == '') { this.value = 'Last Name'; }" onfocus="if(this.value == 'Last Name') { this.value = ''; }" />
                </li>

                <li class="long">
                    <label>Email Address<span class="required"></span></label>                  
                    <input type="text" name="email" id="email" value="Email Address" class="requiredField email" onblur="if(this.value == '') { this.value = 'Email Address'; }" onfocus="if(this.value == 'Email Address') { this.value = ''; }" />                        </li>

                <li class="short">
                    <label>Company Name</label>
                    <input type="text" name="company" id="company" value="Company Name" class="requiredField" onblur="if(this.value == '') { this.value = 'Company Name'; }" onfocus="if(this.value == 'Company Name') { this.value = ''; }" />
                </li>

                <li class="short">
                    <label>Telephone Number</label>
                    <input type="text" name="phone" id="phone" value="Telephone Number" class="requiredField" onblur="if(this.value == '') { this.value = 'Telephone Number'; }" onfocus="if(this.value == 'Telephone Number') { this.value = ''; }" />
                </li>

                <li class="textarea">
                    <label>Message<span class="required"></span></label>
                    <textarea name="message" id="message" rows="20" cols="30"></textarea>
                </li>

                <li class="button"><input name="submitted" id="submitted" value="Submit" class="submit" type="submit" />
                </li>

PHP:

    <?php
    $email_to = "michaelggerhart@gmail.com";
    $first = $_POST["first"];
    $last = $_POST["last"];
    $email = $_POST["email"];
    $company = $_POST["company"];
    $phone = $_POST["phone"];
    $message = $_POST["message"];
    $text = "NAME: $first    $last <br>
     EMAIL: $email<br>
     COMPANY: $company<br>
     TELEPHONE NUMBER: $phone<br>    
     MESSAGE: $message";
    $headers = "MIME-Version: 1.0" . "
"; 
    $headers .= "Content-type:text/html; charset=utf-8" . "
"; 
    $headers .= "From: <$email>" . "
";
    mail($email_to, "Message", $text, $headers);
    ?>   
  • 写回答

3条回答 默认 最新

  • doukao8851 2013-11-13 20:27
    关注

    You should use fwrite function of php! And it would be better to put the content in a html file, because you can use <br>, style the page and access it from your browser

    <?php
    $first = $_POST["first"];
    $last = $_POST["last"];
    $email = $_POST["email"];
    $company = $_POST["company"];
    $phone = $_POST["phone"];
    $message = $_POST["message"];
    $text = "NAME: $first    $last <br>
     EMAIL: $email<br>
     COMPANY: $company<br>
     TELEPHONE NUMBER: $phone<br>    
     MESSAGE: $message<br><hr><br><br><br>";
     $file = fopen("contactrequests.html","a+");
     fwrite($file, $text);
     fclose($file);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?