Jscript 'confirm box' FORM handler- need help!

Associate
Joined
25 Aug 2004
Posts
163
Location
Reading
I need a confirmation box in order to ascertain whether someone wants to delete something using a form. I've managed to get a confirmation page to redirect on both ok | cancel and go to the target page on ok | cancel. But haven't managed to delete on ok, and stay on the same page on cancel :\ !

Here's the code:

Code:
<SCRIPT language="JavaScript"> 
<!-- function confirm_del() 
{ 
           var where_to= confirm("Do you really want to delete?"); 
            if (where_to== true) 
           {
                   document.delete.action = 'delete.php';                                                
                    document.delete.submit(); // Submit the page 
           }
          
           else 
          { 
                 document.delete.action = 'currentpage.php'; 
                 document.delete.submit(); // Submit the page 
          }
 }
 //-->
 </SCRIPT>

...and here's the form (it takes a dynamically generated php hidden field as a variable to pass to the delete page. I took the form action out as it defaulted to delete whatever the confirm box did and tried to pass it to the jscript to decide the form action.

Code:
<form name="delete" method="post"> <input type="hidden" name="del_date" value="<?php echo $row['del_date'];?>"/> <input type="submit" value=" Delete " onclick="confirm_del()"/> </form>

I'm a jscript noob and have only ever done server side php and ASP before! I'm sure it's quite a simple solution and I'm being stupid, but found nothing on Google.

Thanks in adv.

Gareth
 
www.tizag.com have some pretty good tutorials :) If that doesn't work, you could also try

Code:
document.getElementById['delete'].action = "blah";
document.getElementById['delete'].submit();

along with the

Code:
<form id="delete method="post">

I have no idea whether it will work but it's worth a try :)
 
So do you now have the code as

Code:
<script language="javascript"> 
function confirm_del() 
{ 
           var where_to= confirm("Do you really want to delete?"); 
            if (where_to == "true") 
           {
                   document.getElementbyId['delete'].action = "delete.php";
                   document.getElementbyId['delete'].submit();

           }
          
           else 
          { 
                 // Do Nothing  
          }
 }
 </script>

Code:
<form id="delete" method="post">
    <input type="hidden" name="del_date" value="<?php echo $row['del_date'];?>"/>
    <input type="submit" value=" Delete " onclick="confirm_del()"/> 
</form>

I don't think there's any point doing anything if the user clicks cancel unless you need it do for something else. One thing you could do to see how far the script is getting is after the document.getElementbyId['delete'].submit();, stick an alert('test'); in and click ok on the confirm box and see if the message pops up- gives you an idea of how far it's getting then :)
 
The getElementById() example above should be using parentheses, not square brackets:

Code:
<script type="text/javascript">
function confirm_del(){
	return confirm("Do you really want to delete?");
}
</script>
Since you don't need to do anything if the user cancels, then just don't do anything in the function or provide a return value to use.

And your form. Note the onclick event receiving the return value -if it's false above, it'll do nothing/not submit -, and also not the addition of the ID so you can better access the form through the DOM:
Code:
<form name="delete" id="delete" method="post">
	<input type="hidden" name="del_date" value="your code here"/>
	<input type="submit" value=" Delete " onclick="return confirm_del();"/>
</form>
This method accounts for users without javascript - if it's not available, the form will submit without warning, but at least it will be functional. There are cleaner and more structured approaches to this.

With regard to a good book, I highly recommend Jeremy Keith's DOM Scripting book. Well written, and doesn't go overboard on [unnecessary] heavy technical detail. Great introduction to javascript, and as it's fairly recent it takes into account modern methods for implementation.
 
Last edited:
Back
Top Bottom