Member Avatar for starsinthesky

i really dunno what i miss or what's wrong with my codes.
it would only work when you hit the "Post" button on the second try.
i think it has something to do with my GUI, coz if i would just test the form named "fields" the javascript would work. but if i would run it with the GUI, it would no longer work.

here's my whole PHP file.
..sorry if its long.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>FAQ manager</title>
<link rel="stylesheet" type="text/css" href="design.css" />
<script type="text/javascript" src="checkFields.js"></script>
</head>

<?php
	include("connector.php"); 
	$query=mysql_query("select * from qa");
	$row = mysql_fetch_assoc($query);
	$query1=mysql_query("select * from categories");
	
?>
<body background="1.gif">
<div id="container">
    <div id="header"></div>
    <div id="heading"><font color="#FFFFCC" face="Copperplate Gothic Bold" size="6">FAQ</font><font color="#FFFFCC" size="7" face="Blackadder ITC"> &nbsp;&nbsp;manager</font></div>
	<div id="tabs">
    <div align="left"><span class="style8"><a class="for_tabs" href="FAQ.php">Home</a> | <a class="for_tabs" href="inbox.php">Inbox</a> </span></div>
  </div>
  <div id="search">
  <table width="244">
  	<tr> 
    	<td>
			<form action="search.php" method="post"><font color="#FFFFCC" face="Andalus" size="3">Search:</font> 
            <input type="text" name="search"> 
            <input type="image" src="go.png" alt="Submit" /></form>
		</td>
    </tr>
  </table>
  </div>
  <div id="faqs_label"></div>
  <div id="faq_text"><font color="#336600" size="4">Add FAQ</font></div>
  <div id="faqs">
  <center>
  <br>
<table border="1">
	<form method="post" action="adding_FAQ_check.php" name="fields" onSubmit="return checkFields(this)">
	<tr>
		<td>Question:</td>
		<td><textarea name="question" rows="1" cols="50"></textarea></td>
	</tr>
	<tr>
		<td>Answer:</td>
		<td><textarea name="answer" rows="7" cols="50"></textarea></td>
	</tr>
	<tr>
		<td>Category:</td>
		<td><select name="category">
				<?php while($row1 = mysql_fetch_assoc($query1)) { ?>
				<option value=<?php echo $row1['cat_num'];?>><?php echo $row1['category']; ?></option>
				<?php } ?>		
			</select>
		</td>
	</tr>
	<tr>
		<td><input type="submit" value="Post"></td>
	</form>
	</tr>
</table>
  </center>
  </div>
  <div id="category_label"></div>
  <div id="categories">
  	<table width="270">
    <?php while($row1 = mysql_fetch_assoc($query1)){?>
    	<tr> 
        	<td width="180">&nbsp;&nbsp;<a class="hype" href="categories.php?num=<?php echo $row1["cat_num"]; ?>#<?php echo $row1["cat_num"]; ?>"><font color="#9B004E"><?php echo $row1['category']; ?></fontO></a>
			</td>
        </tr>
        <?php } ?>
    </table>
  </div>
  <div id="category_text"><font color="#336600" size="4">categories</font></div>
  <div id="cat_button">
  	<table>
    	<tr> 
        	<td><form action="new_category.php" method="post">
                <input name="submit" type="submit" value="Add Category" />
                </form>
			</td>
            <td><form action="view_categories.php" method="post">
                <input name="submit" type="submit" value="View Categories" />
                </form>
			</td>
        </tr>
    </table>
  </div>
</div>
 
</body>
</html>

and my javascript

function checkFields()
{	
	if(fields.question.value == "")
	{
		alert('Fill up Question field.')
		fields.question.focus()
		return false
	}
	if(fields.answer.value == "")
	{
		alert('Fill up Answer field.')
		fields.answer.focus()
		return false
	}
}

thanks for your time. :)

Recommended Answers

All 4 Replies

On your submit event simply apply this code:

<form method="post" action="adding_FAQ_check.php" name="fields" onsubmit="return checkFields(); /* without the [b]this[/b] global. */ ">
Member Avatar for starsinthesky

i've already tried it before. but still it wont work.
but thanks anyway, im going to try figuring it out at home. its almost 10pm and ill be off from office (OJT ^^).

thanks essential.

ps. hope i could be as good as you guys! :P

Don't just quit, yet! Am sure even w/o my help you can settle this thing. If it still ain't working, you can try this code:

function checkFields( form ) {
   try {
      if ( form.question.value.length < 3 || form.question.value === "" ) {
      alert("Fill up Question field!");
      form.question.focus();
      return false;
      }
      if ( form.answer.value.length < 3 || form.answer.value === "" ) {
      alert("Fill up Answer field!");
      form.answer.focus();
      return false;
      }
   } catch( e ) {
      (( e.description ) ? alert( e.description ) : alert( e.message )); // Trap those error's in the form.
   }
}

Now attach this function to your form with onsubmit handler, onsubmit="return checkFields( this );" .
And put the whole code inside the header section of your document, so that your form can access it, without hustle.

Member Avatar for starsinthesky

essential,

thanks a lot. i haven't visited daniweb since i got out of the office that night. we had a holiday and day-offs. :)

BTW, i've finished my task. ;)

ps. i didn't quit. never will.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.