PHP联系表单建议

时间:2015-07-22 16:00:40

标签: php forms

我为我的网站建立了一个联系页面,当我测试它时,唯一出现的是消息。除了托管网站的网址外,没有联系信息。关于如何解决这个问题的任何建议将不胜感激。谢谢。

以下是HTML代码:

<form action="contact.php" class="form-style-9" method="post">
<ul>
<li>
<input type="text" name="first_name" class="field-style field-split align-left" placeholder="First Name" />
 <input type="text" name="last_name" class="field-style field-split align-right" placeholder="Last Name" />


</li>
<li>
<input type="text" name="phone" class="field-style field-split align-left" placeholder="Phone" />
<input type="email" name="email" class="field-style field-split align-right" placeholder="Email" />
</li>
<li>
<input type="text" name="subject" class="field-style field-full align-none" placeholder="Subject" />
</li>
<li>
<textarea name="message" class="field-style" placeholder="Message"></textarea>
</li>
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</form>

这是PHP代码:

<?php

 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $phone = $_POST['phone'];
 $email = $_POST['email'];
 $subject = $_POST['subject'];
 $message = $_POST['message'];

 $to = "kyle@kylenarovich.com";
 $subject = "New Message";

 mail ($to, $subject, $message, "From: " . $first_name . $last_name);


echo "Thank you for contacting me. I will be in touch with you very soon."; 





?>

同样,任何建议和帮助都会非常感激,因为该网站是有效的,如果有人与我联系,如果他们没有在邮件中提供信息,我无法回复他们。

非常感谢!!

1 个答案:

答案 0 :(得分:-1)

<?php
    if($_SERVER['REQUEST_METHOD']=='POST' && array_key_exists( 'form_submitted',$_POST )){
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $to = "kyle@kylenarovich.com";
        $subject = "New Message";

        $res=@mail ($to, $subject, $message, "From: " . $first_name . $last_name);
    }
?>

<html>
    <head>
        <title></title>
    </head>
    <body>

        <form action="contact.php" class="form-style-9" method="post">
            <ul>
                <li>
                    <input type="text" name="first_name" class="field-style field-split align-left" placeholder="First Name" />
                    <input type="text" name="last_name" class="field-style field-split align-right" placeholder="Last Name" />
                </li>
                <li>
                    <input type="text" name="phone" class="field-style field-split align-left" placeholder="Phone" />
                    <input type="email" name="email" class="field-style field-split align-right" placeholder="Email" />
                </li>
                <li>
                    <input type="text" name="subject" class="field-style field-full align-none" placeholder="Subject" />
                </li>
                <li>
                    <textarea name="message" class="field-style" placeholder="Message"></textarea>
                </li>
                <li>
                    <input type="submit" value="Send Message" />
                    <input type='hidden' name='form_submitted' value='true' />
                </li>
            </ul>
            <?php
                if( $_SERVER['REQUEST_METHOD']=='POST' && array_key_exists( 'form_submitted',$_POST ) ){
                    if( $res==true ) echo 'Thanks for submitting the form... etc etc';
                    else echo 'Oops! Failed to send email';
                }
            ?>
        </form>
    </body>
</html>