PHP表单提交 [英] PHP form submit

查看:138
本文介绍了PHP表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建的表单存在问题。基本上,即使PHP代码是正确的,它也不允许我将电子邮件发送给收件人。这可能是TRIM PHP代码的问题吗?

I have a problem with the form I'm trying to create. Basically, it does not allow me to send the email to the recipient, even though the PHP code is correct. Could it be the problem with TRIM PHP code?

<?php
    if ($_POST['submit']) {
        if (empty($_Post['name'])  || 
            empty($_POST['email']) || 
            empty($_POST['comments'])) {

            $error = true;
        } 
        else {

            $to = "linardsberzins@gmail.com";

            $name = trim($_POST['name']);
            $email = trim($_POST['email']);
            $comments = trim($_POST['comments']);

            $subject = "Contact Form";

            $messages =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $message, $headers);

            if ($mailsent) {
                $sent = true;
            }
        }
    }
?>

我的HTML是:

My HTML is:

<?php if($error == true){ ?>

    <p class="error">Text</p>

<?php } if($sent == true) { ?>

    <p class="sent">Text</p>

<?php } ?>

<div id="form">
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <h4>Contact Me!</h4>
            <label for="name">Name:</label>
                <input type="text" name="name" id="name"/>
                <label for="email"/>Email:</label>
                <input type="text" name="email" id="email"/>
                <label for="comments" id="comments">Comments:</label>
                <textarea name="comments" id=""></textarea>
                <fieldset>
                    <input class="btn" type="submit" name="submit"   class="submit" value="Send email"/>
                    <input class="btn" type="reset" value="Reset"/>
                </fieldset>
        </fieldset>
    </form>


推荐答案

试试这个(我不得不修复一些东西):

Try this (I had to fix a number of things):

<?php
    $error = false;
    $sent = false;

    if(isset($_POST['submit'])) {
        if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
            $error = true;
        }
        else {
            $to = "linardsberzins@gmail.com";

            $name = trim($_POST['name']);
            $email = trim($_POST['email']);
            $comments = trim($_POST['comments']);

            $subject = "Contact Form";

            $message =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $message, $headers);

            if($mailsent) {
                $sent = true;
            }
        }
    }
?>

<?php if($error == true){ ?>
<p class="error">Text</p>
<?php } if($sent == true) { ?>
<p class="sent">Text</p>
<?php } ?>
<div id="form">
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <h4>Contact Me!</h4>
            <label for="name">Name:</label>
                <input type="text" name="name" id="name"/>
                <label for="email"/>Email:</label>
                <input type="text" name="email" id="email"/>
                <label for="comments" id="comments">Comments:</label>
                <textarea name="comments" id=""></textarea>
                <fieldset>
                <input class="btn" type="submit" name="submit"  class="submit" value="Send email"/>
                <input class="btn" type="reset" value="Reset"/>
                </fieldset>
        </fieldset>
    </form>
</div>

这篇关于PHP表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆