我在提交后编写了一条消息,有一些麻烦。
以下是代码:
<?php
if(isset($_POST['selected_name'])){
$stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?");
$stmt->bind_param('i', $_POST['selected_name']);
$stmt->execute();
$stmt->close();
}else{
echo "<h2>Artikel gelöscht!</h2>";
header ("refresh:3; url=e3_artikel_loeschen.php");
}
?>
<form action="" method="POST">
<p>Artikel:
<?php
$stmt = $mysqli->prepare("SELECT anr, name FROM artikel");
$stmt->execute();
$stmt->bind_result($anr, $name);
echo "<select name='selected_name'><br />";
while ($stmt->fetch()) {
echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>';
}
echo "</select>";
?>
<input type="submit" name="submit" value="Datensatz löschen" />
</form>
</body>
</html>
我意识到这样的消息总是在那里而且不会消失。我希望对它进行编码,以便在提交后只显示消息,然后返回主页面。谢谢您的帮助。
答案 0 :(得分:0)
您尝试在发送标题后发送标题(通过echo
消息)。将header
行放在echo
之前,它应该有效。
附注:如果您计划维护的代码多于此类代码,请了解如何从视图中分离模型和控制器。学习和使用PSR-1和PSR-2也不会受到伤害。
答案 1 :(得分:0)
echo '<script>
alert("You have selected '.$language.'");
window.location = "'.$SITE_URL.'index.php?view=view_service."
</script>';
答案 2 :(得分:0)
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$phoneno = mysqli_real_escape_string($con, $_POST['phoneno']);
$status = mysqli_real_escape_string($con, $_POST['status']);
$sql = "SELECT * FROM register WHERE email='".$email."' AND status='1'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
$errormsg="email already exist";
}
else{
$sql="INSERT INTO register (name,email,password,phoneno,status) VALUES ('$name','$email','$password','$phoneno','1')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
exit;
mysqli_close($con);
}
?>
<form role="form" action="" autocomplete="off" method="POST">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="name" class="form-control" name="name" placeholder="Company Name" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="email address" required>
</div>
<?php echo $errormsg;?></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="tel" id="tel" class="form-control" name="phoneno" pattern="\d{10}" placeholder="Please enter a ten digit phone number" required />
</div>
<span class="help-block"></span>
<input class="form-control" value="" type="hidden" name="status">
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign Up</button>
</form>
答案 3 :(得分:0)
在这种情况下,您可以使用javascript进行重定向,延迟时间为2秒:
echo "<h2>Artikel gelöscht!</h2>";
echo '<script>setTimeout(function () {
window.location.href = "e3_artikel_loeschen.php"";
}, 2000); </script>';
答案 4 :(得分:0)
<击> 移动击>
<击>echo "<h2>Artikel gelöscht!</h2>";
header ("refresh:3; url=e3_artikel_loeschen.php");
内部
if(isset($_POST['selected_name'])){}
并将其重新排序为
header ("refresh:3; url=e3_artikel_loeschen.php");
echo "<h2>Artikel gelöscht!</h2>";
击> <击> 撞击> 更新:试试这个
<?php
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['selected_name'])) {
$stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?");
$stmt->bind_param('i', $_POST['selected_name']);
$stmt->execute();
$stmt->close();
header ("refresh:3; url=e3_artikel_loeschen.php");
echo "<h2>Artikel gelöscht!</h2>";
}
?>
答案 5 :(得分:0)
我用这种方式解决了我的问题:
我在主页外写了一段代码:
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html" />
<meta charset="utf-8" />
<?php
require_once("db.inc.php");
?>
<link rel="stylesheet" type="text/css" href="styles2.css" />
</head>
<body>
<form action="loeschen.php" method="POST">
<p>Artikel:
<?php
$stmt = $mysqli->prepare("SELECT anr, name FROM artikel");
$stmt->execute();
$stmt->bind_result($anr, $name);
echo "<select name='selected_name'><br />";
while ($stmt->fetch()) {
echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>';
}
echo "</select>";
?>
<input type="submit" name="submit" value="Datensatz löschen" />
</form>
</body>
</html>
这是主页的代码:
{{1}}
希望这对其他人也有帮助。