FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


JavaScript confirm() problem





ammonkc
I'm having some problems with a javascript confirm() box. I have a onClick on a form submit button. it pops up and displays the data fine. but when the user clicks on the 'cancel' button it still submits the form and gos to the next page. the only thing that i can think of is that somehow its returning true for both the 'ok' and 'cancel' buttons. does anyone know what I'm doing wrong here? this is the exact code below.


Code:
<script language="JavaScript" type="text/javascript">
<!--
//this script displays a summary of all the form data in a confirm box
function summary(){
confirm(
"Are you sure you want to submit this?\n\n email: "+document.email.email.value+
"\nAgency name: "+document.email.agency_name.value+"\nYour name: "+document.email.name.value+
"\nCategory: "+document.email.category.value+"\nRecord locator: "+document.email.record_locator.value+
"\nTicket Quote 2 name: "+document.email.quote.value+"\nTicket Issuance: "+document.email.issuance.value+
"\nAmadeus: "+document.email.amadeus.value+"\nAdditional: "+document.email.additional.value
);

}
// -->
</script>

<input name="submit" type="submit" value="Submit" onclick="summary();" />
 


Thanks in advance
SystemWisdom
Try this sample test page I whipped up from your posted code:

Code:

<html>
<head>

<script language="JavaScript" type="text/javascript">
<!--
//this script displays a summary of all the form data in a confirm box
function summary(){
    var szQuestion = "Are you sure you want to submit this?\n\n email: "+document.frmMessage.email.value+
                     "\nAgency name: "+document.frmMessage.agency_name.value+"\nYour name: "+document.frmMessage.name.value+
                     "\nCategory: "+document.frmMessage.category.value+"\nRecord locator: "+document.frmMessage.record_locator.value+
                     "\nTicket Quote 2 name: "+document.frmMessage.quote.value+"\nTicket Issuance: "+document.frmMessage.issuance.value+
                     "\nAmadeus: "+document.frmMessage.amadeus.value+"\nAdditional: "+document.frmMessage.additional.value;
    return confirm(szQuestion);
}
// -->
</script>

</head>

<body>

<form name="frmMessage" onsubmit="return summary();">
<input type="text" name="email" value="" /><br />
<input type="text" name="agency_name" value="" /><br />
<input type="text" name="name" value="" /><br />
<input type="text" name="category" value="" /><br />
<input type="text" name="record_locator" value="" /><br />
<input type="text" name="quote" value="" /><br />
<input type="text" name="issuance" value="" /><br />
<input type="text" name="amadeus" value="" /><br />
<input type="text" name="additional" value="" /><br />

<input name="submit" type="submit" value="Submit" />
</form>

</body>
</html>


I hope that helps!
ammonkc
Thanks. That worked for me. What exactly was the problem with my old code? was it that I didn't return the confirm() value, or was it because I was using onClick() in the submit tag instead on onSubmit() in the form tag?

Either way the code that your posted works. Thanks
SystemWisdom
Your code wasn't doing anything at all with the return value from the confirm function. You could have simply modified your script to use an IF statement:

Code:

if( confrim( "yadda yadda yadda" ) )
   // Submit form
else
   // dont submit form


Something like that...
ammonkc
cool, thanks. well it works now. I don't do a lot of javascript. for some reason I thought that all of that logic was build in to javascript and the browser would interpret 'cancel' to mean stop processing automatically. good to know.
Thanks
kk_loke86
Thanks a lot for this thread...It is very helpful.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.