繁体   English   中英

我如何使此联系​​表有效?

[英]How Do I Make this contact form work?

我无法编辑此“与我们联系”表单代码,无法将消息传递到我的邮箱中。

假设我想将邮件传递到我的电子邮件:doherty@noob.com,如何编辑此代码来完成任务?

 <div class="conatct-form"> <ul class="form"> <li> <input type="text" class="text" value="Name*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name*';}" > </li> <li> <input type="text" class="text" value="Email*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email*';}" > </li> <li> <textarea value="Message*:" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Message*';}">Message*</textarea> </li> <div class="sub-button"> <input type="submit" value="SEND"> </div> </ul> </div> 

我已经优化了您的代码并添加了一些PHP代码

PHP代码

 <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (isset($name, $email, $message)) {
        // Insert into database
        $query = mysql_query("INSERT INTO contacts ...");

        if ($query) {
            // send email using phpmailer, sendmail, ...
            mail($mail, 'Subject', $message, '...');
        }
    }
?>

的HTML

<form id="contact-form" method="post">
    <ul>
        <li>
            <!--
                Substituting the code below
                <input type="text"
                       value="Name *"
                       onfocus="this.value=''"
                       onblur="if (this.value == '') {this.value = 'Name *'}" />

                use the placeholder attribute instead
            -->
            <input type="text" name="name" placeholder="Name *" />
        </li>
        <li>
            <input type="text" name="email" placeholder="Email *" />
        </li>
        <li>
            <textarea name="message" placeholder="Message *"></textarea>
        </li>
        <li>
            <input type="submit" value="Send" />
        </li>
    </ul>
</form>

您需要将HTML输入包装在一个元素中,以便正常工作。

因此,您将需要以下标记:

<form action='url_to_submit_to' method='POST' enctype='application/x-www-form-urlencoded'>

 <!-- Rest of your code here -->

</form>

有关<form>标记的格式和参数的更多详细信息,请参见Mozilla文档:

https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/form

由于内容未包装在表单标签内,因此此处未提交。 所有内容都应包装在标记内,该标记具有对某些php / java文件的操作属性,该文件可验证用户传递的数据并提交给数据库。

请检查mdn上的表单标签以获取更多信息。

而不是您使用if(isset($ name,$ email,$ message)){...}在标签中使用HTML5'required'会更好。 例如,此外,您提交的类型需要有一个名称。 该名称将用于处理您的表格。

contact.php页面。 下面的表格会自行处理,因为该方法的名称为contact.php。

<form id="contact-form" method="post" action="contact.php" >
<input type="text" name="name" placeholder="Name" />
<input type="text" name="subject" placeholder="Subject *" />
<input type="text" name="email" placeholder="Email *" />
<textarea name="message" placeholder="Message *"></textarea>
<input type="submit" value="Send" name="send"/>
</form>

<?php
if (isset($_POST['send']))  {
$to = "youremail@something.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['email'];
$headers = "From: $from $name";
$sent = mail($to,$subject,$message,$headers);

if ($sent)  {
echo "<p style = 'color: #00ff00;'>Message was sent.</p>";
}
else    {
echo "<p style = 'color: #ff0000;'>Message was not sent.</p>";
}
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM