...

View Full Version : Return false is not stopping form submission



MsCongeniality
09-27-2012, 04:32 PM
This is the javascript


<script type="text/javascript">
function validate()
{
if(document.gapcalc.rev.value == "<?php echo $rev;?>" ||
document.gapcalc.rev2.value == "<?php echo $rev2;?>" ||
document.gapcalc.gm.value == "<?php echo $gm;?>" ||
document.gapcalc.gm2.value == "<?php echo $gm2;?>" ||
document.gapcalc.eps.value == "<?php echo $eps;?>" ||
document.gapcalc.eps2.value == "<?php echo $eps2;?>" ||
document.gapcalc.sss.value == "<?php echo $sss;?>" ||
document.gapcalc.sss2.value == "<?php echo $sss2;?>")
{
document.getElementById('req').innerHTML = '<span style=\"color:red;\">These fields are required</span>';
document.getElementById('asterisk').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk1').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk2').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk0').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk3').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk4').innerHTML = '<span style=\"color:red;\">*</span>';
document.getElementById('asterisk5').innerHTML = '<span style=\"color:red;\">*</span>';
alert("oeu");
return false;
}
}
</script>



This is in the form


<form action="i3.php" method="post" onsubmit="return validate();" name="gapcalc">
<table class="myTable">
<tr>
<th width="170"></th>
<th>Q3</th>
<th>Q4</th>
</tr>
<tr>
<td>Revenue ($mm)</td>
<td><input type="text" name="rev" size="2" value="<?php echo $rev;?>" onfocus="remQuery(this,'<?php echo $rev;?>');" onblur="chkQuery(this,'<?php echo $rev;?>');"></td>
<!-- <td>9.81</td> -->
<td><input type="text" name="rev2" size="2" value="<?php echo $rev2;?>" onfocus="remQuery(this,'<?php echo $rev2;?>');" onblur="chkQuery(this,'<?php echo $rev2;?>');"><span id="asterisk0"></span></td>
</tr>
<tr>
<td>Gross Margin (GM-%)</td>
<td><input type="text" name="gm" size="2" value="<?php echo $gm;?>" onfocus="remQuery(this,'<?php echo $gm;?>');" onblur="chkQuery(this,'<?php echo $gm;?>');"></td>
<!-- <td>1.81</td> -->
<td><input type="text" name="gm2" size="2" value="<?php echo $gm2;?>" onfocus="remQuery(this,'<?php echo $gm2;?>');" onblur="chkQuery(this,'<?php echo $gm2;?>');"><span id="asterisk1"></span></td>
</tr>
<tr>
<td>Earnings/Share (non GAAP)</td>
<td><input type="text" name="eps" size="2" value="<?php echo $eps;?>" onfocus="remQuery(this,'<?php echo $eps;?>');" onblur="chkQuery(this,'<?php echo $eps;?>');"></td>

<td><input type="text" name="eps2" size="2" value="<?php echo $eps2;?>" onfocus="remQuery(this,'<?php echo $eps2;?>');" onblur="chkQuery(this,'<?php echo $eps2;?>');"><span id="asterisk2"></span></td>
</tr>

</table>






The JavaScript is supposed to check if the form has been filled out. I use php to dynamically check if any of the numbers have been changed (because there are numbers prefilled).

The form fires no matter what, I cannot seem to get it functioning. I've done some research and google explains that javascript will just stop compiling if it sees any error (thus causing form to fire). But I cannot see any error in my JS, can anyone help?

WolfShade
09-27-2012, 04:50 PM
There is nothing in the error console or FireBug?

MsCongeniality
09-27-2012, 04:59 PM
Ahhhhhh....


document.getElementById("asterisk") is null
validate()i3.php (line 239)
onsubmit()onsubmit (line 2)
event = submit
[Break On This Error]

...ument.getElementById('asterisk').innerHTML = '<span style=\"color:red;\">*</span...


I didn't realize this...

the javascript was using "getElementById" on IDs that didn't exist. (asterisk, asterisk4 and asterisk5)

I didn't realize that this would cause JS to bomb. I figured it was the equivalent of setting a variable and not using it in PHP. My mistake.

Could someone explain to me why this is the case?

WolfShade
09-27-2012, 07:18 PM
In JavaScript, if anything doesn't exist but is being referenced, JS throws an error. Unlike PHP, where if a variable doesn't exist it's the equivalent of being null.

If you tried to reference an object or element that didn't exist, JS aborts and sends a message to the error console. It cannot skip past it. It's very touchy, that way. :/

Old Pedant
09-27-2012, 08:23 PM
Not really. The same thing would happen in PHP.

Just doing this does *NOT* cause an error when there is no such ID as "asterisk".


document.getElementById('asterisk');

The problem comes when you then try to do


document.getElementById('asterisk').innerHTML = "...anything..";

You are then trying to do


null.innerHTML = "...anything...";

And of course the null object doesn't *HAVE* any properties. So you can't use the innerHTML of a null (non-existent) object.

In PHP you would have the same problem if you were indeed using objects.

You would then get the PHP error message
Trying to get property of non-object

SAME THING!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum