在您按下提交信息后,我会尝试显示一条消息
这个名字已经存在,使用另一个
或
2.添加名称!
html
<form action="add.php" method="post">
<input type="text" name="name" id="name" class="form-control" required>
<input class="btn btn-default" type="submit" value="Submit">
</form>
php
$connectie=mysqli_connect("localhost","usernam","pw","db");
$name = $_POST['name'];
$name = stripslashes($name);
$name = mysql_real_escape_string($name);
$check_if_exists="SELECT * FROM names WHERE name = '$name'";
if($data[0] > 1) {
echo"already exists";
} else {
$newUser="INSERT INTO users (name) values('$name')";
if (mysqli_query($connectie,$newUser))
{
echo "name is registerd";
}
else
{
echo "error<br/>";
}
现在它在add.php上发布了echo,而不是在表单所在的页面上。我怎么去那里?
答案 0 :(得分:0)
您应该考虑使用会话。将session_start()
添加到两个文件的顶部。
if(mysqli_query($connectie, $uewUser))
{
$_SESSION['flash'] = 'name is registered';
}
else
{
$_SESSION['flash'] = 'error<br/>';
}
然后在包含表单的页面中,执行以下检查以打印数据。
if(isset($_SESSION['flash']))
{
echo $_SESSION['flash'];
unset($_SESSION['flash']);
}
这将确保仅为下一个请求保留会话消息。
答案 1 :(得分:0)
您可以使用session / sessionflashdata。
首先,您没有正确运行选择查询。应该是:
$check_if_exists="SELECT * FROM names WHERE name = '$name'";
$count = mysql_num_rows($check_if_exists);
if($count != 0) { // this means that the name already exists:
$_SESSION['msg']="The name already exists. Please try another name";
} else {
//perform the insert query and have the session msg
$_SESSION['msg']="The name was inserted successfully";
}
header('Location: form.php');
// Now in your form page just above the form, have a div to display this message as:
<div class="msg">
<?php if(isset($_SESSION['msg']))
{
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
</div>
NOTE: you must start the session on both the pages. Have this code on top of both the pages
<?php
session_start();
?>
&#13;