What is the good solution to replace alert?

2

In a question , how to do alert , has one answer, the most voted, that says:

  

The alert is not a good solution to show some message to the user in the browser but I will answer what you asked ...

What would be the good solution to show some message to the user then?

    
asked by anonymous 26.06.2017 / 19:56

2 answers

5

The message should be displayed on the page itself, preferably close to the data that gave rise to the error, highlighting, perhaps temporarily and in a way that does not obstruct the normal use of the page. And this was the spirit of the answer, I can assure you:)

Ofcoursethereareexceptions.Theremaybesomecasethatopeningastandardbrowseralertpatternmaybemoreappropriate.Butthisisapracticeconsideredoldandunfriendly.

Eveninthiscasewhereamodalisthemostsuitabletodayitisconsideredmorepresentabletomakeawindowwithinthepagewithalookthatisconsistentwiththerestofthepage.

PHPisanbackendtechnologysoitmakeslittledifferencetohowitispresentedtotheuser.Infrontendyoucancreateauserexperienceanywayyouwant.ThisisdonewithJavaScript,HTML,CSS.NotethattheoriginalquestionwasspeakinginJavaScript,notPHP.PHPdoesnotevenknowwhat'srunningontheclient.

Thereareaninfiniteamountofwaystodothis,searchinghereinSOptalreadyfindsseveral( here and here ).

    
26.06.2017 / 20:02
0

p>
<?php
   echo "<script>alert('bem vindo ao site');</script>";
?>

It is not very recommended to send a message through the alert, this is the user can disable the alert, so it can happen that he does not receive his message the second time, a way to get around this would be a hidden part of the page with the message , example a div

<div id="kodomsg" style="display: none">
senha invalida
</div> 

When you need to display that message simply simply display it, you can use JS to do it dynamically

<script>
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "block"; //"none" para ocultar
</script>

You can create a function to show / hide and call it for a specific event, example as soon as you press a button

<div id="kodomsg" style="display: none">
era uma vez um botão ... fim da historia
</div> 
<input type="button" value="aperte" onclick="kodofun()">
<script>
function kodofun(){
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "block"; //"none" para ocultar
}
</script>

It does not necessarily have to be static You can change the message itself dynamically by JS too

<div id="kodomsg" style="display: none">
senha invalida
</div>
<script>
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.innerHTML = "essa é a nova msg";
</script>

You can even create something like alert, let the div float by overlapping the page with a button to close it using js and css, you can even customize it the way you like

<style>
#kodomsg{
    background-color: blue;
    width: 200px;
    height: 60px;
    position: absolute;
    left: 20%;
    top: 20%;
}
</style>

<div id="kodomsg">
bem vindo ao site<br>
<input type="button" value="fechar" onclick="kodofechar()">
</div> 

<script>
function kodofechar(){
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "none";
}
</script>

There are many interesting ways to do this as there are many applications for this ^^ (ps: my example is ugly for damn kkk)

    
27.06.2017 / 10:12