Jump to content

Form Validation. PHP and JS.


smithmr8

Recommended Posts

Hi

Having alot of trouble from a seemingly fine piece of JS, which is supposed to validate selected fields on a form, and display a message (contained within a div tag). I can get it to work using only Subject within the JS, but as soon as I try to get it to work with more than one of them it just seems to bypass the JS or not validate properly and submit the form. Even when every field is blank.

 

Javascript

<script>
  function checkForm() {
subject = document.getElementById("subject").value;
category = document.getElementById("category").value;
location = document.getElementById("location").value;

  
  if (subject == "") {
  hideAllErrors();
document.getElementById("subjectError").style.display = "inline";
document.getElementById("subject").select();
document.getElementById("subject").focus();
  return false;
  } else if (category == "") {
  hideAllErrors();
  document.getElementById("categoryError").style.display = "inline";
  document.getElementById("category").select();
document.getElementById("category").focus();
return false;
  } else if (location == "") {
  hideAllErrors();
   document.getElementById("locationError").style.display = "inline";
  document.getElementById("location").select();
document.getElementById("location").focus(); 
return false;
  }
   return true;
  }

  function hideAllErrors() {
document.getElementById("subjectError").style.display = "none"
document.getElementById("locationError").style.display = "none"
document.getElementById("categoryError").style.display = "none"


  }
</script>

 

Form

<form onSubmit="return checkForm();" method="POST" action="ticket.php?step=created" enctype="multipart/form-data" name="form1" id="form1">
<table>
<tr> 
<td width="150"><b>Subject</b></td>
<td width="200"><input method="text" type="text" size="20" name="subject" id="subject" value=""></td>
<td width="200"><div class="error" id="subjectError">Subject Required!</div></td>
</tr>
<tr> 
<td width="150"><b>Category</b></td>
<td width="200">
<select name="category" id="category">
<option SELECTED value="">Select a Category    </option>
<?
$cat = mysql_query("SELECT * FROM `category`");
while($ca = mysql_fetch_array($cat)){
echo "<option value=".$ca['ID'].">".$ca['name']."</option>";
}
?>
</select>
</td>
<td width="200"><div class="error" id="categoryError">Category Required!</div></td>
</tr>
<tr> 
<td width="150"><b>Computer / Location</b></td>
<td width="200"><input method="text" type="text" size="20" name="location" value="" id="location"></td>
<td width="200"><div class="error" id="locationError">Location Required!</div></td>
</tr>
<tr>
<td width="150"><b>Attachment</b></td>
<td colspan="2"><input name="ufile" type="file" id="ufile" size="40"/></td>
</tr>
<tr> 
<td width="150"><b>Message</b></td>
<td width="200"></td>
<td width="200">Validation For Message</td>
</tr>
<tr>
<td colspan=3><textarea cols=60 rows=5 name="message" id="message" value="" type="text"></textarea></td>
</tr>
<tr>
<td width="150"><input type="submit" value="Submit Ticket"></td>

</tr>

</table>
</form>

 

Would greatly appreciate some help with this problem.

 

Regards,

Luke

Link to comment
https://forums.phpfreaks.com/topic/147719-form-validation-php-and-js/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.