我想像在Bootstrap代码中一样在PHP和HTML中创建一个不错的错误消息,而不是在echo()中显示消息。 但是我不想使用任何功能。
<?php
include 'condb.php';
if (isset($_POST['sub'])) {
$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email'];
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) ||
empty($pass2)) {
echo "<p class='alert alert-danger'> must fill all </p>";
}else{
if (empty($pass1) || empty($pass2)) {
echo "you must fill password";
}
else{
if ($pass1 == $pass2) {
$hashpass = md5($pass1);
$query="insert into users values (null, '$name', '$lname', '$email',
'$hashpass')";
$result=mysqli_query($conn,$query);
echo "success";
}else{
echo "passwords must be same";
}
}
}
}
?>
答案 0 :(得分:4)
将邮件存储在session
之类的$_SESSION["errormsg"]='you must fill password';
变量中
现在您可以像
<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div>
别忘了在脚本的开头包含 session_start()
,并在打印内容后取消设置会话,否则,将一遍又一遍地向您显示完全相同的内容。 / p>
// remove all session variables
session_unset();
// destroy the session
session_destroy();
答案 1 :(得分:3)
如果您在不同的页面中工作,一个是纯PHP,另一个是HTML嵌入,则在php页面中进行会话,然后将值保存在会话中,然后在html页面中使用会话。
<?php
include 'condb.php';
session_start();
if (isset($_POST['sub'])) {
$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email'];
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) ||
empty($pass2)) {
$_SESSION["errormsg"]='you must fill password';
}else{
if (empty($pass1) || empty($pass2)) {
$_SESSION["errormsg"] = "you must fill password";
}
else{
if ($pass1 == $pass2) {
$hashpass = md5($pass1);
$query="insert into users values (null, '$name', '$lname', '$email',
'$hashpass')";
$result=mysqli_query($conn,$query);
echo "success";
}else{
$_SESSION["errormsg"] = "passwords must be same";
}
}
}
}
?>
在您的html代码中使用上面的会话。
<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div>
答案 2 :(得分:0)
创建一个单独的文件(例如messages.php),并将其包含在您的代码中(例如header.php中)或所需的单独页面中。
现在在该message.php文件中,您可以定义所有想要的消息,以后更容易更改和更新它们。更容易在任何地方使用它们
示例
define("ADVANCE_ADDED","Advance detail has been added successfully.");
define("ADVANCE_UPDATED","Advance detail has been updated successfully.");
define("ADVANCE_REMOVED","Advance detail has been removed successfully.");
define("APPROVE_LV_MSG","Are you sure you want to approve this leave?");
define("LV_APPROVED","You have approved the leave successfully.");
define("LV_REJECTED","You have rejected the leave successfully.");