SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A VERY Strange Problem

    Hello all.

    I'm just learning JS (Sitepoint Anthology), and I have encountered a bizarre problem.

    I have a form with the following line in the form declaration:
    Code:
    <form name="myForm" onsubmit="return validateForm(this)" action="" method="post">
    Here is my validateForm(myForm) function (dates and cluster are required):
    Code:
    function validateForm(myForm) {
    
        var dates;
        var cluster;
    
        dates = YAHOO.example.calendar.cal1.getSelectedDates();
        document.getElementById("query_dates").value = dates;
        document.getElementById("cluster").value;
        cluster = document.getElementById("cluster").value;
        document.getElementById("site").value;
        document.getElementById("email").value;
        document.getElementByID("by_date").value;
    
        if (dates == "") {
    
            alert("You must select at least one date");
            return false;
    
        } else {
    
            if (cluster == "") {
    
                alert("You must select a cluster");
                return false;
    
            } else {
    
                return true;
            }
        }
    }
    The bizarre part is that no matter what I try, flow bypasses the if() construct and true is returned in ALL cases. It will not evaluate the conditional statement(s).

    I put an alert() at the very end of the validation function - and it doesn't execute. It's as though flow just dies and passes back a 'magic' true.

    I have used alert() to display the values when I leave dates and cluster empty, and they appear to be just that. If the form elements are populated, it passes the values correctly.

    I tried putting the code in the page. No go.
    I tried testing against NULL. No go.
    I tried testing against false. No go.
    I tried testing against zero. No go.


    Can someone tell me why the if() construct is not being entered and evaluated?

    This is driving me bonkers.
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    It seems correct to me.

    Is your HTML right?? Are you sure you gave the objects ID's and not just Names??

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try alerting cluster and and dates after they are defined to see if they are not null. And as Shaun says, it would help if you provided the HTML (at least the form).

  4. #4
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try alerting cluster and and dates after they are defined to see if they are not null. And as Shaun says, it would help if you provided the HTML (at least the form).
    If I enter values the validateForm() function correctly populates the $_POST array. I even tried setting dates and cluster to "invalid" by default in the validateForm() function - to force a false on invalid input - even that won't work because the if() construct is not evaluated at all. In validateForm, the statements down to the if() are executed, then I get the 'magic' true no matter the form element values. I have placed alert()'s in the if() construct and they are not executed.

    Here is the HTML. Note the php - I use a js calendar widget to get dates, then pass all of the form values to php via $_POST if the form input validates. I have successfully used this before - and if I enter values for the form elements the validateForm function correctly populates $_POST (it just won't evaluate the if() conditional as I mentioned earlier.

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <?php
        header ("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header ("Cache-Control: no-cache, must-revalidate");
        header ("Pragma: no-cache");
    ?>
    <title>My Page</title>
        <script type="text/javascript" src="calendar/yahoo.js"></script>
        <script type="text/javascript" src="calendar/event.js"></script>
        <script type="text/javascript" src="calendar/dom.js"></script>
        <script type="text/javascript" src="calendar/calendar.js"></script>
        <script type="text/javascript" src="cluster_site.js"></script>
        <script type="text/javascript" src="dps_metrics_init.js"></script>
        <link type="text/css" rel="stylesheet" href="styles/interface.css">
        <link type="text/css" rel="stylesheet" href="styles/calendar.css">
    </head>
    <body onload="init()">
    	<div id="page_title">
    		<p class="title">My Page</p>
              <ul>
                  <lh><b>Instructions/Help</b></lh>
                  <li>You MUST select at least a single date.</li>
                  <li>You MUST select a cluster.</li>
                  <li>Select the begin and end dates of your desired date range.</li>
                  <li>Clicking a single date will query a single day.</li>
                  <li>If you select the wrong date(s), clicking the 'Reset Calendar' link will reset the date(s).</li>
                  <li>Click the 'Run Report' button to run the program.</li>
              </ul>
    	</div> <!-- end page_title div -->
    	<div id="calendar">
    	   <p class="text_input">
    		      <a href="javascript:YAHOO.example.calendar.cal1.reset()" class="nav_link" >Reset Calendar</a>
           </p>
    	   <div id="cal1Container">
    	   </div>
    	</div>
    	<div id="form">
    		<form name="myForm" onsubmit="return validateForm(this)" action="" method="post">
                <input type="hidden" id="query_dates" name="query_dates" />
                <br><br>
                Cluster: <select onchange="setSite(this, site)" id="cluster" size="1" name="cluster">
                    <option value="" selected="selected">Select Cluster</option>
                    <script type="text/javascript">
                        setClusters(this);
                    </script>
                </select>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Facility: <select name="site" id="site" size="1" disabled="disabled"></select>
                <br><br>
                Email: <input type="text" id="email" name="email" />@mysite.com&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Break Out by MOD Date: <input type="checkbox" name="by_date" id="by_date" />
                <br><br>
                <div id="submit">
                    <input type="submit" id="submit" value="Run Report" />
                </div> <!-- end submit div -->
            </form>
        </div> <!-- end form div -->
    </body>
    </html>
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  5. #5
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Are you getting any javaScript error messages popping up when you test this??

    Perhaps there's something else undefined and it's causing the browser to skip over those lines of code or drop some declarations.

  6. #6
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shaun,

    I had to hit the 'stop' button quickly (because it disappears on a redirect),
    but I did manage to capture the following error message (I know that IE does not accurately report line numbers):

    Line: 22
    Char: 5
    Error: Object doesn't support this property or method
    Code: 0
    URL: the page URL
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Re-check with Firefox, it will tell give you (in almost all cases) a very good error message (including the correct line number).

  8. #8
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try that when I get home (we are only allowed to use MSIE at my work....).

    Thank you.
    EDIT:

    Danke for the great FF tip. I found someone who managed to install FF here, and found the error:

    In validateForm(myForm), the line:

    Code:
    document.getElementByID("by_date").value;
    should have been:
    Code:
    document.getElementById("by_date").value;
    I had a capital D in 'Id' which caused the function to 'choke'. It is once again working. Thank you for the assistance.
    Last edited by GWild; Jan 3, 2007 at 12:16. Reason: Found the problem...
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  9. #9
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd bet that the code is executing before a value is returned to be assigned to dates.

  10. #10
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GWild
    I had a capital D in 'Id' which caused the function to 'choke'. It is once again working. Thank you for the assistance.
    You're welcome, dude.

    I know how maddening these things can be.

    Best of luck with the site.


Bookmarks

Posting Permissions

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