[英]How to display message when a user submits the form
Hi I have the following code. 嗨,我有以下代码。 I was just wondering how to add a message/popup to say "thanks for registering" if everything is successful.
我只是想知道如果一切成功,如何添加一条消息/弹出窗口说“感谢注册”。 Here is the code and btw i am new the php.
这是代码,顺便说一句,我是新的php。 Thanks for your help
谢谢你的帮助
<?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");
}
?> ?>
Try this -- insert instead of the header( Location:index.php);
试试这个-插入而不是
header( Location:index.php);
and die()
lines: 和
die()
行:
Following will display Thanks message for FIVE ( content=5)
seconds before redirecting to index.php 在重定向到index.php之前,以下内容将显示感谢消息,持续
content=5)
( content=5)
秒
echo '<h1>Thanks for registering</h1>';
echo '<meta HTTP-EQUIV="REFRESH" content="5; url=index.php">';
Did you consider using jquery? 您是否考虑过使用jquery? You can use the "$("#id").submit(function(event)){} with one alert, it will give you a popup with a personalized message.
您可以通过一个警报使用“ $(”#id“)。submit(function(event)){},它会弹出带有个性化消息的弹出窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.