Forum Moderators: coopster

Message Too Old, No Replies

Using PHP to send form results. How to create required fields

         

shush1

7:21 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



Hi I have a form which I use PHP to submit results to my email address, how can I get the html form to have required fields? I want name, address and post code to be the required fields before someone can submit the form.

Below is my PHP script, hope someone can help.

Thanx

<?php

$result = "Beechwood Homes website enquiry form\r\n\r\n";
$result .= "Developments: " . $Developments . "\r\n";
$result .= "Name: " . $Fname . "\r\n";
$result .= "Last Name: " . $Lname . "\r\n";
$result .= "Email: " . $Email . "\r\n";
$result .= "Tel: " . $Phone . "\r\n";
$result .= "Address: " . $Address . "\r\n";
$result .= "Address2: " . $Address2 . "\r\n";
$result .= "Town: " . $Town . "\r\n";
$result .= "County: " . $County . "\r\n";
$result .= "Postcode: " . $Postcode . "\r\n";
$result .= "Country: " . $Country . "\r\n";
$result .= "Min property price: " . $Minprice . "\r\n";
$result .= "Max property price: " . $Maxprice . "\r\n";
$result .= "Do you have a house to sell?: " . $Selling . "\r\n";
$result .= "How did you find this Web Site?: " . $Source . "\r\n";
$result .= "Please tick the box if you would like to be added to our mailing list: " . $Mailinglist . "\r\n";

if ($Email) {
$headers = "From: $Fname <$Email>\r\n\r\n";
} else {
$headers = "From: No Name given <anonymous@yourserver.com>\r\n\r\n";
}

$sendit = mail("my@domain.co.uk", "Website contact form", $result, $headers) or die("Something went wrong");

echo "<meta http-equiv='refresh' content='0; url=thank.htm'>";
die;

?>

surrealillusions

8:11 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



You need to check the contents of the form fields. The script you have there has no security checks and therefore can be used for spam etc..

To make fields 'required', you need to check that the fields have something in them.

For example -

$errorString = '';
if(sizeof($_POST)==0){
$errorString .= 'No form data submitted<br />';
die($errorString);
}

Hope that helps you on the way

:)

shush1

8:16 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



Hi thanx for reply, im fairly new to PHP, how do i get my form to be secure? also do I have to change anything on the actual html form?

thanx

cameraman

9:07 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These threads, in this forum's library, should help:
Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]
Combatting Webform hijack [webmasterworld.com]

shush1

6:42 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Hi thanx for that, i have made some changes but I when I submit the the form it comes up with error and doesnt prompt when required fields are not filled in. can anyone help?

<?php

// Receiving variables
@$Developments = addslashes($_POST['Developments']);
@$Fname = addslashes($_POST['Fname']);
@$Lname = addslashes($_POST['Lname']);
@$Email = addslashes($_POST['Email']);
@$Phone = addslashes($_POST['Phone']);
@$Address = addslashes($_POST['Address']);
@$Address2 = addslashes($_POST['Address2']);
@$Town = addslashes($_POST['Town']);
@$County = addslashes($_POST['County']);
@$Postcode = addslashes($_POST['Postcode']);
@$Country = addslashes($_POST['Country']);
@$Minprice = addslashes($_POST['Minprice']);
@$Maxprice = addslashes($_POST['Maxprice']);
@$Selling = addslashes($_POST['Selling']);
@$Source = addslashes($_POST['Source']);
@$Mailinglist = addslashes($_POST['Mailinglist']);

// Validation
if (strlen($Fname) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid First name</font></p>");
}

if (strlen($Lname) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid Last name</font></p>");
}

if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $Email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (strlen($Address) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid address</font></p>");
}

if (strlen($Postcode) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid postcode</font></p>");
}

//Sending Email to form owner
$pfw_header = "From: $Email\n"
. "Reply-To: $Email\n";
$pfw_subject = "Beechwood Homes website form";
$pfw_email_to = "shush@freshlemon.co.uk";
$pfw_message = "Developments: $Developments\n"
. "Fname: $Fname\n"
. "Lname: $Lname\n"
. "Email: $Email\n"
. "Phone: $Phone\n"
. "Address: $Address\n"
. "Address2: $Address2\n"
. "Town: $Town\n"
. "County: $County\n"
. "Postcode: $Postcode\n"
. "Country: $Country\n"
. "Minprice: $Minprice\n"
. "Maxprice: $Maxprice\n"
. "Selling: $Selling\n"
. "Source: $Source\n"
. "Mailinglist: $Mailinglist\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//Sending auto respond Email to visitor
$pfw_header = "From: shush@freshlemon.co.uk\n"
. "Reply-To: shush@freshlemon.co.uk\n";
$pfw_subject = "Confirmation of your enquiry to Beechwood Homes";
$pfw_email_to = "$Email";
$pfw_message = "Thank you $Fname";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//saving record in a text file
$pfw_file_name = "form-results.csv";
$pfw_first_raw = "Developments,Fname,Lname,Email,Phone,Address,Address2,Town,County,Postcode,Country,Minprice,Maxprice,Selling,Source,Mailinglist\r\n";
$pfw_values = "$Developments,$Fname,$Lname,$Email,$Phone,$Address,$Address2,$Town,$County,$Postcode,$Country,$Minprice,$Maxprice,$Selling,$Source,$Mailinglist\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name))
{
$pfw_is_first_row = true ;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
die("Cannot open file ($pfw_file_name)");
exit;
}
if ($pfw_is_first_row)
{
if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
die("Cannot write to file ($pfw_filename)");
exit;
}
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
die("Cannot write to file ($pfw_filename)");
exit;
}
fclose($pfw_handle);

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Thank you</font></p>");
?>

cameraman

7:01 pm on Mar 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at addslashes() [us.php.net] - you should check for magic_quotes_gpc.

Lose the @ signs - those suppress error messages, which you want to see at this point. After you know your script is bug-free, you can use error_reporting() [us3.php.net].

There's not really anything wrong with doing this:
if (strlen($Fname) == 0 )

But it's simpler and faster to to do this:
if($Fname == '')

What's the error you're getting? If it mentions a line number, please also point out which line it is.

shush1

8:01 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Hi this is the page that comes up after submit:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

[edited by: coopster at 8:03 pm (utc) on Mar. 10, 2008]
[edit reason] generalized email [/edit]

cameraman

12:06 am on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it sending the emails?
To isolate the error, try putting an exit() line at the spot just before it writes the file. If the error goes away, you know it's in the file writing section - check directory/file permissions and such. If the error persists, keep moving the exit line up until the error goes away.

shush1

12:14 am on Mar 11, 2008 (gmt 0)

10+ Year Member



no its not sending anything, nor does it prompt when fields are left blank

thanx

cameraman

12:53 am on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No prompts has become a secondary problem, first you need to figure out why it's generating the internal server error.
Compare the script file's permissions against others that work ok. Try turning the lower half of the file (everything above where it starts to send the email) into one big comment with the /* */ markers.
If it's not a permission problem, you want to keep commenting or exiting until the script does something other than generate an error. Then you can resume working on the script itself.

shush1

11:24 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Hi I am having no joy and really desperate, is there another way I can get this sorted?

cameraman

5:43 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know of any magic spells for computer scripts, so no.

If you'll report your results I/we can help. What happens if you comment the whole bloody thing and add an echo line:
<?php
echo "Hello.";
/*
// All the stuff you've posted
*/
?>
Does the script execute and produce the word in your browser or does it have an internal server error?
1. If there's an error, you've got permission problems. Try changing it to 644 on the server with a file manager or ftp program. Or try a brand new file.
2. If there's no error, move the /* down a few lines and execute it again. Keep going until the error reappears.

Report back after your experiments.