简体   繁体   English

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

[英]How can I messages between separate sections of one page?

Basically, in the following code: 基本上,在以下代码中:

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

The problem is I want to show the message that show up in the top (before the html part) in the body part. 问题是我想显示在正文部分顶部(在html部分之前)显示的消息。 That means when the user completes the registration, the message will show up instead of the fields in the body section (Name, UserName, Email ,....). 这意味着,当用户完成注册时,将显示消息,而不是正文部分中的字段(名称,用户名,电子邮件,....)。

To illustrate it: 为了说明这一点:

If the registration is valid, I want the message: 如果注册有效,我想要消息:

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

Appears in the body part (instead of the fields). 出现在身体部位(而不是字段)。

I hope you understand my explanation. 希望您能理解我的解释。

You set a variable, let it be regSuccess , in the php part to either true to false depending on whether user registration was successfull or not 您可以在php部分regSuccess变量设置为regSuccess ,将true或false设置为true,具体取决于用户注册是否成功

Then in the html part, you checkk for the value of this variable in an if condition and output the corresponding html. 然后在html部分中,在if条件下检查此变量的值,并输出相应的html。

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

Utilize a $_SESSION variable to indicate that the user successfully registered. 使用$ _SESSION变量指示用户已成功注册。 You will start a session on your page and check if that value is set before doing anything else. 您将在页面上启动会话,并在执行其他任何操作之前检查该值是否已设置。 If the variable exists, then display the activation message, otherwise provide the registration fields and continue with your existing code. 如果变量存在,则显示激活消息,否则提供注册字段并继续使用现有代码。

The reason for utilizing $_SESSION is to persist state information between page requests. 使用$_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;
...
}

?>

you could create a variable to store you error message instead of echo it directly. 您可以创建一个变量来存储错误消息,而不是直接回显它。 And add a 'IF' case in the <body> for validation occur error, echo the error, otherwise print the register form. 并在<body>添加'IF'大小写以确认发生错误,回显错误,否则打印寄存器表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM