Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

showing sweet alert after php database record delete

Posted on 2016-10-10
5
Medium Priority
?
1,686 Views
Last Modified: 2016-10-10
After I click on a button to delete a single record, I want a sweet alert to display but I don't know if it can be done using php or if it would have to be done using ajax?

This is the button which is in a form with method of "post":

 <input type="submit" class="btn btn-danger btn-fill" name="delete_comment" value="Delete message">

Open in new window


Here is my function:

function delete_contact_message($link) {
	
	if(isset($_POST['delete_comment'])){
		
		$stmt = $link->prepare("DELETE FROM `contact_form` WHERE `contact_id` = ?");
		$stmt->bind_param("i", $contact_id);
		$contact_id = clean_input($_GET['contact_id']);
		$stmt->execute();
		$stmt->close();
		
		
	} 
}

Open in new window


And this is what I want to appear once the function runs and the record is deleted:

	<script>
	swal(
  'Success!',
  'Message deleted.',
  'success'
)
	</script>

Open in new window


To see what sweet alert is, go to :

https://limonte.github.io/sweetalert2/
0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41837423
OK, there are a few things to think about here.  

Sweet Alert looks susceptible to using JSON, and PHP can generate the JSON strings.  I haven't used it, but that seems like a good way to think about displaying custom messages from the PHP AJAX script.

The instructions appear to be out-of-order in the PHP function.  This is why we use error_reporting(E_ALL), so we can catch things that are amiss.  $contact_id is undefined in the bind_param() function call.

You might or might not care whether the DELETE query works.  If you care (and maybe want to adjust the Sweet Alert output) you would want to test the query for success or failure, and perhaps get the number of rows that were deleted.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41837430
Hi Ray,

My query is working fine and the delete works as it should. I used the W3 schools example as a base whereby they name their variables after they use bind_param. That's the only reason I defined $contact_id after the bind_param. You can check it out here:

http://www.w3schools.com/php/php_mysql_prepared_statements.asp

But, where should I put it then? Before I even start the $link->prepare()   ?

Oh, I was meant to put LIMIT 1 at the end of the SQL statement.. Will fix that on my end.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41837450
Yeah, that part about naming variables after using them may be one of those things that works, but makes the computer science part of me itch.  If you're going to filter an external variable with something like this...
$contact_id = clean_input($_GET['contact_id']);

Open in new window

... my instinct would be to put that before the bind statement.  I think most "linters" would flag an undefined variable.

I think your instincts are good about using AJAX.  The order of events is such that you will need a PHP server-side event to occur after the web page is loaded.  This article explains the HTTP request/response sequence.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html

If you're looking for some ideas around AJAX and JSON, this might be helpful.
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41837462
Thanks for the tip Ray, I will do it before the bind statement from now.

I did a very basic course on ajax yesterday, so basic that it was only like an hour or something. I was hoping I could try use that knowledge but I think I need to learn some more about ajax and jquery. Anyway, I tried something like this but I get a blank screen as a result.

I think I will leave the ajax part out for now because I actually don't know enough to pull it off. I will have to do some more learning. I will give you points anyway since you advised me that I should be declaring my variables before binding.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41837530
Here's the basics of jQuery and AJAX.  FWIW, it took me a long time to find any decent examples that showed the patterns from beginning to end.
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html

Cheers, ~Ray
1

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question