Form validation?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Form validation?

Post by jmandango »

I've seen this done before....I think it's using frontpage extensions which I wan't to stay away from. I want to be sure the user enters information into the form correctly and they receive a specific warning if they don't.

Code: Select all

<script Language="JavaScript"><!--
function FrontPage_Form1_Validator(theForm)
&#123;

  if (theForm.first_name.value == "")
  &#123;
    alert("Please enter a value for the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;

  if (theForm.first_name.value.length < 1)
  &#123;
    alert("Please enter at least 1 characters in the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;

  if (theForm.first_name.value.length > 50)
  &#123;
    alert("Please enter at most 50 characters in the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;
 return (true);
&#125;
//--></script>
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

I guess I do not see the question in your post. Can you clarify it please?




Direwolf
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Post by jmandango »

My question is. Can I do this type of thing using PHP or even a similar JAVASCRIPT code? If so, how?

thanks
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Yes, you can do this with either PHP os JavaScript, but you should realize that it IS JavaScript. The reference to Front Page (FrontPage_Form1_Validator) is only the name of the function that validates the form. It could be called anything but the person that wrote it gave it a meaningful function name... meaningful to him that is. Does this code work the way you have it written?


Direwolf
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Post by jmandango »

No it doesn't work in this manner. Help pretty please.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It might be easier if we knew what your form looked like.

Mac
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Post by jmandango »

Here is my code
I want to be able to give a warning if they don't enter info correctly.

Code: Select all

<?php

if ($submit) &#123;

  // process form

  $db = mysql_connect("localhost", "user", "pass");
  
  mysql_select_db("database",$db);
  
  $sql = "INSERT INTO table (first_name,last_name,etc) VALUES ('$first_name','$last_name')";
  
  $result = mysql_query($sql);
  
  echo '<table align="center" cellpadding="3"><tr><td align="center" class="graybold">Thank you '.$_POST&#1111;'first_name'].' , for voting!<br>Please look for the results in our September issue.</td></tr></table>';

&#125; else&#123;


	// display form
	?>

<form method="post" action="<?php echo $PHP_SELF?>">
	
	<div align="center">
	<table width="450" border="1" cellspacing="0" cellpadding="3" class="graybody" bordercolor="#999999">
            <tr> 
              <td colspan="2" align="center">Please Enter Your Personal Information</td>
            </tr>
            <tr> 
              <td width="150" align="right">First / Last Name</td>
              <td> 
                <input type="text" name="first_name" size="20">
                * 
                <input type="text" name="last_name" size="20">
                * </td>
            </tr>
            <tr>
            <td> 
            
              <input type="submit" name="submit" value="Enter Information">
              <input type="reset">
            
          </td>
        </tr>
      </table>
</form>
	
	
	<?php
	
&#125; // end if

?>
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

try adding this before your query:

Code: Select all

<?php
if (empty($firstname)) || empty($second_name)) &#123;
    echo "Form not filled out completely";
&#125; else &#123;
mysql_query etc...
&#125;
?>
Post Reply