如何在用户提交表单时显示消息

时间:2014-01-19 23:32:01

标签: php

您好我有以下代码。我只是想知道如果一切都成功,如何添加消息/弹出窗口说“感谢注册”。这是代码和顺便说一句我是新的PHP。谢谢你的帮助

<?php 
require("config.php");
if(!empty($_POST)) 
{ 
    // Ensure that the user fills out fields 
    if(empty($_POST['username'])) 
    { die("Please enter a username."); } 
    if(empty($_POST['password'])) 
    { die("Please enter a password."); } 
    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    { die("Invalid E-Mail Address"); } 

    // Check if the username is already taken
    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            username = :username 
    "; 
    $query_params = array( ':username' => $_POST['username'] ); 
    try { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
    $row = $stmt->fetch(); 
    if($row){ die("This username is already in use"); } 
    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            email = :email 
    "; 
    $query_params = array( 
        ':email' => $_POST['email'] 
    ); 
    try { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());} 
    $row = $stmt->fetch(); 
    if($row){ die("This email address is already registered"); } 

    // Add row to database 
    $query = " 
        INSERT INTO users ( 
            username, 
            password, 
            salt, 
            email 
        ) VALUES ( 
            :username, 
            :password, 
            :salt, 
            :email 
        ) 
    "; 


    // Security measures
    $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
    $password = hash('sha256', $_POST['password'] . $salt); 
    for($round = 0; $round < 65536; $round++){ $password = hash('sha256', $password . $salt); } 
    $query_params = array( 
        ':username' => $_POST['username'], 
        ':password' => $password, 
        ':salt' => $salt, 
        ':email' => $_POST['email'] 
    ); 
    try {  
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
    header("Location: index.php"); 

    die("Redirecting to index.php"); 


} 

&GT;

2 个答案:

答案 0 :(得分:0)

试试这个 - 插入而不是header( Location:index.php);die()行:

以下将显示5条感谢消息(content=5)秒,然后重定向到index.php

echo '<h1>Thanks for registering</h1>';
echo '<meta HTTP-EQUIV="REFRESH" content="5; url=index.php">';

答案 1 :(得分:0)

你考虑过使用jquery吗? 你可以使用“$(”#id“)。submit(function(event)){}和一个警报,它会给你一个带有个性化信息的弹出窗口。