表单提交后,php中的Echo未显示在网页上

时间:2015-06-22 19:48:49

标签: php html zurb-foundation

我在这个网页上使用了基础5和php。

当我进入网页并填写所有空格并按下创建帐户时,网页上不会显示任何回声。页面只是刷新,只是创建一个新页面,就像我刚刚重新加载页面。没有文字显示,也没有任何功能正常工作。

这是我的代码:

<?php

error_reporting(0);
@ini_set('display_errors', 0);

?>
<!doctype html>
<html class="no-js" lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>AskmanProducts</title>
    <link rel="stylesheet" href="css/foundation.css" />
    <script src="js/vendor/modernizr.js"></script>
    <script src="js/signinvaldator.js"></script>
</head>
<body>
<?php

if ($_POST['registerbtn']) {
    $getuser = $_POST['user'];
    $getemail = $_POST['email'];
    $getpass = $_POST['pass'];
    $getconfirmpass = $_POST['confirmpass'];

    if ($getuser) {
        if ($getemail) {
            if ($getpass){
                if ($getconfirmpass) {
                    if ($getpass === $getconfirmpass) {
                        if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                            require ("connect.php");

                            $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                            $numrows = mysql_num_rows($query);
                            if ($numrows == 0) {
                                $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                $numrows = mysql_num_rows($query);
                                if ($numrows == 0) {

                                    $password = md5(md5("kjfiufj".$getpass."Fj56fj")); 
                                    $date = date("F d, Y");
                                    $code = md5(rand());

                                    mysql_query("INSERT INTO users VALUES (
                                        '', '$getuser', '$password', '$getemail', '0', '$code', '$date'
                                    )");

                                    $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                                    $numrows = mysql_num_rows($query);
                                    if ($numrows == 1) {

                                        $site = "http://localhost/Projects/project";
                                        $webmaster = "donotreply@askmanproducts.com";
                                        $headers = "From: $webmaster";
                                        $subject = "Activate Your Account";
                                        $message = "Thanks For Registering. Click The Link Below To Activate Your Account.\n";
                                        $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                        $message .= "You Must Activate Your Account To Login.";

                                        if (mail($getemail, $subject, $message, $headers)) {
                                            echo "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b>";
                                            $getuser = "";
                                            $getemail = "";
                                        }
                                        else {
                                            echo "An error has occured. You activation email was not sent.";
                                        }

                                    }
                                    else {
                                        echo "An error has occured. Your account was not created.";
                                    }

                                }
                                else {
                                    echo "There is already a user with that email.";
                                }
                            }
                            else {
                                echo "There is already a user with that username.";
                            }

                            mysql_close();
                        }
                        else {
                            echo "You must enter a valid email address to register.";
                        }
                    }
                    else {
                        echo "Your password do not match.";
                    }
                }
                else {
                    echo "You must confirm your password to register.";
                }
            }
            else {
                echo "You must enter your password to register.";
            }
        }
        else {
            echo "You must enter your email to register.";
        }
    }
    else {
        echo "You must enter your username to register.";
    }
}
else {

}

$form = "<form action='register.php' method='post'>
<div class='row' style='margin-top:10%'>
    <div align='center'><h2>Create an Account</h2></div>
        <br />
        <div class='medium-6 medium-centered large-centered large-6 columns'>
            <form data-abide>
                <div class='name-field'>
                <label>Username</label>
                <input type='text' name='user' value='$getuser'></input>

                <div class='email-field'>
                <label>Email</label>
                <input type='email' name='email' value='$getemail'></input>

                <label for='password'>Password</label>
                <input type='password' name='pass' value=''></input>

                <label for='confirmPassword'>Confirm Password</label>
                <input type='password' name='confirmpass' value=''></input>
            <br />
            <br />         
            <button type='submit' name='registerbtn'>Create Account</button>
            <a href='login.php' class='button'>Log In</a>
            <br />

        </form>
    </div>
</div>

<script src='js/vendor/jquery.js'></script>
<script src='js/foundation.min.js'></script>
<script>
$(document).foundation();
</script>
</form>";

echo $form;

?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的表单有行动=&#39; register.php&#39;作为目的地。当你这样做时,按下提交按钮,页面将跳转到register.php,然后给你的错误检查代码发射任何机会。

我建议你使用

action='<?php echo $_SERVER['PHP_SELF']; ?>' 

以便您的“提交”按钮让您保持在同一页面上,然后在您的错误检查过程通过时,使用:

header('Location: register.php');

或者将每个POST变量传递给register.php,在那里执行错误检查。

答案 1 :(得分:0)

在这个解决方案中,我已经大大改变了你的代码,但我一直在我的网站上使用这个逻辑。我将解释它下面的代码。

注意:

我确实检查了你的处理但没有测试......从最初的评论来看,你得到了有关编程的过时信息。你的mysql查询代码已经过时并且已经过折旧了,你应该使用@Dave在这个答案下面的评论中指出的参数化查询。

我建议你去youtube并搜索pdo教程,学习查询mysql的现代方法。提供的代码显示了如何通过jQuery和处理php文件处理表单。

HTML&amp; jQuery的

<!doctype html>
<html class="no-js" lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>AskmanProducts</title>
    <link rel="stylesheet" href="css/foundation.css" />
    <script src="js/vendor/modernizr.js"></script>
    <script src="js/signinvaldator.js"></script>
</head>
<body>

<input type="hidden" name="processRegistrationURL" value="register.php">

<div id="showRegistrationResults" class="row" style="margin-top:10%">
    <div align="center"><h2>Create an Account</h2></div>
        <br />
        <div class="medium-6 medium-centered large-centered large-6 columns">
            <form data-abide>
                <div class="name-field">
                <label>Username</label>
                <input type="text" is="user" name="user" value="$getuser"></input>

                <div class="email-field">
                <label>Email</label>
                <input type="email" id="email" name="email" value="$getemail"></input>

                <label for="password">Password</label>
                <input type="password" id="pass" name="pass" value=""></input>

                <label for="confirmPassword">Confirm Password</label>
                <input type="password" id="confirmpass" name="confirmpass" value=""></input>
            <br />
            <br />         
            <button type="submit" id="registerbtn" name="registerbtn">Create Account</button>
            <a href="login.php" class="button">Log In</a>
            <br />
    </div>
</div>


<script src='js/vendor/jquery.js'></script>
<script src='js/foundation.min.js'></script>
<script>
$(document).foundation();
</script>
<script>
$(function() {
$( "#registerbtn" ).click(function(){
        var url = $('#processRegistrationURL').val();       
        var user = $('#user').val(); 
        var email = $('#email').val();
        var pass = $('#pass').val();
        var confirmpass = $('#confirmpass').val();      
        var postit = $.post( url, {
            user:user, 
            email:email,
            pass:pass,
            confirmpass:confirmpass         
        });     
        postit.done(function( data ) {
        var result = data.split('|');
        if(result[0] == 1){alert(result[1]);}
        else if(result[0] == 2){
        $('#showRegistrationResults').html(result[1]);
        }       
        });     
    }); 
});
</script
</body>
</html>

没有表单标签...表单处理在register.php处理,存储在隐藏输入<input type="hidden" name="processRegistrationURL" value="register.php">

我已在每个表单和提交按钮中添加了id

register.php

$getuser = $_POST['user'];
    $getemail = $_POST['email'];
    $getpass = $_POST['pass'];
    $getconfirmpass = $_POST['confirmpass'];

    if ($getuser) {
        if ($getemail) {
            if ($getpass){
                if ($getconfirmpass) {
                    if ($getpass === $getconfirmpass) {
                        if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                            require ("connect.php");

                            $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                            $numrows = mysql_num_rows($query);
                            if ($numrows == 0) {
                                $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                $numrows = mysql_num_rows($query);
                                if ($numrows == 0) {

                                    $password = md5(md5("kjfiufj".$getpass."Fj56fj")); 
                                    $date = date("F d, Y");
                                    $code = md5(rand());

                                    mysql_query("INSERT INTO users VALUES (
                                        '', '$getuser', '$password', '$getemail', '0', '$code', '$date'
                                    )");

                                    $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                                    $numrows = mysql_num_rows($query);
                                    if ($numrows == 1) {

                                        $site = "http://localhost/Projects/project";
                                        $webmaster = "donotreply@askmanproducts.com";
                                        $headers = "From: $webmaster";
                                        $subject = "Activate Your Account";
                                        $message = "Thanks For Registering. Click The Link Below To Activate Your Account.\n";
                                        $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                        $message .= "You Must Activate Your Account To Login.";

                                        if (mail($getemail, $subject, $message, $headers)) {
                                            echo "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b>";
                                            $getuser = "";
                                            $getemail = "";
                                        }
                                        else {
                                            echo "2|An error has occurred. You activation email was not sent. Please refresh this page and try again. If this issue persists please contact administration.";
                                        }

                                    }
                                    else {
                                        echo "2|An error has occurred. Your account was not created. Please refresh this page and try again. If this issue persists please contact administration.";
                                    }

                                }
                                else {
                                    echo "1|There is already a user with that email.";
                                }
                            }
                            else {
                                echo "1|There is already a user with that username.";
                            }

                            mysql_close();
                        }
                        else {
                            echo "1|You must enter a valid email address to register.";
                        }
                    }
                    else {
                        echo "1|Your password do not match.";
                    }
                }
                else {
                    echo "1|You must confirm your password to register.";
                }
            }
            else {
                echo "1|You must enter your password to register.";
            }
        }
        else {
            echo "1|You must enter your email to register.";
        }
    }
    else {
        echo "1|You must enter your username to register.";
    }
    else {
        echo "2|WHATEVER YOU WANT TO RENDER IN #showRegistrationResults";
    }

点击该按钮后会触发jQueryregister.php会将表单提交传递给echo "1|You must enter your password to register.php";

你会注意到每个回声都有1或2个带管道。 result[0] == 1 echo将作为数据返回jQuery。

然后jQuery在管道(|)处拆分数据。如果result[0] == 2,则jQuery会发出警报,使表单保持不变,以便进行更正。如果#showRegistrationResults,jQuery将替换包含表单的register.php的内容(它将替换表单)与通过echo返回的数据。

查看$hash_key = trim(file_get_contents('PATH-TO/key.dat')); $password = hash_hmac('sha512', $getpass, $hash_key); 上的回声。前两个替换表单作为处理中的错误已发生。所有其他人都会收到提醒信息。

您可以根据需要切换这些内容。

提高您的密码安全性

您可以使用以下代码提高密码安全性:

key.dat

72093OT7Yw6g0925T9Ly07G6y7WhI2v5只包含一个键:例如:{{1}}

希望这有帮助

皮特