+ Reply to Thread
Results 1 to 4 of 4

Thread: Javascript : Small Script...Big Headache

  1. #1
    PharaohInc's Avatar
    PharaohInc is offline x10Hosting Member PharaohInc is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    In Da 242/876/301/305
    Posts
    8

    Question Javascript : Small Script...Big Headache

    Ok right, so I basically have a comment box and want users to post comments on certain pages. I just want a javascript function to alert the user if the comment field is empty. I have looked through the net, but for some reason I can't seem to get this simple code to work. I have tried getElementByID, have also called the forms explicitly but yet no luck. Can someone please point out what I'm doing wrong.

    this is the sample html

    Code:
    <html> 
    		<head>
    			<script type="text/Javascript" language ="Javascript" src="comments.js"></script>
                <title> About Us </title>
    
    		</head>
    <body>
    		<fieldset>
    			<legend> Post Comment </legend>
    			<form name="comments" action="../dbconnect/post_comments.php" onSubmit="return    
    			checkFields()" method="post">
                <input type="text" name="comment" size="100" class="txt"/>
                <input type="hidden" name="source" value="aboutus" />
                <input type="hidden" name="url" value="AboutUs.php" />
                <input type="submit" class="btn2" value="Post" />
                </form>
                </fieldset>
    </body>
    And this is the simple javascript file

    Code:
    function checkFields()
    {
        missinginfo="";
        var form = document.forms[0].elements[0];
        
        if (form.value == "")
        {
            missinginfo += "\n - Comment";
        }
        
        msg = "The following fields must be filled in\n";
        if(missinginfo !="")
        {
            missinginfo = msg+ missinginfo + "\n";
            alert(missinginfo);
            return false;
        }
        
        else return true;
    }
    Any help whatsoever will be greatly appreciated.
    Show me a man that has never made mistakes and I will show you a man that has never tried.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript : Small Script...Big Headache

    Does your actual page have a line break in your inline submit handler? If so, checkSubmit will never be called, because the newline character is a statement separator; the return and checkSubmit() are separate statements. Other than that, I don't know what to tell you, since you didn't describe exactly what behavior you expect and what you get, including any error messages.

    Note that you can access forms and inputs by name:
    Code:
    var comment = document.forms.comments.elements.comment;
    Last edited by misson; 03-30-2010 at 07:18 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    essellar's Avatar
    essellar is online now Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,152

    Re: Javascript : Small Script...Big Headache

    First things first -- are you even sure that your script is loading? You are using a relative path that tells the browser to request the script from the current directory, but your form's action attribute indicates that you are at least one directory down in the hierarchy. Make sure the script is in the same directory or (preferably) move it to a scripts directory and use server-relative addressing (src="/scripts/validate_comment.js" or something similar). To just plain see if the script works, put it inline on the page for testing first.

  4. #4
    PharaohInc's Avatar
    PharaohInc is offline x10Hosting Member PharaohInc is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    In Da 242/876/301/305
    Posts
    8

    Re: Javascript : Small Script...Big Headache

    Thanks for the replies, but I got it to work. For one like an idiot the function that was created didn't really cancel the submission of the file. I got help from another website and it seems to have worked. Here is the edited and working code just in case anybody has a problem like me.

    The edited JavaScript

    Code:
    function checkFields()
    {
        missinginfo="";
        var form = document.getElementById('comment');
        
        if (form.value == "")
        {
            missinginfo += "\n - Comment";
        }
        
        msg = "The following fields must be filled in\n";
        
        if(missinginfo !="")
        {
            missinginfo = msg + missinginfo + "\n";
            alert(missinginfo);
        }
        
        else
        {
        document.getElementByID('commentform').submit()
            }
        
    }
    And here is the edited HTML

    Code:
    <html> 
    		<head>
    			<script type="text/Javascript" language ="Javascript" src="comments.js"></script>
                <title> About Us </title>
    
    		</head>
    <body>
    		<fieldset>
    			<legend> Post Comment </legend>
    			<form id="commentform" action="../dbconnect/post_comments.php" method="post">
                <input type="text" id="comment" size="100" class="txt"/>
                <input type="hidden" name="source" value="aboutus" />
                <input type="hidden" name="url" value="http://shiva/~KhalinA09/GroupAlpha/Project4/about_us/AboutUs.php" />
                <input type="button" class="btn2" value="Post" onclick="checkFields()" />
                </form>
                </fieldset>
    </body>
    Thanks once again for all the replies, really appreciate the input.
    Show me a man that has never made mistakes and I will show you a man that has never tried.

+ Reply to Thread

Similar Threads

  1. Finding a Javascript News Bar script
    By rufussweetwater in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 06-22-2009, 12:19 PM
  2. Password changing headache
    By rodney in forum Free Hosting
    Replies: 3
    Last Post: 02-05-2009, 09:51 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. Cpanel Headache
    By shdw.puppet in forum Free Hosting
    Replies: 2
    Last Post: 01-09-2008, 04:11 PM
  5. You small help
    By 3aKaT in forum Off Topic
    Replies: 4
    Last Post: 01-11-2007, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers