如何在一个页面的不同部分之间发送消息?

时间:2012-07-10 03:52:30

标签: php html

基本上,在以下代码中:

<?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

出现在身体部位(而不是字段)。

我希望你理解我的解释。

3 个答案:

答案 0 :(得分:3)

根据用户注册是否成功,您在php部分中将变量设为regSuccess为true或false

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

<?php
if($regSuccess == TRUE) {
?>
    Thank you message
<?php
} 
else 
{ ?>
    the input fields
<?php 
} ?>

答案 1 :(得分: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;
...
}

?>

答案 2 :(得分:0)

您可以创建一个变量来存储错误消息,而不是直接回显它。 并在<body>中添加一个“IF”情况,以便验证发生错误,回显错误,否则打印寄存器表单。