从文本框中获取值并将其保存在文本文件中或将其发送到PHP中的电子邮件地址

时间:2015-04-27 11:28:48

标签: php html

我是PHP和HTML的初学者,基本上我想做的就是有几个文本框,按下提交按钮后,在那些文本框中引入的文本我想保存在文本文件中或者被发送到电子邮件地址。你能帮我吗?我真的不知道如何开始这个项目。

谢谢!

3 个答案:

答案 0 :(得分:2)

  

复制粘贴并查看结果

<!DOCTYPE HTML>
    <html>
        <head>
            <title>Example</title>
            <style>

            </style>
        </head>
        <body>
            <form method="post">
                <pre>
                    Name <input type="text" name="name">
                </pre>
                <pre>
                    Email <input type="text" name="email">
                </pre>
                <pre>
                    <input type="submit" value="Submit" name="submit">
                </pre>
            </form>
        </body>
    </html>


    <?php
    $to = "somebody@example.com";
    $subject = "Subject of your email goes here";
    $txt = "Body of your email goes here!";
    $headers = "From: webmaster@example.com" . "\r\n" .
        "CC: somebodyelse@example.com";

    if(isset($_POST['submit'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        if($name != '' || $email != ''){
            mail($to,$subject,$txt,$headers); // Here your email is being sent.
            $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); // Here your file is being opened if it doesn't exist so it will create it first
            fwrite($myfile, "Name :".$name."&nbsp;&nbsp;&nbsp;&nbsp;"."Email:". $email); // Here we are wirting file Name and email from the textboxes
            fclose($myfile);//closing the file
        }
    }
    ?>

答案 1 :(得分:1)

你应该尝试一下,如果你有任何问题,那就来这里吧。我可以用任何方式告诉你一些方法来做到这一点

这里有一些链接,您可以通过电子邮件发送link 1 link2 link 3 link form

创建一个简单的PHP表单

将内容输入文件阅读here

基本上代码是

<?php 
$action=$_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
    { 
    ?> 
    <form  action="" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="action" value="submit"> 
    Your name:<br> 
    <input name="name" type="text" value="" size="30"/><br> 
    Your email:<br> 
    <input name="email" type="text" value="" size="30"/><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30"></textarea><br> 
    <input type="submit" value="Send email"/> 
    </form> 
    <?php 
    }  
else                /* send the submitted data */ 
    { 
    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
    $message=$_REQUEST['message']; 
    if (($name=="")||($email=="")||($message=="")) 
        { 
        echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
        } 
    else{         
        $from="From: $name<$email>\r\nReturn-path: $email"; 
        $subject="Message sent using your contact form"; 
        mail("youremail@yoursite.com", $subject, $message, $from); 


        echo "Email sent!"; 
        } 
    }   
?> 

将内容放入文件的代码是

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

答案 2 :(得分:0)

这可以帮助你开始

<form name=myform method=post action="same_page.php">
    <input type="text" name=username value="<?php echo $_POST['username'] "?>
    <input type="text" name=email value="<?php echo $_POST['email']?> >
    <input type="submit" value=submit>
    </form>
    <?php 
    $username = $_POST["username"]; //Email address you want it to 'appear' to come 
    $email = $_POST["email"]; 

    if(strlen($username) && strlen($email))
    {
         $mailText ="The Contact Details: <br>";
    }
                  $mailText=$mailText."<table border=1 cellspacing=0 cellpadding=0>";
                      while(list($Key, $Val)= each($_POST))
                       {
                        $mailText=$mailText."<tr><td width=50%>"; 
                        $mailText=$mailText."<b>".$Key."</b></td>";
                        $mailText=$mailText."<td width=50%>";   
                        $mailText=$mailText.$Val;
                        $mailText=$mailText."</td></tr>";  

                 }

    $to = "youremailid.com";
    if(strlen($username) && strlen($email))
    {
    $subject = "your subject"; //Subject Line
      $headers .= "From:yourmail.com\n";
        $headers .= "X-Sender:  yourheader.com\n";
         $headers .= "Content-Type: text/html; charset=iso-8859-1\n";         // Mime type
    $mailforms = mail($to, $subject, $mailText, $headers);
    }
    ?>