Marco Marco - 5 months ago 24
PHP Question

Message after submitting

I have some Troubles with coding a message after submitting.

Here is the Code:

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


I realize that this way the message is always there and don't disappear. My wish is to Code it, in order to let appear only the message after submitting, and then return to the main page. Thank you for your help.

Answer

Move

echo "<h2>Artikel gelöscht!</h2>";
header ("refresh:3; url=e3_artikel_loeschen.php");

Inside

if(isset($_POST['selected_name'])){}

and Reorder it to

header ("refresh:3; url=e3_artikel_loeschen.php");
echo "<h2>Artikel gelöscht!</h2>";

UPDATE: Try this

<?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>";   
}
?>

PHPFIDDLE EXAMPLE