0

基本上,在以下代码中:

<?php
$hostname = '';
$username = '';
$password = '';
$dbn = '';
try {
$dbh = mysqli_connect($hostname , $username, $password ,$dbn);
//echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
if (isset($_POST['formsubmitted'])) {
$fullname = $_POST['fullname'];
$username = $_POST['username'];
$email1 = $_POST['email1'];
$password1 = $_POST['password1'];
$dob = $_POST['dob'];
$query_verify_email = "SELECT * FROM User  WHERE Email = '$email1'";
$result_verify_email = mysqli_query($dbh, $query_verify_email);
if (!$result_verify_email) {//if the Query Failed ,similar to             if($result_verify_email==false)
echo ' Database Error Occured ';
}
if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this     email .
// Create a unique  activation code:
$activation = md5(uniqid(rand(), true));
//$id= uniqid();
$query_insert_user = "INSERT INTO `User` ( `Name`, `Username`, `Email`, `Password`,     `DOB`, `Activation`) VALUES ( '$fullname', '$username', '$email1', '$password1', '$dob',     '$activation')";
$result_insert_user = mysqli_query($dbh, $query_insert_user);
if (!$result_insert_user) {
echo 'Query did not work ';
}
if (mysqli_affected_rows($dbh) == 1) { //If the Insert Query was successfull.
// Send the email:
$message = " To activate your account, please click on this link:\n\n";
$message .= 'http://website' . '/active.php?email=' . urlencode($email1) . "&key=$activation";
mail($email1, 'Registration Confirmation', $message, 'From: a@b.com');
// Flush the buffered output.
// Finish the page:
echo '<div class="success">Thank you for registering! A confirmation email has been     sent to '.$email1.' Please click on the Activation Link to Activate your account </div>';
} else { // If it did not run OK.
echo '<div class="errormsgbox">You could not be registered due to a system error. We     apologize for any inconvenience.</div>';
}
} else { // The email address is not available.
echo    '<div class="errormsgbox" >That email address has already been registered.
</div>';
}
mysqli_close($dbh);//Close the DB Connection
}// End of the main Submit conditional.
?>
<html>
<head>
</head>
<body>
    <form name="f1" action="Main.php" method="post">
        <p>Full name: <br/><input class="tb10" type="text" name="fullname" /></p>
        <p>Username: <br/><input class="tb10" type="text" id="username"     name="username" /><br/>
        <p>Email: <br/><input class="tb10" type="text" id="email1" name="email1" /></p>
        <p>Re-Enter Email: <br/><input class="tb10" type="text" name="email2" /></p>    <br/>
        <p>Password: <br/><input class="tb10" type="password" name="password1" /></p>
        <p>Re-Enter Password: <br/><input class="tb10" type="password" name="password2"     /></p><br/>
        <p>Date of Birth: <br/><input class="tb10" type="text" name="dob" /></br><img     src="img/calendar1.gif" alt="Calendar"     onclick="displayCalendar(document.forms[0].dob,'yyyy/mm/dd',this)"/></p><br/>
        <div class="submit">
            <input type="hidden" name="formsubmitted" value="TRUE" />
            <input type="submit" value="Submit" class="button" />
        </div>                  
    </form>
</body>
</html>

问题是我想显示显示在正文部分顶部(在 html 部分之前)的消息。这意味着当用户完成注册时,将显示消息而不是正文部分中的字段(姓名、用户名、电子邮件......)。

为了说明这一点:

如果注册有效,我想要消息:

Thank you for registering! A confirmation email has been     sent to '.$email1.' Please click on the Activation Link to Activate your account

出现在正文部分(而不是字段)。

我希望你能理解我的解释。

4

3 回答 3

3

regSuccess根据用户注册是否成功,您在 php 部分设置一个变量,让它成为true 到 false

然后在 html 部分,在一个条件中检查这个变量的值if并输出相应的 html。

<?php
if($regSuccess == TRUE) {
?>
    Thank you message
<?php
} 
else 
{ ?>
    the input fields
<?php 
} ?>
于 2012-07-10T03:56:26.480 回答
0

利用$_SESSION变量来指示用户成功注册。您将在您的页面上启动一个会话,并在执行任何其他操作之前检查该值是否已设置。如果变量存在,则显示激活消息,否则提供注册字段并继续使用现有代码。

使用的原因$_SESSION是在页面请求之间保持状态信息。

<?php

session_start();

if(isset($_SESSION['registered_email'])){

//Display message indicating user has already registered
echo 'Thank you for registering! A confirmation email has been sent to '. $_SESSION['registered_email'] .' Please click on the Activation Link to Activate your account';

}else{

// The rest of your code
...
// set session variable to indicate the registration was successful
$_SESSION['registered_email'] = $email1;
...
}

?>
于 2012-07-10T03:59:05.263 回答
0

您可以创建一个变量来存储您的错误消息,而不是直接回显它。<body>并在验证发生错误时添加“IF”案例,回显错误,否则打印注册表单。

于 2012-07-10T03:59:22.577 回答