Something I've noticed in your paste:
if (mysqli_query($conn, $sql)) {
$message = "Records added successfully.";
echo "<script type='javascript'>alert('$message');</script>";
} else {
$message= "ERROR: Could not able to execute $sql. " . mysqli_error($link);
echo "<script type='text/javascript'>alert('$message');</script>";
}
In the first case, you use just <script type='javascript'>
, which may cause some issues as that is not a valid type (check your browser dev console for errors).
In the second case you are not escaping the $sql
portion of your $message
. This will assuredly cause a javascript error you should see in your browsers dev console.
Therefore, try this instead:
if (mysqli_query($conn, $sql)) {
$message = "Records added successfully.";
} else {
$message = "ERROR: Could not execute $sql. " . mysqli_error($link);
}
echo '<script language="Javascript" type="text/javascript">';
echo 'alert('. json_encode($message) .');';
echo '</script>';
I put the echo of script after the if/else, because you will always have a $message to alert, and thus this consolidates code.
-EDIT-
In light of news that you have a header(); redirect at the bottom of your php... which kills any notion of javascript firing off since the web browser never gets that information... this is a possible way to handle it:
echo '<meta http-equiv="refresh" content="1; url=addp.php">';
Tells the browser to refresh to the final destination after spewing out the javascript alert (messy, and ugly).
Another example is to use:
header("Refresh: 1; url=addp.php");
Which puts the refresh timer into the header, instead of the html body. Has the same effect, and is ugly and messy.
The last exemaple is to really restructure your path of execution and what you are doing... for which I will not supply code for. Too much work.