Apr 20, 2008

PHP Form Validation

PHP Form Validation

In the PHP form creation page, the following line was used to start the form.

<form name="addLink" onSubmit="return checkForm(this);" action="addurl.php" method="POST">

This line invokes a JavaScript routine which is contained in the HTML header part of the page. If this routine returns with a successful status, the action item is called which means that "addurl.php" is called and data from the form is passed to it. Sessions are not required to pass this data to the addurl.php file and the names of values created on the form page will be available. These values include:

  • title
  • description
  • uname
  • email
  • stype
  • category

This JavaScript function will check to be sure there is a URL, title, link description, and category entered.

<SCRIPT LANGUAGE="JavaScript">
<!--
function checkForm(TheForm) {
  // url, title, description
  if (TheForm.url.value.length == 0) {
   TheForm.url.value = prompt("You forgot to enter the link URL for addition!");
   return false;
  }
  if (TheForm.title.value.length == 0) {
   TheForm.title.value = prompt("You forgot to enter the link title for addition!");
   return false;
  }
  if (TheForm.description.value.length == 0) {
   TheForm.description.value = prompt("You forgot to enter the link description for addition!");
   return false;
  }
  if (TheForm.category.value.length == 0) {
   TheForm.category.value = alert("You forgot to enter the category!");
   return false;
  }
  return true;
}// end function checkalForm
// -->
</SCRIPT>

No comments:

Post a Comment

Popular Posts