The first code block is the include for the second code block. I am trying to te following working:
if ($db->query($insert)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
The page works and successfully inserts data into the database but the echo message for successful insertion does not work. I get the "Data not successfully Inserted" message
Any ideas, please
DatabasesPHPMySQL Server
Last Comment
doctorbill
8/22/2022 - Mon
Julian Hansen
Several issues here.
1. Your insert code runs in a loop - do you want a response message for each iteration
2. Are the inserts related - i.e. if one fails must none of them go through? If so you should consider wrapping them in a transaction
3. Reporting output to the client should not really be done using the method you are trying. It is best to keep the server and client side code where possible. It might seem to make sense that you want to output a response at the time you do the insert but when you consider the fact that the entire PHP script must terminate before anything is actually sent to the browser and starts rendering - then it makes sense - they are two completely separate processes. One (server) passes off its output to the other (client) and only then does the client render the page at which point the alert's show.
The best way of doing this is calling the server side script with an AJAX call, handling the response and using client side code to report the result to the user.
doctorbill
ASKER
I think all I need is to notify the user after all the code has run or just a redirect to another page
1. Your insert code runs in a loop - do you want a response message for each iteration
2. Are the inserts related - i.e. if one fails must none of them go through? If so you should consider wrapping them in a transaction
3. Reporting output to the client should not really be done using the method you are trying. It is best to keep the server and client side code where possible. It might seem to make sense that you want to output a response at the time you do the insert but when you consider the fact that the entire PHP script must terminate before anything is actually sent to the browser and starts rendering - then it makes sense - they are two completely separate processes. One (server) passes off its output to the other (client) and only then does the client render the page at which point the alert's show.
The best way of doing this is calling the server side script with an AJAX call, handling the response and using client side code to report the result to the user.