Results 1 to 6 of 6

Thread: Javascript method confirm() problem

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default Javascript method confirm() problem

    The code does not work, and I have tried many times without success. I'm using this to confirm deletion of records in a MySQL database, so it's important for the users.

    In my php script (and in the resultant html code) I have this:
    Code:
    echo('<div class="managedel"><input type="Submit" name="file4" value="Delete" onClick="return confirmDelete();"></div>');
    And in my javascript section I have this:
    Code:
        myWindow = window.open("", "responseWindow", 'toolbar, width=200, height=100')
    
        function confirmDelete() {
            myWindow.document.write("Click 'O.K'. to delete or 'Cancel' to resume")
            var answer = confirm("Are you sure you want to delete these records?");
            if(answer) {
                document.location = <? echo $_SERVER['PHP_SELF'];?>;
            }
            else {
                return false;
            }
        }
    Nothing happens, not even the pop-up window. Could it be that the method in not a part of FF or IE any longer? Is there another substitute? Oh, BTW, I have tried to call the function with onSubmit, as well as onClick.....
    Last edited by Strangeplant; 10-02-2008 at 03:50 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Easily tested but yes, the confirm() method is still part of javascript in those and virtually all browsers.

    However, unless your entire page is inaccessible to non-javascript enabled browsers, this is a pretty bad idea in the first place. It would be better, if you want confirmation in a case like this to use all PHP. Have the first delete request submit the info on what is to be deleted to a page that asks, "Do you really want to delete whatever?" One form on that page will actually delete the info, it's submit button could be value="Yes". Another form on that page could have a submit button value="No" which would return to the previous page (with or without the original information entered on that page posted back to it).

    To your question from the perspective of javascript though, the onclick event of a submit button generally will not fire when it is clicked, though may. However the form will still submit. What fires and controlls the form's submission (in most cases) is the form's onsubmit event:

    Code:
    <form action="whatever" method="post" onsubmit="return something();">
    form contents here
    <input type="submit" value="Whatever">
    </form>
    There are other problems with your code, you need to write the entire content to the pop up, not just the question, and you need to open the window as part of the action of the user. Something like so would be more direct (opening a window is even riskier than javascript in general - pop up blockers):

    Code:
    <form action="whatever" method="post" onsubmit="return confirm('Are you sure you want to delete these records?');">
    form contents here
    <input type="submit" value="Whatever">
    </form>
    Have the form itself carry out the delete action. But, as I say, bad idea - there will be no confirm for non-javascript users.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Strangeplant (10-03-2008)

  4. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Additionally, that popup window, since its opening is not triggered by any user-initiated event, will be blocked by most of today's browsers.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    I cannot get the pop-up to work in IE7, FF2 or FF3. So, I'm going to use the multiple page approach.....

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Strangeplant View Post
    I cannot get the pop-up to work in IE7, FF2 or FF3. So, I'm going to use the multiple page approach.....
    If you mean a pop up window, a pop up isn't a good idea, as it requires both javascript and pop ups enabled by the user - quite unreliable.

    However, as both Twey and I pointed out, to get a pop up window at all in modern browsers, the pop up window must be initiated by user action. Therefore it would have to be a direct part of the function that fires when the user clicks the button, not as you had it:

    Code:
        myWindow = window.open("", "responseWindow", 'toolbar, width=200, height=100')
    
        function confirmDelete() {
            myWindow.document.write("Click 'O.K'. to delete or 'Cancel' to resume")
            var answer = confirm("Are you sure you want to delete these records?");
            if(answer) {
                document.location = <? echo $_SERVER['PHP_SELF'];?>;
            }
            else {
                return false;
            }
        }
    outside of that function.

    Still, it is best that you do this all with PHP as I already pointed out. That means no confirm box or pop up window.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah! Sorry John, I didn't see that you'd already noted that.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •