doushang7209 2014-04-27 19:23
浏览 61
已采纳

PHP在同一页面上提交表单,回显如下表所示

I'm aware that this question has been asked a huge number of times but the answers seem really specific to the script posted and with my current knowledge I can't make the transition from the corrected script to implementing it into my own.

This all works fine - it submits on the same page and provides feedback of any errors, but I want any errors to be echoed beneath the form making it more convenient for the user to just change what they entered incorrectly and re-submit.

From what I can gather from the questions I read before posting this - it may only be possible using jQuery/Ajax?

My script:

<?php

if(isset($_POST['submit'])){
require "connection.php";

$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$repeat_password = $_POST["repeat_password"];

$username_query = mysql_query("SELECT id FROM users WHERE username = '$username'");
$email_query = mysql_query("SELECT id FROM users WHERE email = '$email'");

if($username == "" || $password == "" || $repeat_password == "" || $email == ""){
  die("All boxes must be filled out!");
}// ^ Checking if all boxes have been filled out
else {
    if (!ctype_alnum($username)){
        die("Username can only contain letters and numbers");
    }// ^ Checking if username is alphanumeric
    else {
        if (strlen($username) < 6 || strlen($username) > 15){
            die("Username must be between 6-15 characters.");
        }// ^ Checking if username is between 6-15 characters
        else {
            if (mysql_num_rows($username_query) != 0){
                die("Username is taken, please choose another.");
            }// ^ Checking if username exists in database
            else {
                if (!preg_match("/[0-9]/",$password)){
                    echo "password doesnt contain a number";
                    }
                else {
                    if ($password != $repeat_password){
                        die("Passwords do not match");
                    }// ^ Checking if password and repeat_password match
                    else {
                        if (strlen($password) < 6){
                            die("Password must be atleast 6 characters long.");
                        }// ^ Checking if password is longer than 6 characters
                        else {
                            if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
                                die("E-mail address is not vaild.");
                            }// ^ Checking if e-mail address is valid
                            else {
                                if (mysql_num_rows($email_query) != 0){
                                    die("This e-mail address has already been used to create a different account.");
                                }// ^ Checking if e-mail address has already been used
                                else {
                                    mysql_query("INSERT INTO users (username, password, email, signup_date) VALUES ('$username', '$password', '$email', CURDATE())") or die(mysql_error());
                                    echo "Account succesfully created, welcome ".$username."!";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
}
exit;
}

// 

?>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <table>
                <tr>
                    <td>
                        Username:
                    </td>
                    <td>
                        <input type="text" name="username">
                    </td>
                </tr>
                <tr>
                    <td>
                        Password:
                    </td>
                    <td>
                        <input type="password" name="password">
                    </td>
                </tr>
                <tr>
                    <td>
                        Repeat password:
                    </td>
                    <td>
                        <input type="password" name="repeat_password">
                    </td>
                </tr>
                <tr>
                    <td>
                        E-mail:
                    </td>
                    <td>
                        <input type="email" name="email">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <center><input type="submit" name="submit"></center>
                    </td>
                </tr>
            </table>
        </form>
  • 写回答

3条回答 默认 最新

  • dongxieyou3314 2014-04-27 19:44
    关注

    Answer: Yes, you have to use jQuery there.

    You can validate form just after user entered 1st letter. You can validate on key up/down or on submit. I suggest to use jQuery Validation Plugin.

    To sumbit form use ajax requests. It is rly simple. You can read about it here. There are some examples at the end page I had given.

    Note, that if you will use jQuery Validation Plugin you can send ajax request on valid action. Using this, ajax request with serialized form will be sent on form submit + on form valid. If form has some invalid fields, errors will be shown, if there are no errors, ajax-request will be send.

    Advice: Your arhitecture not very good. Why? If people will write bad name and make 10 more other errors, only 1 error:

    Username can only contain letters and numbers

    will be shown. Why? Because of you arhitecture. After that he will correct 2nd erorr. 3rd error etc.

    I suggest you do to handle errors this way:

    $errors = array();
    $errorsFlag = false;
    
    if(check_username1()) {
        $errors[] = 'Tell about this error';
        $errorsFlag = true;
    }
    
    if(check_username2()) {
        $errors[] = 'Tell about this error';
        $errorsFlag = true;
    }
    if(check_mail()) {
        $errors[] = 'Tell about this error';
        $errorsFlag = true;
    }
    

    And how to output it? Use this:

    if($errorsFlag == true) {
        foreach($errors as $error) {
            echo $error . " <br /> ";;
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件