coupe-r Posted September 17, 2010 Share Posted September 17, 2010 I am trying to display a js dialog box when someone wants to delete something. Currently I have it all with buttons and PHP in the background. When they click the delete icon, I want a dialog box to appear with OK or Cancel. On cancel, return back to the same page, If OK, I want it to run the PHP/mysql code that deletes the record from the DB. Once deletes, I want the box to appear again saying it was deleted successfully. I found this but now sure how to make it work. function show_confirm() { var r=confirm("Press a button"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); } } Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/ Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 The code that you have there is to prompt a user for an answer. If you wanted you could bind that the onclick event of an element on the page. However, if you want to display a dialog when it is done that is a little more confusing as you will have to do a page refresh and then have that javascript popup open on page reload. There is a more elegant solution that I could suggest. The technology that you need to look into is ajax. It allows you to communicate with php via javascript in a sense. One way people use ajax is with ajaxobject (just google it) and another way is with jQuery. If you are a beginner and would like to play around with using php and javascript in combination I would look into jquery as it is really easy to get a graspe on. If you do decide to look into jQuery (http://jquery.com) you should take a look at the $.ajax() method or one of my personal favorites the $.load() method. Post back when you have more questions and I would be more than willing to help. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111934 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Well, maybe Ill just have the box pop up, when they click yes, delete the record and forget about the other pop up that says its deleted. would you still recommend jQuery? Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111937 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 Personally I like jQuery because it has some really nice built in features that make javascript really easy and fast to use. However, for this I would use a function similiar to what you have. So first thing I would do is change your submit button to just a regular button and then add the javascript call to it passing the url for the delete. <input type="button" onClick="delete('index.php?delete=true&id=5');" /> Then you can setup the javascript function just add this somewhere in the page. <script type="text/javascript"> function delete(url) { var answer = confirm("Are you sure you would like to delete this item?") if (answer){ window.location = url; } else{ return false; } } </script> Please ask if you have any questions. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111939 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Well, what I have is a .png icon and when you click it, the URL changes to del=true?id=123, instead of an actual delete button. Here is what I have "<a href='index.php?id=".$id."&del=1'>".'<img src="../images/delete_sm.png" title="Delete Application" alt="Delete Application" width="20" height="20" border="0" />' Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111941 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 Same concept just a different method. If my assumption is correct this is coming from an echo statement. Try this: '<a onclick="delete(\'index.php?id='.$id.'&del=1\');"><img src="../images/delete_sm.png" title="Delete Application" alt="Delete Application" width="20" height="20" border="0" />' Then somewhere on that page add that javascript code that I previously posted outside of the php tags. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111945 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 I really appreciate your help. Unfortunately, nothing happens when I click the icon. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111947 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 Can you post your full code so that I can see where you went wrong? Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111949 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 $del_text = '<a onclick="delete(\'index.php?id='.$id.'&del=1\');">';$del_text.'<img src="../images/delete_sm.png" title="Delete Application" alt="Delete Application" width="20" height="20" border="0" /></a>'; Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111951 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 Ok so I figured it out. It was my fault, apparently delete isnt a valid name for a function because it is already used I am guessing lol. Also you have to add the href attribute and just set it to javascript:void(0); (means do nothing). change it to this: $del_text = '<a onclick="deleteApplication(\'index.php?id='.$id.'&del=1\');">'; Then change the javascript functions name to deleteApplication instead of delete. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111957 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Alright, now we're getting somewhere. OK, now when the dialog box pop up and I hit OK, it takes me to index.php?id=98&del=1. Is there a way, instead of taking me to a URL, that it executes the delete query? Or is my only option to have an if(isset($_GET['id']) && isset($_GET['del'])) { DELETE CODE HERE; } Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111959 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 Like I was saying before the only way to do that is by using ajax to send the request to the page without using a reload. However, If you are just looking for the user to return the the same page you could just use a redirect. For example: <?php//delete querymysql_query("DELETE something FROM something WHERE somthing='something'");//redirectheader("Location:index.php");?> All this does is it allows you to delete the entry but then redirect them to the same page so they dont notice where they went when they got redirected. Obviously you would have to change the location to the correct page. Another thing to keep is that you would want this code at the very top of the page because the header function can only be used before anything is out put is sent to the page. Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111960 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 OK, Ill just leave it so when I click ok it give me the ...id=123&del=1. Then I will do this: if(isset($_GET['id']) && isset($_GET['del']) && $_GET['del'] == 1) CODE HERE header(SAME PAGE) Thanks for all your help !!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111962 Share on other sites More sharing options...
ngreenwood6 Posted September 17, 2010 Share Posted September 17, 2010 I would add an is_numeric check to the id if they are always numeric to ensure valid data: if(isset($_GET['id']) && is_numeric($_GET['id']) && isset($_GET['del']) && $_GET['del'] == 1) Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111965 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Thank you very much!!!! Quote Link to comment https://forums.phpfreaks.com/topic/213632-running-php-inside-js-dialog-box/#findComment-1111972 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.