Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'd like to show a message to the user if there has been any problem interacting with the database...

As an example, if I want to delete a record, but this record can't be deleted due to dependencies... I'd love showing a message to the user (something like a messagebox /alert).

The pages I'm working in are all generated by PHP.

The current structure is:

1. In page A I click a link (to delete a database record) that takes me to page B where the deletion code is.
2. In page B the deletion happens (call to the deleting function, check result) and jump back to page A.

What I've done by now works, but would like to know if there is a better way:

1. At the function fail (in page B) I set a session variable to the message I want to show:
PHP
if (deleteRecord()==FALSE) $_SESSION["myMessage"]="Deletion failed.";

2. At the page A I've written:
PHP
if ($_SESSION["myMessage"] != "")
{
  $myMessage= addslashes($_SESSION["myMessage"]);
  echo "<script type='text/javascript'>alert('$myMessage');</script>";
  unset($_SESSION["myMessage"]);
}
Is there a better way to do this?

Thank you very much for your time and help.

What I have tried:

Searched the internet, tried to put a middle page with an onload event...
Posted
Updated 13-Sep-19 2:17am
v8
Comments
ZurdoDev 3-Sep-19 16:24pm    
I don't do php but I imagine that most beginner tutorials would show you how to do that.
Joan M 3-Sep-19 16:33pm    
I'd love that this would be the case...
PHP works on server side and therefore it's complicated to show a messagebox to the user...
I've thought on different ways, but all of them involve a click event and I could try to store a session variable (some kind of global) and then react to that variable in the onload event of the next page visited, but it seems cumbersome to me... (And it has not worked with variable texts).

All in all, having fun.
ZurdoDev 3-Sep-19 16:35pm    
Showing a message is very simple. I think php uses echo. If you want a popup, then yes, there is more to it.
Joan M 3-Sep-19 16:41pm    
You are right! I've updated my question to reflect I want a popup (like an alert).
MadMyche 3-Sep-19 17:47pm    
Is this actual page generated by PHP?

$_SESSION["myMessage"] = "Hellow World";
if (isset($_SESSION["myMessage"]))
{
  $myMessage= addslashes($_SESSION["myMessage"]);
  echo "<script type='text/javascript'>alert('$myMessage');</script>";
  unset($_SESSION["myMessage"]);
}
 
Share this answer
 
Comments
Joan M 13-Sep-19 7:44am    
Why is this better than what I've done?
Richard Deeming 13-Sep-19 11:42am    
It's not - it's identical to the code you posted in the question.
Joan M 13-Sep-19 11:53am    
Yes... I wanted to hear an explanation from the poster... it would have been fun... ^^¡
Hi bro
The another way is the use chrome notification.
Adding Push Notifications to a Web App  |  Web Fundamentals  |  Google Developers[^]
 
Share this answer
 
Comments
Joan M 13-Sep-19 11:54am    
I want a solution that works everywhere... but thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900