显示弹出消息而不重置表单

时间:2014-06-06 17:13:04

标签: php forms

目前我创建了注册表单。问题是如果我错误地输入表单,它会在表单顶部和表单

中显示错误消息

您可以在此处查看我的完整代码

    <?php 
    include 'core/init.php';

    logged_in_redirect();
    include 'includes/overall/header.php' ; 

    //if form is being submitted
    if(empty($_POST)=== false)
    {
        //to validate whether user enters smtg or not otherwise no point continue to do the next validation
        //create an array
        $required_fields = array ('username','password','password_again','first_name','email','passport','gender','contactno','address');
        foreach($_POST as $key=>$value)
        {

            //if the key (value) in array of $required_fields is true which is empty
            if(empty($value) && in_array ($key, $required_fields) === true )
            {
                $errors[] = 'Fields marked with an asterisk are compulsory!';
                //the validation can happen to more than 1 field
                break 1;
            }
        }

        if(empty($errors) === true)
        {
            if(user_exists($_POST['username']) === true)
            {
                $errors[] = 'Sorry, the username \'' .$_POST['username'] . '\' is already taken.';
            }
            //search for the string,  preg_match(search_pattern, your_string)
            // \s = white space character
            //if(preg_match("/\\s/", $_POST['username']) == true)
            if(preg_match("/\s/", $_POST['username']) == true)
            {
                $errors[] = 'Your username must not contain space.';
            }
            if(strlen($_POST['password']) < 6)
            {
                $errors[] = 'Your password must be at least 6 characters';
            }
            if($_POST['password'] !== $_POST['password_again'])
            {
                $errors[] = 'Your passwords do not match at all';
            }
            if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
            {
                $errors[] = 'A valid email address is required';
            }
            if(email_exists($_POST['email']) === true)
            {
                $errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already in use.';
            }
            if(preg_match("/\s/", $_POST['passport']) == true)
            {
                $errors[] = 'Your passport must not contain space!!.';
            }
            else if(preg_match("/\s/", $_POST['gender']) == true)
            {
                $errors[] = 'Your gender must not contain space.';
            }
            else if (!preg_match('/^[0-9]\d{9}$/', $_POST['contactno'])) 
            {

                $errors[] = 'Enter your contact number correctly!!.';
            }       
        }
    }
    //what does this line does is that to check whether success is in the end of the URL
    if(isset($_GET['success']) && empty($_GET['success']))
    {
        echo 'You have been registered successfully! You may login now';
    }
    else
    {//if there are no errors
        if (empty($_POST) === false && empty($errors) === true)
        {
            //create an array of $register_data for sanitizing purpose ,prevent SQL injection attactk
            $register_data = array
            (
                'username' => $_POST['username'],
                'password' => $_POST['password'],
                'passport' => $_POST['passport'],
                'first_name' => $_POST['first_name'],
                'last_name' => $_POST['last_name'],
                'gender' => $_POST['gender'],   
                'email' => $_POST['email'],
                'contactno' => $_POST['contactno'],
                'address' => $_POST['address']
            );
            register_user($register_data);
            //redirect and bring success variable to register.php
            header('Location: register.php?success');
            exit();
        }

        //
        else if (empty($errors) === false)
        {
            echo output_errors($errors);
        }

    ?>


    <form action="" method="post">
        <ul>
            <li>
                Username*:<br>
                <input type="text" name="username">
            </li>
            <li>
                Password*:<br>
                <input type="password" name="password">
            </li>
            <li>
                Password again*:<br>
                <input type="password" name="password_again">
            </li>
            <li>
              <table width="200" border="0">
                <tr>
                  <td width="157" scope="col">First name*: <br>
                    <input type="text" name="first_name">              </td>
                  <td width="439" scope="col">Last name: <br>
                    <input type="text" name="last_name">              </td>
                </tr>
                <tr>
                  <td><p>Gender*:
                      <select name="gender" id="gender">
                      <option value="">Select</option>
                        <option value="Male">Male</option>
                        <option value="Female">Female</option>
                      </select>
                  </p></td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>Passport: <br>
                  <input type="text" name="passport"></td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td><p>Email*:<br>
                    <input type="text" name="email">
                  </p></td>
                  <td><p>Contact No*:<br>
                      <input type="text" name="contactno" id="contactno">
                  </p></td>
                </tr>
                <tr>
                  <td colspan="2"><p>Address*<br>
                      <input name="address" type="text" size="100">
                  </p></td>
                </tr>
              </table>
            </li>
            <li>
                <input type="submit" value="Register">
            </li>
        </ul>
    </form>

    <?php 
    }
    include 'includes/overall/footer.php' ; ?>

现在我想更改为显示弹出消息而不重置表单。

有没有解决办法?

2 个答案:

答案 0 :(得分:1)

如果你给这样的表单输入标签会很好: <input type="text" name="username" placeholder="Username" value="<?php echo $_POST['username']; ?>"> 以及除密码以外的其他字段。

OR

<input type="text" name="username" placeholder="Username" value="<?php echo (isset($_POST['username'])?$_POST['username']:''); ?>">

如果出现某种错误并且您尚未验证表单,它会保留您的表单数据。

答案 1 :(得分:0)

PHP是服务器端处理语言。如果没有提交表单,就无法在客户端上处理PHP代码。

但是,您可以完成使用Javascript的操作,这将运行客户端,并且可以在用户提交表单之前弹出警报(或弹出窗口)以指示表单有什么问题。

这里有一些很棒的图书馆建议,以及一些很好的样本:Javascript form validation