PDA

View Full Version : Javascript method confirm() problem



Strangeplant
10-02-2008, 03:19 PM
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:
echo('<div class="managedel"><input type="Submit" name="file4" value="Delete" onClick="return confirmDelete();"></div>');


And in my javascript section I have this:
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.....

jscheuer1
10-02-2008, 03:50 PM
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:


<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):


<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.

Twey
10-02-2008, 05:15 PM
Additionally, that popup window, since its opening is not triggered by any user-initiated event, will be blocked by most of today's browsers.

Strangeplant
10-03-2008, 08:32 PM
I cannot get the pop-up to work in IE7, FF2 or FF3. So, I'm going to use the multiple page approach.....

jscheuer1
10-04-2008, 08:07 AM
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:



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.

Twey
10-04-2008, 08:42 AM
Ah! Sorry John, I didn't see that you'd already noted that.