Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Return false is not stopping form submission

    This is the javascript
    Code:
    <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
    Code:
    <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?

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    There is nothing in the error console or FireBug?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    MsCongeniality (09-27-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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. :/
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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".
    Code:
    document.getElementById('asterisk');
    The problem comes when you then try to do
    Code:
    document.getElementById('asterisk').innerHTML = "...anything..";
    You are then trying to do
    Code:
    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!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •