Solved: PHP Form Validation

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

doomlord289

Thread Starter
Joined
Nov 1, 2005
Messages
192
Is there any way to get the length of a form field in PHP before storing it to a text file or a database?
I just started using MySQL databases and when someone enters a long comment, I want a JavaScript alert popup to tell them to shorten their answer so they don't get a MySQL error.
Also, a counter of characters would be nice to put under the text box to tell users how many characters they have left.

Current Form:
HTML:
<form action="processcontact.php" method="POST" name="Contact">
  <div align="center">
  <table width="800" border="0">
    <tr>
      <td width="317"><div align="right">Name:</div></td>
      <td width="473"><input name="Name" type="text" id="Name" size="40"></td>
    </tr>
    <tr>
      <td width="317"><div align="right">E-mail:</div></td>
      <td width="473"><input name="E-mail" type="text" id="E-mail" size="40"></td>
    </tr>
    <tr>
      <td width="317"><div align="right">Program:</div></td>
      <td width="473"><select name="Program" id="Program">
        <option value="Formula Suite (TI-89)" selected>Formula Suite (TI-89)</option>
        <option value="Formula Suite (Windows)">Formula Suite (Windows)</option>
        <option value="Chemistry Helper">Chemistry Helper</option>
        <option value="Algebra II/Trig. Helper">Algebra II/Trig. Helper</option>
        <option value="Site">Site</option>
      </select></td>
    </tr>
    <tr>
      <td width="317"><div align="right">Topic:</div></td>
      <td width="473"><select name="Topic" id="Topic">
        <option value="Bug" selected>Bug</option>
        <option value="New Program">New Program</option>
        <option value="New Formula">New Formula</option>
        <option value="Suggestion">Suggestion</option>
        <option value="Comment">Comment</option>
        <option value="Add/Remove Link">Add/Remove Link</option>
        <option value="Other">Other</option>
      </select></td>
    </tr>
    <tr>
      <td width="317"><div align="right">Question:</div></td>
      <td width="473"><textarea name="Question" cols="40" id="Question"></textarea></td>
    </tr>
  </table>
  <p>
    <input name="Submit" type="submit" value="Submit">
    <input name="Reset" type="reset" value="Reset">
  </p>
  </div>
</form>
Current PHP Script:
PHP:
<?php
	$to = "[email protected]";
	$subject = "Formula Suite Contact";
	$name = $_POST['Name'];
	$email = $_POST['E-mail'];
	$program = $_POST['Program'];
	$topic = $_POST['Topic'];
	$message = $_POST['Question'];
	$headers = "From: $email";
	mail($to,$subject,"Name: $name\n\nE-mail: $email\n\nProgram: $program\n\nTopic: $topic\n\nQuestion: $message",$headers);
	$time = date("r");
	$ip = $_SERVER['REMOTE_ADDR'];
	$writeto = "logs/contact.txt";
	$text = "\nTime: $time\nName: $name\nE-mail: $email\nIP: $ip\nProgram: $program\nTopic: $topic\nQuestion: $message\n";
	$handle = fopen($writeto, 'a,t');
	fwrite($handle, $text);
	fclose($handle);
?>
This script just mails me the results of the form and logs them into a file on my server (which is password protected).
 
Joined
Jan 6, 2006
Messages
38
Place this in your head:

Code:
<script type="text/javascript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Written by Steve - http://jsmadeeasy.com/ -->

<!--
function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

//Contador de caracteres.
function Contar(entrada,salida,texto,caracteres) {
  var entradaObj=getObject(entrada);
  var salidaObj=getObject(salida);
  var longitud=caracteres - entradaObj.value.length;
  if(longitud <= 0) {
    longitud=0;
    texto='<span class="disable"> '+texto+' </span>';
    entradaObj.value=entradaObj.value.substr(0,caracteres);
  }
  salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
}

//-->
</script>
And replace this:
Code:
<textarea name="Question" cols="40" id="Question"></textarea>
With this code:
Code:
<textarea name="Questions" cols="40" rows="5" size="60" id="eBann" name="bannerURL" maxlength="100" onKeyUp="Contar('eBann','sBann','{CHAR} characters left.',100);"></textarea>
<br><span id="sBann">100 characters left.</span>
Format the textarea, font, etc. as you need to. To change the limit on your textarea, replace "100" with whatever number you need.

PS: Found this by doing a simple Google search. :) Credit is given in the comments for the Javascript.
 
Joined
Jul 8, 2002
Messages
14,681
Users with JavaScript disabled can still go over the limit.
You could use PHP's strlen() function to get the length of the value entered, and use an if() statement to check if its longer than it should be.
 
Joined
Jul 8, 2002
Messages
14,681
PHP:
$name = $_POST['Name'];
if (strlen($name) > 40) { die('Name may not be longer than 40 characters.'); }
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top