一个简单的网站留言表单

2013-12-12 12:41:25

[开发运维 | Dev Ops] , ,

因为这个网页不太好,很多杂乱的代码,因此没有用jQuery.

 

还害得我要弄一个非常简陋的PHP直接提交式的表单. 全部都要自己写.

 

 

内容比较简单,就是判断一下POST的数据然后根据情况返回,

这里直接是自己返回页面元素..

 

 

 

代码如下:

<?php
function show_error($str) {
?>
<div class="notification error closeable">
<p><?php echo $str
?></p>
</div>
<?php
}
function show_success($str) {
?>
<div class="notification success closeable">
<p><?php echo $str
?></p>
</div>
<?php
}
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["phone"])) {
    if ($_POST["name"] == "Your name..." || $_POST["email"] == "Your e-mail..." || $_POST["phone"] == "Your phone #") {
        show_error("Please fill the form.");
    } else {
        $error = "";
        if (!preg_match("/^[a-zA-Z0-9]{3,20}$/", $_POST["name"])) {
            $error.= " Please enter your name.<br />";
        }
        if (!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $_POST["email"])) {
            $error.= " Please enter a valid email.<br />";
        }
        if (!preg_match("/^([2-9]\d{2}-\d{3}-\d{4}|[2-9]\d{9}|  \d{3}-\d{4})$/", $_POST["phone"])) {
            $error.= " Please enter a valid phone number.<br />";
        }
        if ($error == "") {
            $to = "chen@infoempire.com";
            $title = "Contact Request - Form [客户名称] " . $_POST['name'];
            $headers = 'From: noreply@邮箱地址.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
            $msg = 'Name: ' . $_POST['name'] . "\r\n" . 'Phone Number: ' . $_POST['phone'] . "\r\n" . 'Email: ' . $_POST['email'] . "\r\n" . 'IP: ' . $_SERVER["REMOTE_ADDR"] . "\r\n" . "\r\n";
            $i = mail($to, $title, $msg, $headers);
            if ($i) {
                show_success("<span>Thank you!</span> We have got your message.");
            } else {
                show_error("<span>Sorry</span> Please try later");
            }
        } else {
            show_error($error);
        }
    }
}
?>

 

这篇博文发表在 开发运维 | Dev Ops 目录下,标签为 , ,
如需引用,请使用链接:https://note.mc256.dev/?p=173

This article published in 开发运维 | Dev Ops with tags , , .
Cite this page using this link:https://note.mc256.dev/?p=173

您的邮箱地址不会被公开,评论使用Gravatar头像。
Your email address will not be published. This blog is using Gravatar.

正在提交评论... Submitting ...
正在为您准备评论控件 Loading Comment Plugin
Copyright © 2013-2024 mc256. All Rights Reserved.
Powered by WordPress on top of a dual-stack k3s Cluster using JuiceFS.
Wordpress Theme Designed By mc256.
Encrypted By Let's Encrypt.  Hosted On Linode + OVH + AWS.
DNS Provided By Hostker.
Status Page by CloudFlare Worker.