<?php if(isset($_POST['submit'])){ $sql=mysqli_query($mysqli,"INSERT INTO `table name`(`name`,`email`,`password`,`phone`)VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['password']."','".$_POST['phone']."')"); if($sql){ echo "success"; }else{ echo "error"; } } ?>
I have applied this code to submit data into the database. After inserting data, I want to display a success message on the form page. But it goes to the submit.php page. How can I redirect my form page with a success message?
Apply this code.
<?php if(isset($_POST['submit'])){ $sql=mysqli_query($mysqli,"INSERT INTO `table name`(`name`,`email`,`password`,`phone`)VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['password']."','".$_POST['phone']."')"); if($sql){ $_SESSION["success"]="Insert Successfully"; header("form.php"); }else{ $_SESSION["error"]="Insert Error!!"; header("form.php"); } } ?>
In the form page apply this code to display a success message after the form submit.
<?php if(!empty($_SESSION["success"])) { echo $_SESSION["success"]; } ?>
Question and answer is powered by AnsPress.io