Web Hosting Talk







View Full Version : Simple PHP, check if forms are filled in


Suedish
04-16-2004, 07:34 AM
Hiya.

I'm not very good with PHP so i would like some help, if someone could please spare the time for it. Sorry that it is long, but I wanted to get the whole code with it.
What i wanna is make all the forms mandatory to fill in, so that people can't simply refuse to enter information in one of the forms. I truly hope I can get some help with this..
(the form names are in swedish, but i hope it's ok anyway ..)

---- NEW FILE: FILE.PHP

<html>
<head>
<title>Send an email through form</title>
</head>
<body>
<form action="sendformmail.php" method="post">

<table>
<tr>
<td>Namn:</td><td><input type="text" name="namn" style="background:#DDDDDD;color:black;border:1px solid black;"></td>
</tr><tr>
<td>Företag:</td><td><input type="text" name="foretag" style="background:#DDDDDD;color:black;border:1px solid black;"></td>
</tr><tr>
<td>Telefon:</td><td><input type="text" name="telefon" style="background:#DDDDDD;color:black;border:1px solid black;"></td>
</tr><tr>
<td>E-post:</td><td><input type="text" name="epost" style="background:#DDDDDD;color:black;border:1px solid black;"></td>
</tr></table>

<table>
<tr>
<td>Ärende:<br>
<textarea name="arende" cols=30 rows=5 style="background:#DDDDDD;color:black;border:1px solid black;"></textarea></td>
</tr><tr>
<td><br><br><input type="submit" value="Submit Form"></td>
</form>
</tr></table>

</body>
</html>

--- EOF FILE.PHP

--- NEW FILE: SENFORMMAIL.PHP
<html>
<head>
<title>An email has been sent"</title>
</head>
<body>
<?php
print "Thank you <b>$_POST[namn]</b> for filling out the forms!<br><br>\n";
print "We will contact you as soon as possible\n";

//build the mail
$msg = "Namn: $_POST[namn]\n";
$msg .="Företag: $_POST[foretag]\n";
$msg .="Telefon: $_POST[telefon]\n";
$msg .="Epost: $_POST[epost]\n";
$msg .="Ärende: $_POST[arende]\n";

//config the actual mail
$mottagare = "roger.almstedt@mainline.se";
$subject = "Förfrågan från mainline.se";
$mailheaders = "From: Mainline Kontaktformulär <dettaarreply@ione.se> \n";
$mailheaders .= "Reply-To: $_POST[epost]";

//send the mail
mail($mottagare, $subject, $msg, $mailheaders);
?>
<form method="post">
<input type="button" value="Close Window"
onclick="window.close()">
</form>

</body>
</html>

ambirex
04-16-2004, 10:34 AM
you want the functions empty() and isset()
http://us4.php.net/empty

so your code would look something like

if(empty($_POST[var]) | empty($_POST[var2] | empty($_POST[var3] ... )
{
echo "you need to enter every field";
}
else
{
do something...
}
this would exclude any var that is 0 or FALSE
but maybe you want isset instead of empty... there is a good comparison on page above. you will have to decide what is best for you.

trukfixer
04-16-2004, 10:49 AM
I use a simple javascript function to check OnSubmit="Validate_form();" (basically) so when a form is submitted, javascript first checks form fields for inputs, then in the following php page the inputs themselves are checked for being within parameters. (I use a definitive Validate_Email regex script to check email formats, otherwise, I use regex (preg_replace) to remove undesired input and check for str_len() or is_numeric() or whatever I require for teh form fields. The javascript in the form prevents submitting the page without all fields fileld in, the regular expressions check user input to ensure no injection of SQL or nasty php code. (NTUI- Never Trust User Input)

I'd be happy to provide copies of the javascript (I found it by googling the web for snippets) and/ or the php code regular expressions (open source GPL) if you PM me your email address....

its all pretty easy to implement, the hard part is done- building the ever so tricky regex checks.. :)

Misto-Roboto
04-16-2004, 10:50 AM
Actually since you are expecting all fields to filled out you should just use the foreach in combination with isSet or ! to check to see if any POST globals aren't there:


foreach($_POST as $key => $value)
{
if((!isSet($value)) || (!$value) || ($value = ""))
{
header("Location: file.php");
exit;
}
}

ambirex
04-16-2004, 12:03 PM
Originally posted by Misto-Roboto
Actually since you are expecting all fields to filled out you should just use the foreach in combination with isSet or ! to check to see if any POST globals aren't there:


foreach($_POST as $key => $value)
{
if((!isSet($value)) || (!$value) || ($value = ""))
{
header("Location: file.php");
exit;
}
}

I was just trying to keep it as simple as possible.

first: Misto has it right, I made a mistake in my code with the single "|" (comparison operator vs the correct logical operator)

I mainly use empty for $_GET vars as you can have a script that accepts something like blah.php?somevar=2$othervar= and isset will say true for othervar. I suppose in post it isn't as much of an issue ( and his code will handle that).

but I think:

foreach($_POST as $value)

is more clear (as there is no need to look at the key)

you could also look into javascript form validation so the user doesn't have to hit submit to be reminded of the mistake.

Sheps
04-16-2004, 01:09 PM
You should use the key => value. It just keeps everything consistent, and then he might also want to compare against a "required" array, which the key would be valuable for.

Misto-Roboto
04-17-2004, 10:22 AM
Originally posted by Sheps
You should use the key => value. It just keeps everything consistent, and then he might also want to compare against a "required" array, which the key would be valuable for.

Yes, like if he wanted to find out which fields were not filled in and then rewrite the form in PHP with the fields showing which ones were missed using the keys that were missed.

C~J~V
04-21-2004, 09:35 PM
JavaScript is far better suited for form validation.
But if JavaScript is not an option, then your PHP is going to be fun.
Key thing is to make sure that the form doesn't have to be completely filled out again if somebody messes up and submits with incorrect data.

Burhan
04-22-2004, 03:07 AM
If you use sessions, you can then redirect the user back to the form page to display an appropriate error message. Store you form values in the session, so that the user doesn't have to fill out the correct information.

-- OR --

Just use one script and avoid the redirects. If there has been no submission of information ($_POST is empty), display the form. Otherwise, do the checks. Avoids having to redirect.

ambirex
04-22-2004, 11:32 AM
On a tangent, you might want to look at PEAR's HTML_QuickForm (http://pear.php.net/manual/en/package.html.html-quickform.php#package.html.html-quickform.introduction). There somewhat of a high learning curve, but once you get it down its really easy to whip out forms and add validation rules to it.


<?php
// Load the main class
require_once 'HTML/QuickForm.php';

// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('firstForm');

// Add some elements to the form
$form->addElement('header', null, 'QuickForm tutorial example');
$form->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255));
$form->addElement('submit', null, 'Send');

// Define validation rules
$form->addRule('name', 'Please enter your name', 'required', null, 'client');

// Try to validate a form
if ($form->validate()) {
echo '<h1>Hello, ' . htmlspecialchars($form->exportValue('name')) . '!</h1>';
exit;
}

// Output the form
$form->display();
?>