Jump to content

Create alert in php and then remove from dom


Adamhumbug
 Share

Go to solution Solved by Barand,

Recommended Posts

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;


}

 

Link to comment
Share on other sites

  • Solution

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>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.