在提交时验证表格并发送确认电子邮件

时间:2013-09-24 10:23:41

标签: php mysql forms validation email

我正在创建一个管理页面,管理员可以在其中为人员创建用户帐户。我们的想法是,一旦表单完成,点击“提交”时,必须向用户发送一封电子邮件(包含所选帐户的ID和名称)。在同一操作中,还必须首先验证表单,如果验证有任何错误,则不应将数据提交到数据库。这一切都没有发生,我无法弄清楚原因。

  • 电子邮件未发送,
  • 即使存在错误并且在加载页面时,数据也会插入数据库中,
  • 即使未单击提交按钮,
  • 也会显示所有表单域的错误。

我们非常感谢任何有关可能来源/教程的帮助,建议或链接。

以下是我的代码:(请注意,我只使用PHP,HTML和使用MYSQL数据库)

<html>
<head>
    <title>
        User Registration
    </title>
    <?PHP


    include_once 'includes\functions.php';
    connect();

    error_reporting(E_ERROR | E_PARSE);


//Assign variables
    $accounttype=mysql_real_escape_string($_POST['accounttype']);
    $sname = mysql_real_escape_string($_POST['sname']);
    $fname = mysql_real_escape_string($_POST['fname']);
    $email = mysql_real_escape_string($_POST['email']);
    $address = mysql_real_escape_string($_POST['address']);
    $contact_flag = mysql_real_escape_string($_POST['contact_flag']);


//Validating form(part1)        

    $error='';          


//Connect to database                   
        $SQL=
            "INSERT INTO student 
                        (
                            sname,fname,email, address, contact_flag
                        ) 
            VALUES 
                        (

                        '$sname', '$fname', '$email', '$address', '$contact_flag' 
                        )
            ";
        if (!mysql_query($SQL))
            {
                print'Error: '.mysql_error();
            }

        mysql_close($db_handle);
//Validate form(part 2)
    if (isset($_POST['sname'], $_POST['fname'],$_POST['email'],$_POST['address']));
        {
            $errors=array();


                $accounttype= mysql_real_escape_string($_POST['accounttype']);
                $sname = mysql_real_escape_string($_POST['sname']);
                $fname = mysql_real_escape_string($_POST['fname']);
                $email = mysql_real_escape_string($_POST['email']);
                $address = mysql_real_escape_string($_POST['address']);
                $contact_flag = mysql_real_escape_string($_POST['contact_flag']);

// form validation
                        if(strlen(mysql_real_escape_string($sname))<1)
                        {
                            $errors[]='Your surname is too short!';
                        }

                        if(strlen(mysql_real_escape_string($fname))<1)
                        {
                            $errors[]='Please insert you full first name';
                        }


                        if(filter_var($email, FILTER_VALIDATE_EMAIL)===FALSE)
                        {
                            $errors[]='Please insert your valid email address';
                        }
                        if(strlen(mysql_real_escape_string($address))<8)
                        {
                            $errors[]='Please insert your postal address';
                        }

                            echo'<pre>';
                            print_r($errors);
                            echo'</pre>';
        }


//confirmation email

    // Subject of confirmation email.
        $conf_subject = 'Registration confirmed';

    // Who should the confirmation email be from?
        $conf_sender = 'PHP Project <my@email.com>';

    $msg = $_POST['fname'] . ",\n\nThank you for registering. \n\n You registered for account:".$accounttype."\n\n Your account number:".mysql_insert_id;

        mail( $_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender );



   ?>
</head>
<body>


    </br>
    <form name ="form0" Method="POST" Action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
    </br>
        </br>
        <b>Select the course you wish to register for:</b></br>
                <select   name="accounttype">
                    <?PHP query() ?>

                </select>   
                    <?PHP close() ?>


    </form>


    <form name ="form1" Method="POST" Action="<?PHP echo $_SERVER['PHP_SELF']; ?>">

        </br>
        </br>
        <Input type ="" Value = "Surname" Name = "sname"></br>
        <Input type ="" Value = "First name" Name = "fname"></br>
        <b>Email:</b> <Input type ="" Value = "" Name = "email"></br>
        <b>Address:</b> </br>
        <textarea rows="4" cols="20" Name="address">Please provide your postal address here </textarea></br>
        <b>Tick to receive confinmation email:</b> <Input type ="checkbox" Value = "1" Name = "contact_flag"></br>

        <Input type = "Submit" Value="Submit">

    </form>





</body>
</html>

3 个答案:

答案 0 :(得分:1)

 <?PHP
if(isset($_POST['submit']))
{
    include_once 'includes\functions.php';
    connect();

// your rest of the code

        mail( $_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender );

}

   ?>

并将此代码保留在<html>标记之外,但在此之前

答案 1 :(得分:0)

如果你只想坚持使用PHP,那么我可以看到一个错误是

that you have kept the **validation code below the `INSERT`** 

查询这意味着插入查询将首先执行,这将首先将数据存储在数据库中,然后它将用于验证... 请将验证码保留在INSERT语句之上..  并且第二件事使用exit()方法,如果它给你错误的话,每个字段都会使用 if(strlen(mysql_real_escape_string($sname))<1) { $errors[]='Your surname is too short!'; echo '//whatever you want to echo'; exit(); } if(strlen(mysql_real_escape_string($fname))<1) { $errors[]='Please insert you full first name'; echo '//whatever you want to echo'; exit(); } 方法...如果任何字段在验证过程中出现错误,它将停止执行其余的PHP代码....因此,如果在发现错误时找到退出方法,它也会阻止数据存储到数据库中,例如

{{1}}

答案 2 :(得分:0)

首先,在验证表单之前执行查询。

后移动SQL
echo'<pre>';
print_r($errors);
echo'</pre>';

并用

包围它
if(!$errors){}

如果出现任何错误,这将阻止您执行查询。 (您也可以删除您的第一个变量分配)

关于您的电子邮件问题,请使用简单的消息测试您的连接

mail('your@email.com', 'subject', 'message');

如果您收到任何错误,可能是您的邮件服务器设置不正确