简体   繁体   中英

Display success message after form submission in mysqli prepare

How to display success message after record is inserted successfully. This code work perfectly but doesn't shows any message.

error_reporting(E_ALL ^ E_NOTICE);
$name=$_POST['name'];
$email=$_POST['email'];
$massage=$_POST['massage'];
$organization=$_POST['organization'];

$stmt = mysqli_prepare($connection, "INSERT INTO massage_board(name,email,organization,massage)VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'ssss', $name, $email, $organization, $massage);

/* execute prepared statement */
mysqli_stmt_execute($stmt);
if($stmt->execute()){
    echo "<div id=\"success\"><h2>Your message has been sent. Thank you!</h2></div>";

header('Location:contact.html');
}else{
echo "Please retry";    
}

Your message display but It redirect to another page so you do not see it try following code without redirect on page.

if($stmt->execute()){
    echo "<div id=\"success\"><h2>Your message has been sent. Thank you!</h2></div>";

header('Location:contact.html');   <--- Remove this line
}

It went to contact.html immediately after shown the message, so you couldn't see the message :) You should do this by Javascript

echo "<div id=\"success\"><h2>Your message has been sent. Thank you!</h2></div>";
echo "<script>setTimeout(\"location.href = 'http://www.example.com/contact.html';\",1500);</script>";

UPDATE: Try this:

$stmt = mysqli_prepare($connection, "INSERT INTO massage_board(name,email,organization,massage)VALUES (?, ?, ?, ?)");
$stmt->bind_param('ssss', $name, $email, $organization, $massage);

if($stmt->execute()){
    echo "<div id=\"success\"><h2>Your message has been sent. Thank you!</h2></div>";

echo "<script>setTimeout(\"location.href = 'http://www.example.com/contact.html';\",1500);</script>";
}else{
echo "Please retry";    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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