Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Scripting - "Contact Us" Form

Status
Not open for further replies.

TechCarnivore

Technical User
Apr 13, 2006
249
US
We are using a PHP script on the "Contact Us" page of our website. Currently anyone can submit a blank request. That is anyone can press submit and an empty form is emailed off.

How can we fix this? I don't do PHP scripting but I'm interested to know what it would take to atleast require a name and phone number or name and email etc...


Thanks
 
there is a two-tier method. the first tier is client-side validation, using javascript (forum216). this is not required but is helpful at lessening server traffic.

you check to make sure the user has entered the required information when he/she clicks submit. if not, you prevent the page from submitting at all. otherwise, the form is permitted to submit.

the second tier is server-side validating. using php you simply verify that the user has entered the appropriate data in the necessary fields. if not, you display an error message, do not process the form, and re-display the form. otherwise, you continue as normal.

to get a more helpful/specific response, you'll need to start us off by showing us the code you've attempted thus far.

also, you might want to google for "php form validation".



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
You need to validate the form processing.

<?php

if (empty ($_POST["fieldname"]) || empty ($_POST["other_fieldname"])) {

echo 'NOT ALL FIELDS FILLED IN';

// re-show the form again with the values of what they entered

echo '<input type="text" name="fieldname" value="' . $_POST["fieldname'] . ">';
} else {
// mail the form
}

?>

That is the most basic thing you can do.

Hope this helps!

NATE


mainframe.gif


Got a question? Search G O O G L E first.
 
Spyderix...

Please post your code in
Code:
tags. Pasting code into the body of a message will inevitably provide incomplete code (as the TGML parser will attempt to parse this code as well as the message). There are also some typos in the code you put up (so it will never work).

cLFlaVA's response covers all the bases - I don't understand the need to post a further (less complete, less informed) solution in a non-programming forum. If we keep encouraging this kind of behaviour, Tek-tips will lose some of it's quality appeal.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks for your help. I don't quite understand all of this however I believe this is what you're asking for.

I've edited some of the content that reflects email addresses.

Code:
<?php 
$s_name = addslashes($_POST['name']);
$s_phone = addslashes($_POST['phone']);
$s_email = addslashes($_POST['email']);
$s_title = addslashes($_POST['title']);
$s_attention = addslashes($_POST['attention']);
$l_interest = addslashes($_POST['linux_interest']);
$p_interest = addslashes($_POST['project_interest']);
$n_interest = addslashes($_POST['network_interest']);
$s_interest = addslashes($_POST['service_interest']);
$d_interest = addslashes($_POST['disaster_interest']);
$st_interest = addslashes($_POST['staff_interest']);
$se_interest = addslashes($_POST['server_interest']);
$e_interest = addslashes($_POST['enterprise_interest']);
$h_interest = addslashes($_POST['high_interest']);
$np_interest = addslashes($_POST['networkp_interest']);
$s_comments = addslashes($_POST['comments']);
$s_email_content =   "Name: ".$s_name."\n"
					."Phone: ".$s_phone."\n"
					."Email: ".$s_email."\n"
					."Title: ".$s_title."\n"
					."Attention: ".$s_attention."\n"
					."Interests: ".$l_interest." , ".$p_interest." , ".$n_interest." , ".$s_interest." , ".$d_interest." , ".$st_interest." , ".$se_interest." , ".$e_interest." , ".$h_interest." , ".$np_interest."\n"
					."Comments: ".$s_comments."\n";
$to_email = 'XXXX@XXXX.Com';
$subject_line = 'Client Contact Us Request';
/* $email_headers = 'From: ' .$_POST['email'] . "\r\n" .	
                 'Reply-to: ' .$_POST['email'] . "\r\n" .
				 'X-Mailer: PHP/' . phpversion(); */
				 
mail($to_email, $subject_line, $s_email_content, "From: " .$_POST['email']);
?>

Now that you see the script we have in place, where should we make our edits? Also do you see anything that should be cleaned up or changed?

Regards,
 
TechCarnivore,

What are the required fields to be set before you want the script to send the e-mail to you?

Each field can be checked, but some are required and some arent, I recon?

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top