Javascript : Small Script...Big Headache

PharaohInc

New Member
Messages
8
Reaction score
0
Points
0
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.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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.
 

PharaohInc

New Member
Messages
8
Reaction score
0
Points
0
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.
 
Top