ayok Posted September 24, 2007 Share Posted September 24, 2007 Hi there, I have a question regarding the confirm box of javascript. I'd like to pop up a confirm box when the admin wants to delete an item. So when the person click on "ok", the item will be delete, but if "cancel" is clicked, then it would stay on this page. But I cannot get it works. I've tried this script: <script type="text/javascript"> function disp_confirm(){ var name=confirm("You're going to delete this item, would you like to process it?") if (name==true){ window.location("delete.php?id=$data[id_cat];") } } </script> And this code on the delete: <a href='' onclick='disp_confirm()'>delete</a> Could anybody help me with this? Thank you, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 If you want to cancel the default event you need to return either true or false from the event handler. I forget which value (true, false) is used to cancel the default action associated with the onclick event. Just try one and then the other. Quote Link to comment Share on other sites More sharing options...
ayok Posted September 24, 2007 Author Share Posted September 24, 2007 Hi roopurt, Sorry I don't understand your answer. Could you give me example? ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 <script type="text/javascript"> function disp_confirm(){ var name=confirm("You're going to delete this item, would you like to process it?") if (name==true){ window.location("delete.php?id=$data[id_cat];") }else{ return true; // change true to false if it doesn't work } } </script> Quote Link to comment Share on other sites More sharing options...
ayok Posted September 24, 2007 Author Share Posted September 24, 2007 <script type="text/javascript"> function disp_confirm(){ var name=confirm("You're going to delete this item, would you like to process it?") if (name==true){ window.location("delete.php?id=$data[id_cat];") }else{ return true; // change true to false if it doesn't work } } </script> Hi thanks roopurt, It is return to the page when i click "cancel", but it doesn't delete when i click on "ok". Is there something wrong with the window location? ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 I think it's supposed to be: window.location.href = <rvalue>; Quote Link to comment Share on other sites More sharing options...
ayok Posted September 24, 2007 Author Share Posted September 24, 2007 I think it's supposed to be: window.location.href = <rvalue>; Thanks roopurt, It seems the javascript is working, but i can't get the url "delete.php?id=$data[id_cat];" Is there any help to correct this? ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 What does the URL look like after you click the delete link and then click OK to confirm? Quote Link to comment Share on other sites More sharing options...
ayok Posted September 24, 2007 Author Share Posted September 24, 2007 It goes to index.php.. its an admin folder.. www.myweb.com/admin/ ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 It would be hard to say what's going on without seeing some more of your code. Or you can try changing the line: window.location("delete.php?id=$data[id_cat];") to window.location.href="http://www.yahoo.com/" Just to make sure it's redirecting correctly when you press OK. Quote Link to comment Share on other sites More sharing options...
ayok Posted September 24, 2007 Author Share Posted September 24, 2007 No.. it doesn't go to yahoo.com. So it goes to empty url. Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 Hi, So far my javascript is: function disp_confirm(){ var name=confirm("Do you really want to delete this?") if (name==true){ window.location("delete.php?id=$data[id_cat]") } else{ return true; } } and the link where to click: <a href='' onclick='disp_confirm()'>delete</a> The confirm box is popuped but if i click on ok or cancel it will open the index page. Anybody can help me? Thank you, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 You don't call window.location as if it's a function. Do you see any difference at all between the following? // wrong! window.location("delete.php?id=$data[id_cat];") // correct! window.location.href = "http://www.yahoo.com"; Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 Hi roopurt, I have try the code window.location.href = "http://www.yahoo.com"; . But it doesn't go to yahoo.com. It stays in the same page. thank you, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 Try this: function disp_confirm(){ var msg = "Do you really want to delete this?"; if (confirm(msg)){ window.location("delete.php?id=$data[id_cat]"); return false; } else{ return true; } } Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 Hi roopurt, It still doesn't work. I try in IE it gives an error: Object doesn't support this action. thanks, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 <html> <head> <script type="text/javascript"> function disp_confirm(){ var msg = "Do you really want to delete this?"; if (confirm(msg)){ window.location.href = "http://www.yahoo.com"; } return false; } </script> </head> <body> <a href='' onclick='return disp_confirm();'>delete</a> </body> </html> Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 Hi roopurt, After I tried with html page, it works. So I think the problem is that I'm doing it in php script. Is there anyway to use it in php script. Because the item that will be deleted is in php. thank you, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 Post your PHP script. Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 <?php $show = mysql_query("SELECT service.*,category.name FROM service LEFT JOIN category on service.category=category.id_cat WHERE category='$id'") or die(mysql_error()); echo "<table border=0><tr bgcolor='#D0DCE0'><th>Service</th><th>Description</th><th>Duration (minutes)</th><th>Prices</th></tr>"; while (($data = mysql_fetch_array($tampil)) && (mysql_num_rows($tampil) > 0)) { echo "<tr>"; echo "<td bgcolor='#D47FFF'>$data[name_serv]</td>"; echo "<td bgcolor='#FF55FF'>$data[desc_serv]</td>"; echo "<td bgcolor='#D47FFF'>$data[minutes]</td>"; echo "<td bgcolor='#FF55FF'>$data[price]</td>"; echo "<td><a href='' onclick='disp_confirm()'>delete</a><br><a href=edit_service.php?id=$data[id_serv]>edit</a></td></tr>"; } echo "</table>"; }?> <script type="text/javascript"> function disp_confirm(){ var msg = "Do you really want to delete this?"; if (confirm(msg)){ window.location("delete.php?id=$datakategori[id_cat]"); } return false; } </script> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 Is $datakategori[id_cat] a global PHP variable? Quote Link to comment Share on other sites More sharing options...
ayok Posted September 25, 2007 Author Share Posted September 25, 2007 Sorry, I missed some lines above it <?php $show1 = mysql_query("SELECT * FROM category WHERE id_cat='$id'") or die(mysql_error()); $datakategori = mysql_fetch_array($show1); echo "<H3>$datakategori[name]</H3><br><img align='left' src='../pics/$datakategori[pic]'<br><H5>$datakategori[description](<a href=edit_cat.php?id=$datakategori[id_cat]>edit kategori</a> | <a href=add_cat.php>add kategori</a>)<H5>"; $show = mysql_query("SELECT service.*,category.name FROM service LEFT JOIN category on service.category=category.id_cat WHERE category='$id'") or die(mysql_error()); echo "<table border=0><tr bgcolor='#D0DCE0'><th>Service</th><th>Description</th><th>Duration (minutes)</th><th>Prices</th></tr>"; while (($data = mysql_fetch_array($tampil)) && (mysql_num_rows($tampil) > 0)) { echo "<tr>"; echo "<td bgcolor='#D47FFF'>$data[name_serv]</td>"; echo "<td bgcolor='#FF55FF'>$data[desc_serv]</td>"; echo "<td bgcolor='#D47FFF'>$data[minutes]</td>"; echo "<td bgcolor='#FF55FF'>$data[price]</td>"; echo "<td><a href='' onclick='disp_confirm()'>delete</a><br><a href=edit_service.php?id=$data[id_serv]>edit</a></td></tr>"; } echo "</table>"; }?> <script type="text/javascript"> function disp_confirm(){ var msg = "Do you really want to delete this?"; if (confirm(msg)){ window.location("delete.php?id=$datakategori[id_cat]"); } return false; } </script> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 26, 2007 Share Posted September 26, 2007 The problem is that you're trying to set the $_GET parameter in the Javascript but you are not in PHP mode (because you've stepped out of PHP with the ?>). <script type="text/javascript"> function disp_confirm(){ var msg = "Do you really want to delete this?"; if (confirm(msg)){ window.location.href = "delete.php?id=<?php echo $datakategori['id_cat'];?>"; } return false; } </script> Quote Link to comment Share on other sites More sharing options...
ayok Posted September 26, 2007 Author Share Posted September 26, 2007 Hi roopurt, its the same. it doesnt go to delete.php. The problem is, it results the same if I set the window location to yahoo.com, except I did it in html page. thank you, ayok Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 26, 2007 Share Posted September 26, 2007 Perhaps change the target location to an absolute URL? I'm not sure why it wouldn't work at this point. Quote Link to comment 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.