...

View Full Version : Javascript confirm



pita0017
07-11-2005, 10:28 PM
hi everyone, i have a php page that was built for me to post some things to a mysql database, i wanted to add a confirmation to it, so heres what i have...


<head>
<script language="JavaScript">
function confirmation() {
var answer = confirm("Are you sure you want to enter "+form1.Name.value+" as a new customer?")
if (answer){
SUBMIT FORM??
}
else{

}
}
</script>
</head>

that part is working, but no matter what i have done, the form always gets submitted, im starting to think that i cant do this in java....can someone give me a hand here.....thanks.

heres the php for the form:



<form action="<?php echo $editFormAction; ?>" method="POST" onSubmit="confirmation()"name="form1">


and the submit button:



<input name="submit" type="submit" id="submit" onclick=document.form1.action='<?php echo $editFormAction; ?>' value="Submit">


Again thanks to any who help me.

Burgerking63
07-11-2005, 10:40 PM
try
if (confirm("Are you sure you want to enter "+form1.Name.value+" as a new customer?")){form1.submit(); }; The first time you had it right except for if (answer). That does nothing, and so would submit everytime. If you had if(answer==true), then it would have worked. The method I provided just shortens process and elimanates the need to create a local variable.

pita0017
07-11-2005, 10:54 PM
ok made the modification and its still submitting the form....heres what i have now:



<script language="JavaScript">
function confirmation() {
if (confirm("Are you sure you want to enter "+form1.Name.value+" as a new customer?")){form1.submit(); };
}
</script>


thanks for your help.

Burgerking63
07-11-2005, 11:22 PM
try
if (confirm('Are you sure you want to enter '+form1.Name.value+' as a new customer?')){form1.submit(); }

pita0017
07-11-2005, 11:31 PM
still submits when i hit cancel or "x"...

Burgerking63
07-11-2005, 11:44 PM
Your forcing the form to submit twice by calling a PHP function to submit for the Submit button's onClick event, as well as a OnSubmit event handler for the form. Remove the Onclick event handler from the Submit button and let it do its job, the form's OnSubmit event handler will call the function before it submits. You just doubling the process, if this doesnt work, there is one more thing to try, but I am thinking it is your code that is flawed

pita0017
07-11-2005, 11:50 PM
ok, did that and same deal, this is quite frustrating, as im sure its becoming for you...

so now i have submit button:



<input name="submit" type="submit" id="submit" onclick="confirmation();" value="Submit">


and form:



<form action="<?php echo $editFormAction; ?>" method="POST" name="form1">


thanks for the help, its greatly appreciated.

Burgerking63
07-11-2005, 11:51 PM
I edited my post just as you replied, check my previous post

pita0017
07-11-2005, 11:55 PM
I edited my post just as you replied, check my previous post

me too..lol

Burgerking63
07-12-2005, 12:01 AM
You must have misread my edited post since you still have a "onclick=confirmation();" in your submit button code. This will not work as it will submit after you click because of the properties of the "submit" type of input. Re-read my post and make sure you follow the instructions then let me know if it works

pita0017
07-12-2005, 12:07 AM
sorry, i did misread it, im understading to leave the submit button alone, and to call the function from the forms OnSubmit event....so i did that and now have this, which still is not working, let me know, thanks.



<form action="<?php echo $editFormAction; ?>" method="POST" onSubmit="confirmation();" name="form1">



<input name="submit" type="submit" id="submit" value="Submit">

Burgerking63
07-12-2005, 12:21 AM
alright, it looks like your going to have to use a return method that is dependant on a set of parameters. Since I'm assuming your not doing any form validation at this point, I'm just posting a confirmation to submit or cancel when the users clicks the submit button. Here is your script code:
<SCRIPT LANGUAGE="JavaScript">
function confirmation(f){
if (confirm('Are you sure you want to enter '+form1.Name.value+' as a new customer? Click "OK" to submit change or "CANCEL" to reject the change')) {
f.submit();}
else {
return false }
}
</script> Here is your form code:
<form action="<?php echo $editFormAction; ?>" method="POST" onSubmit="return confirmation(this)" name="form1">

BTW, your submit button code is now fine, as long as it does not contain any event handlers.

pita0017
07-12-2005, 12:34 AM
wow, thats a beautiful thing...lol....thank you.

now if i did want to add form validation, im assuming i cant have 2 onSubmit events? or could i add it to the submit button's onClick event?

Burgerking63
07-12-2005, 12:50 AM
you can have as many functions called during"onsubmit" as you want. You would just seperate each time you call a function with a ";", for example:
onSubmit="function1(); function2()" As long as you call the function that would submit the form last, the others will run first. If you choose to go this route, my suggestion would be to have each function sequentially call the next eliminating the need for multiple properties in your OnSubmit event handler. For example:
<script>
function1(){ alert('dododo'); function2(); }
</script>
<script>
function2(){
do this;
then that;
submitForm();}
</script>
<script>
submitForm(){
form1.submit(); }
</script> Then your onSubmit would be this:
onSubmit="function1();"

pita0017
07-12-2005, 12:54 AM
excellent, when i get the form validation code written up and tested i will then integrate it with the confirmation code and test, if i have any problems i will let you know....thank you once again, you are very knowledgeable and helpful.

glenngv
07-12-2005, 07:12 AM
<SCRIPT LANGUAGE="JavaScript">
function confirmation(f){
if (confirm('Are you sure you want to enter '+form1.Name.value+' as a new customer? Click "OK" to submit change or "CANCEL" to reject the change')) {
f.submit();}
else {
return false }
}
</script>
You don't have to submit the form as it is being submitted already. You just have to return true.

function confirmation(f){
return confirm('Are you sure you want to enter '+f.Name.value+' as a new customer? \nClick "OK" to submit change or "CANCEL" to reject the change')
}
...
<form onSubmit="return confirmation(this)">

If you are going to have validation in the future, you just need that validation to return true (valid) and false (invalid) and call that function first before the confirmation() function.

<form onSubmit="return validate(this) && confirmation(this)">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum