如果出现错误,则使用PHP +验证表单不会刷新

时间:2014-02-07 02:06:22

标签: javascript php html forms

我正在尝试使用php在按下提交时验证我的表单,我想要以下内容:

用户按提交,如果未发现错误,则处理注册表单并将数据发送到数据库。

如果存在错误,则页面不会刷新并显示错误控制台。

每次用户点击提交时,如果发现错误,则会删除旧邮件&错误控制台div中发布的新消息。

我为表单编写了所有代码,验证(php),错误控制台div& CSS。我只是不知道如何将它们整合在一起

类似:

if ( ($username && $email) != 0 ) { error console + remain on same page } else {send to db};

img http://1.ii.gl/5lasBG.png

PHP验证码

        $username = 0;

        $uservar = $_POST['username'];

        if (empty($uservar))
    {
        $username = 1;
    } 
else if (!preg_match("/^\w{5,20}$/",$uservar))
    {   
        $username = 2;
    }


    if ( ($username) != 0 ) {

    echo "<h4 class='error2'><img src='http://s28.postimg.org/ql0x06555/warning6.png' alt='Error'>  Error Console</h4> </br>";


switch ($username) {

    case 1:
        echo "<img src='http://s27.postimg.org/vjxntq073/sign5.png' alt='Error'>";
        echo "     The Field 'Username' cannot be left blank";
        break;
    case 2:
        echo "<img src='http://s27.postimg.org/vjxntq073/sign5.png' alt='Error'>";
        echo "     Invalid 'Username' Format - Please use Letters & Numbers only (5-20 Characters)";
        break;      
}}

</div>

FORM:

<form id="registration-form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">


<input type="text" name="username" id="username" placeholder="Username">

<button type="submit" name="submit" id="submit" class="button-submit">Submit</button>


</form>

1 个答案:

答案 0 :(得分:1)

  1. 将您的主要PHP从您的HTML

  2. 拆分
  3. 你的html中的php只处理简单的逻辑和显示结果

  4. 作为快速回答,请检查以下代码:

    <?php
    
    function valiation(){
        $aryMsg = array();
        $aryExport = array();
        if( !isset( $_POST['_submit'] ) ){
            //no submit
            return array( $aryMsg, $aryExport );
        }
    
        $aryPostFieldWhiteList = array( 'username' );
        //1. post content filter
        //make sure pass all the field you need from $_POST to $aryExport
        foreach( $aryPostFieldWhiteList as $strField ){
            $aryExport[$strField] = htmlentities( $_POST[$strField], ENT_QUOTES );
        }
    
        //2. post content filter
        //...
        //if username is wrong fill $aryMsg['username'] ... etc
    
        //3. check validation result
        if( sizeof($aryMsg) ){
            //means not pass the validation, return error message and filtered data
            return array( $aryMsg, $aryExport );
        }
    
        //4. past validation
    
        //4.1 all db, to insert result
        //db insert ...etc
    
    
        //4.2 page redirect to thank you page
        header( 'Location: /thankyou.php' );
    
        die();
    }
    
    list( $aryMsg, $aryPost ) = valiation();
    ?>
    <html>
        <head>
        ...
        </head>
        <body>
        <?php if( is_array( $aryMsg ) && sizeof( $aryMsg ) ):?>
            <h4 class='error2'><img src='http://s28.postimg.org/ql0x06555/warning6.png' alt='Error'>  Error Console</h4>
            <ul class="error_msg">
            <?php foreach( $aryMsg as $strMsg ):?>
                <li><?php echo $strMsg;?></li>
            <?php endforeach;?>
            </ul>
        <?php endif;?>
    
        <form id="registration-form" action="" method="post">
    
            <input type="text" name="username" id="username" placeholder="Username" value="<?php echo $aryPost['username'];?>">
    
            <button type="submit" name="_submit" id="submit" class="button-submit">Submit</button>
    
        </form>
    
        </body>
    </html>