【问题标题】:How Do I Make this contact form work?如何使此联系​​表格起作用?
【发布时间】:2016-09-11 03:28:45
【问题描述】:

我无法编辑此“联系我们”表单代码工作并将消息发送到我的邮箱。

假设我想将消息发送到我的电子邮件: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>

【问题讨论】:

  • 嗯,这可能是因为上面的代码不是 html &lt;form&gt;,而是一个简单的&lt;div&gt; 容器。看这里:developer.mozilla.org/de/docs/Web/HTML/Element/form
  • 表单必须有一些 php 代码来获取表单值、创建电子邮件并发送。
  • 请添加您的代码 insted of image,以便我们可以让您知道您在哪里做错了,我们可以为您提供正确的代码,目前您没有任何表单标签添加表单标签并添加操作。

标签: javascript php html css web


【解决方案1】:

我已经优化了你的代码并添加了一些 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>

【讨论】:

    【解决方案2】:

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

    因此,您需要以下标签:

    <form action='url_to_submit_to' method='POST' enctype='application/x-www-form-urlencoded'>
    
     <!-- Rest of your code here -->
    
    </form>
    

    有关&lt;form&gt; 标签的格式和参数的更多详细信息,请参阅 Mozilla 文档:

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

    【讨论】:

      【解决方案3】:

      这里没有发生提交,因为内容没有包含在表单标签中。 所有内容都应该包含在标签中,该标签具有某些 php/java 文件的 action 属性,该文件验证用户传递的数据并提交到数据库。

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

      【讨论】:

      • 所以我所缺少的只是一个“表单标签”和一些应该插入表单之间的 php。是吗?
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        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>";
        }
        }
        ?>
        

        【讨论】:

        • 我尝试复制粘贴您共享的代码。输入我的电子邮件而不是 yourremail@something.com 但它导致了错误。这是我看到我的网页为postimg.org/image/7zfzsuj0h 的屏幕截图我做错了什么?这是我复制的:postimg.org/image/uvawpbyrl
        • 您好,抱歉回复晚了。首先,您必须使用名称 contact.php 保存页面
        猜你喜欢
        • 1970-01-01
        • 2014-03-31
        • 2016-10-28
        • 2014-10-04
        • 2014-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多