Adamhumbug Posted December 4, 2021 Share Posted December 4, 2021 HI all, I am looking for some best practice help here. I have a php function called with AJAX and on failure, i would like it to create an alert bar on the page that fades out. I have the know how to show the message on the page but i am doing this using a prepend. I know that this should be handled with JS but i am not sure of the best practice to achive what i am after. Any pointers here appreciated. I have attached the function incase that is useful. function showNewItemAfterBCScan($serial_number, $personId){ include 'includes/dbconn.php'; $out="<div id='assigningKitAlert' class='alert alert-warning'>Bar Code Incorrect</div>"; $stmt = $conn -> prepare(" SELECT ks.id, ks.serial, km.model_name, kf.kit_family from kit_serial ks inner join kit_model km on ks.kit_model = km.id inner join kit_family kf on ks.kit_family = kf.id where ks.serial = ? "); $stmt -> bind_param('i', $serial_number); $stmt -> execute(); $stmt -> bind_result($id, $serial, $mname, $kfamily); if($stmt -> fetch()){ $out = <<<OUTPUT <div class='row bg-secondary m-1 center-all'> <div class='col-2 text-center'> <img class='device-thumb py-3' src='img/DP4800e-Front.png' alt=''> </div> <div class='col-10'> <div class='row'> <div class='col-4'> <h4>$mname</h4> </div> <div class='col-4'> <h4>$kfamily</h4> </div> <div class='col-2'> </div> <div class='col-2'> <h1>x 1</h1> </div> </div> <div class='row'> <div class='col-12'> $serial </div> </div> </div> </div> </div> OUTPUT; assignKit($serial_number, $personId); } $stmt -> close(); return $out; } Quote Link to comment https://forums.phpfreaks.com/topic/314271-create-alert-in-php-and-then-remove-from-dom/ Share on other sites More sharing options...
Solution Barand Posted December 4, 2021 Solution Share Posted December 4, 2021 You could try something like this example <?php if (isset($_GET['ajax'])) { if (rand(0,1)) { exit("Bar Code Incorrect"); } else { exit("Everything OK"); } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#test").click( function() { $.get( "", {"ajax":1}, function(resp) { if (resp=="Bar Code Incorrect") { let err = $("<div>", {"id":"error", "class":"alert alert-warning", "html":resp}) $("#output").html(err) $("#error").fadeOut(4000) } else { $("#output").html(resp) } } ) }) }) </script> <style type='text/css'> .alert { background-color: red; color: white; padding: 16px; } </style> </head> <body> <button id='test'>Test</button> <div id='output'></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/314271-create-alert-in-php-and-then-remove-from-dom/#findComment-1592423 Share on other sites More sharing options...
Adamhumbug Posted December 4, 2021 Author Share Posted December 4, 2021 Thanks for this, ill give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/314271-create-alert-in-php-and-then-remove-from-dom/#findComment-1592424 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.