Forum Moderators: coopster

Message Too Old, No Replies

Using php and javascript to display warnings based on php variables

         

scaleypate

5:14 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



I am trying to create a javascript confirmation box that will display various warning that let users know which fields that have left blank and allows them to decide if they want to continue and submit the form or go back and fill in the blank fields. I used php variables for the warnings. For example, if the zip field is blank, a php variable $zipwarn is given a value. If the zip field has a value, $zipwarn is null. The same goes for all of the other fields that are not mandatory. I only want the javascript to display variables that have value.

If there is a better way than javascript confirm box to do this, I am open to suggestions.

Thanks in advance!

mattx17

5:42 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



One way you could do it a bit differently could be to have a universal $FormError var, and assign errors to it. Then have your javascript check to see if that var is filled in, and display it with a dialog box if it is. For example:

$FormError = "Invalid E-mail address";

The javascript:

function checkForError() {
var error = '<?php echo $FormError;?>';
if (error > '') {
alert(error);
return false;
}
}

Then the form:

<form action="script.php" method="post" onsubmit="return checkForError();">

Just an example off the top of my head, it may need some tweaking to make sure it works like you want it to. Hope this helps!