...

View Full Version : Prevent page from refreshing when user clicks OK in alert box



shirleymck
05-11-2007, 05:13 AM
The following Javascript checks to see that all form fields have been filled in and causes an alert to pop up if they have not. This works correctly. However, after the user clicks OK, the page blinks and appears to refresh, and all the previously entered data is lost so that the user has to re-enter everything again. Can this be prevented?

<script language="javascript">
var yn;
var ye;
var fn;
var fe;
var ms;

function editFields() {
document.invite_form.formGoodOrNot.value = "Y";

yn = document.invite_form.yourname.value;
ye = document.invite_form.youremail.value;
fn = document.invite_form.friendname.value;
fe = document.invite_form.friendemail.value;
ms = document.invite_form.message.value;
if (yn == "" || yn == " " || ye == "" || ye == " "
|| fn == "" || fn == " " || fe == "" || fe == " "
|| ms == "" || ms == " ")
{
alert("You must enter all information");
document.invite_form.formGoodOrNot.value = "N";
}

}
</script>

Here is the link to the actual page:

www.westernoaks.org/invitations/invitation.asp

smalldog
05-11-2007, 10:00 AM
Yeah man.. that reason is pretty simple.. try this code instead and let me know:



<script language="javascript">
var yn;
var ye;
var fn;
var fe;
var ms;

function editFields() {
document.invite_form.formGoodOrNot.value = "Y";

yn = document.invite_form.yourname.value;
ye = document.invite_form.youremail.value;
fn = document.invite_form.friendname.value;
fe = document.invite_form.friendemail.value;
ms = document.invite_form.message.value;
if (yn == "" || yn == " " || ye == "" || ye == " "
|| fn == "" || fn == " " || fe == "" || fe == " " || ms == "" || ms == " ")
{
alert("You must enter all information");
document.invite_form.formGoodOrNot.value = "N";
return;
}
document.invite_form.submit();
}
</script>
<form action="invitation.asp" method="post" name="invite_form">

<input type="button" value="Preview Your Invitation" name="Button" onclick="editFields();">

Philip M
05-11-2007, 11:28 AM
if (yn == "" || yn == " " || ye == "" || ye == " "
|| fn == "" || fn == " " || fe == "" || fe == " " || ms == "" || ms == " ")

This simply checks that at least one character (except a single space but not two or more spaces) - even a ?, and/or however inappropriate - has been entered. It is doubtful if such a simple check is worthwhile. At a minimum check that each field has a length of at least 4 and the email fields have a @ and a . That is still a long way off a robust validation.

shirleymck
05-11-2007, 04:14 PM
Thank you to both of you for your comments. Here are my responses:

smalldog,

Clicking the "Preview Your Invitation" button already leads to running editFields() because the function is called right after that. The problem is what happens when the following line is executed and the user clicks OK.

alert("You must enter all information");

Is there something I can add to this statement to make things behave differently right after that?

Philip,

I actually was aware that this code doesn't check for much. Where can I find a good example of how to create a more robust check? I would like to see how to do this.

Shirley

smalldog
05-11-2007, 04:50 PM
Shirleymck, I understand what is the problem :) problem is, your form is posted, and data lost then :D... So, I suggest you how to modify your script. If you will do it, problem should be solved.

shirleymck
05-11-2007, 07:06 PM
I am doing some more testing on this page, so please disregard this question for now. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum