PDA

View Full Version : Contact Form Mix of compulsory and non


JohnL
01-13-2009, 09:16 AM
I am attempting to find a way that I can have a php contact form that has the ability to have some fields that are compulsory to fill in and others that are not. I have a form that works OK but I would like to knowif it is possible to edit to do as requested?

Here is the code:

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Contact Form Request';

// Your email address. This is where the form information will be sent.
$emailadd = 'ross@azdeadpestcontrol.com.au';

// Where to redirect after form is processed.
$url = 'http://azdeadpestcontrol.com.au/dw/example3/contactformthanks.html';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

This code has the all boxes to be filled in or none.

Thanks,

JL

edbr
01-13-2009, 09:32 AM
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
is lokking at all posted values, you would have to re write it to select spesific poste values .
have a look at jacks mail script from http://www.dtheatre.com/scripts/
that has a way of choosing which fields are required
also a manual making it very easy to implement