Forum Moderators: coopster

Message Too Old, No Replies

PHP form validation

         

ganderla

2:50 am on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I am very new at any coding and to be truthful, I had to follow a tutorial to get this.
I have a form for my site that sends me an email with the persons name, email and message. I get tons of blank forms a day becuause my current form does not validate.
I downloaded vdaemon for dreamweaver to try and help. I set up everything like the tutorial said and it is still submitting the blank form.
Here is the code, can someone help me?

<?php include('vdaemon/vdaemon.php');?>
<html>
<head>
<title>Quick Contact Form Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Quick Contact Form Sample</h1>
<form method="POST" name="QContact" runat="vdaemon" action="qcontact_p.php">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="100">
<vllabel form="QContact" validators="NameReq" errclass="error">Name:</vllabel>
</td>
<td width="220">
<input name="Name" type="text" class="control" size="20">
<vlvalidator name="NameReq" type="required" control="Name" errmsg="Name required" clientvalidate="true">
</td>
<td width="300" rowspan="5" valign="top">
<vlsummary class="error" headertext="Error(s) found:" displaymode="bulletlist">
</td>
</tr>
<tr>
<td>
<vllabel form="QContact" validators="EmailReq,Email" errclass="error">E-mail:</vllabel>
</td>
<td>
<input name="Email" type="text" class="control" size="20">
<vlvalidator name="EmailReq" type="required" control="Email" errmsg="E-mail required">
<vlvalidator name="Email" type="email" control="Email" errmsg="Invalid E-mail" clientvalidate="true">
</td>
</tr>
<tr>
<td colspan="2">
<vllabel form="QContact" validators="MessageReq" errclass="error">Message/Question:</vllabel>
</td>
</tr>
<tr>
<td colspan="2">
<textarea name="Message" cols="40" rows="7" wrap="virtual" class="control"></textarea>
<vlvalidator name="MessageReq" type="required" control="Message" errmsg="Message required" clientvalidate="true">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" class="control" value="Send">
<input type="reset" class="control" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>

lukasz

6:59 am on Dec 15, 2003 (gmt 0)

10+ Year Member



In my opinion it is not a good idea to validate form using php as javascript is much more suitable for the job.
I used something like this in the head of the page:
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
if (form.lastname.value == "") {
alert( "please enter something" );
form.lastname.focus();
return false ;
}
if (form.firstname.value == "") {
alert( "please enter something");
form.firstname.focus();
return false ;
}
return true ;
}
</script>
and then in form tag:
onSubmit="return checkform(this);"
where lastname and firstname are form fields.
Works perfect for me althought that was the only time I have ever touched javascript.

dmorison

7:08 am on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Basic Form Input Validation in PHP using Regular Expressions:

[webmasterworld.com...]

lukasz:
Even if you have done client side verification with JavaScript you should still verify server side also - you must never trust anything coming from the client!

As a particular warning in this case; I don't know which method is being used to send the email; but if it is a case of constructed text being piped to sendmail then there is the possibility for an attacker to inject shell commands (similar to SQL injection) if nothing is done to protect against it!

jetboy_70

1:31 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



You'll find many variations of an email validating regex, but this is what I use:


// Check for valid email
function api_is_email($input)
{
if ($input)
{
if (eregi("^[-a-z0-9_][-a-z0-9_.]*\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com¦edu¦gov¦int¦mil¦net
¦org¦biz¦info¦name¦museum¦coop¦aero¦[a-z][a-z])$", $input)) return 1;
}
}

[edited by: jatar_k at 5:50 pm (utc) on Dec. 15, 2003]
[edit reason] broke line for sidescroll [/edit]

ganderla

2:20 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for the help, the form is validating now, but it is getting stuck on the php page and not sending people to the thank you page.
Is this how it should look in the form tag?

<form action="********.php" method="POST" onsubmit="return check form(this)" name="myform" language="JavaScript">

jetboy_70

5:45 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



"not sending people to the thank you page"

Have you told it to? Something like:

header("Location: /thankyoupage.html");

will do the trick - but only trigger it if the form validates!

ganderla

5:47 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I figured it out. I had a space in the wrong place.
I really appreciate everyone's help, I thrive on this place.

Bonnie Kinship

4:04 pm on Dec 28, 2003 (gmt 0)

10+ Year Member



Following is a Javascript it will definitely help to avoide receiving blank forms:

PART: 1 Include following script in your head tags

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function checkFields() { // field validation -
if ( (document.emailform.name.value=="") ¦¦ // checks if fields are blank.
(document.emailform.email.value=="") ¦¦ // checks if fields are blank.
(document.emailform.phone.value=="") ¦¦ // checks if fields are blank.
(document.emailform.comments.value=="") ) // checks if fields are blank.
{
alert("Please enter your name, phone, email, and comments then re-submit this form.");
return false;
}
</SCRIPT>
</HEAD>

PART:2 INLCUDE ON SUBMIT IN <BODY> <FORM> TAG

<BODY>
... ... ...
<form action="ANY" method="ANY" onSubmit="return checkFields()" name="NAME OF YOUR FORM">

... ... ...

</BODY>

HOPE, THIS WILL SERVE YOUR PURPOSE.
BEST LUCK!

NickCoons

5:09 pm on Dec 28, 2003 (gmt 0)

10+ Year Member



ganderla,

JavaScript is a good way to validate the form because it happens more immediately since there is no interaction with the server, but you may want to include PHP validation also.

You said you are getting a lot of blank submissions.. I've gotten the same thing before, and in checking my logs, I've found that most of these come through from bots. The problem here is that a bot is unlikely to understand the JavaScript. It will most likely suck the URL from the action="..." parameter and follow it causing a blank submission.

So on my site, I've implemented both. This makes it easy and friendly for users who have accidentally missed something, but verifies everything on the backend as well in case someone tries to circumvent the JavaScript (or has it disabled).

ganderla

5:16 pm on Dec 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you all for your help, I will not be getting any more blank forms.