By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Contact Us script

P: n/a
Hello,

I'm relatively new to PHP so I found this free contact us script on
the net that i was going to use for my Contact Us php page. Its works
pretty good except for error handling. I was wondering if any can
hlep me understand what is missing. I've posted the php files below
below. Contact.php calls the sendemail.php script. This appears to be
the validation code in the sendemail.php file. But i would like it to
validate and prefer a pop up box instead on the Contact.php page
before contining to the confirmation page (sendemail.php). Any help
would be appreciated.

Al

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !
strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

==========================CONTACT,PHP============= ================
<table width="100%" cellpadding="5" border="2">
<tr><td><form method="post" action="sendemail.php">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?
>" />
Your Name:<br />
<input type="text" name="visitor" size="35" />
<br />
Your Email Address:<br />
<input type="text" name="visitormail" size="35" />
<br />
<br />
Attention:<br />
<select name="attn" size="1">
<option value=" General Support ">General Inquiries </option>
<option value=" Sales n Billing ">Sales & Billing </option>
<option value=" Technical Support ">Technical Support </option>
<option value=" Webmaster ">Webmaster </option>
</select>
<br /><br />
Message:
<br />
<textarea name="notes" rows="25" cols="49"></textarea>
<br />
<input type="submit" value="Send Mail"/>
<br />
</form></tr></td>
</table>
==============================END OF SCRIPT======================

=====================SENDEMAIL.PHP script=========================
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
?>

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !
strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n";

$from = "From: $visitormail\r\n";
mail("in**@yoursite.com", $subject, $message, $from);
?>

<table width=100% cellpadding="10" border="2">
<tr>
<th align="center"><h3Confirmation of Message Sent</h3></th>
</tr>
<tr>
<td>
<b>IP:</b<?php echo $ip ?>
<br />
<b>Attention:</b<?php echo $attn ?>
<br />
<b>From:</b<?php echo $visitormail ?>
<br />
<b>Date:</b<?php echo $todayis ?>
<br />
<br />
Thank you <?php echo $visitor ?>, your message has been sent
successfully. We generally respond to emails within 24-48 hours.
Thanks for contacting us!
<br />
<br />
</td>
</tr>
<tr>
<td align="center">
<a href="/index.php"Home </a>
</td>
</tr>
</table>
=========================END OF SCRIPT==============================

Jun 3 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
thanos wrote:
Hello,

I'm relatively new to PHP so I found this free contact us script on
the net that i was going to use for my Contact Us php page. Its works
pretty good except for error handling. I was wondering if any can
hlep me understand what is missing. I've posted the php files below
below. Contact.php calls the sendemail.php script. This appears to be
the validation code in the sendemail.php file. But i would like it to
validate and prefer a pop up box instead on the Contact.php page
before contining to the confirmation page (sendemail.php). Any help
would be appreciated.

Al
Al,

You can't do popups in PHP. You would need Javascript to do that - and
that adds a whole new layer of complexity.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 4 '07 #2

P: n/a
thanos wrote:
Hello,

I'm relatively new to PHP so I found this free contact us script on
the net that i was going to use for my Contact Us php page. Its works
pretty good except for error handling. I was wondering if any can
hlep me understand what is missing. I've posted the php files below
below. Contact.php calls the sendemail.php script. This appears to be
the validation code in the sendemail.php file. But i would like it to
validate and prefer a pop up box instead on the Contact.php page
before contining to the confirmation page (sendemail.php). Any help
would be appreciated.

Al

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !
strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

==========================CONTACT,PHP============= ================
<table width="100%" cellpadding="5" border="2">
<tr><td><form method="post" action="sendemail.php">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?
>" />

Your Name:<br />
<input type="text" name="visitor" size="35" />
<br />
Your Email Address:<br />
<input type="text" name="visitormail" size="35" />
<br />
<br />
Attention:<br />
<select name="attn" size="1">
<option value=" General Support ">General Inquiries </option>
<option value=" Sales n Billing ">Sales & Billing </option>
<option value=" Technical Support ">Technical Support </option>
<option value=" Webmaster ">Webmaster </option>
</select>
<br /><br />
Message:
<br />
<textarea name="notes" rows="25" cols="49"></textarea>
<br />
<input type="submit" value="Send Mail"/>
<br />
</form></tr></td>
</table>
==============================END OF SCRIPT======================

=====================SENDEMAIL.PHP script=========================
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
?>

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !
strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n";

$from = "From: $visitormail\r\n";
mail("in**@yoursite.com", $subject, $message, $from);
?>

<table width=100% cellpadding="10" border="2">
<tr>
<th align="center"><h3Confirmation of Message Sent</h3></th>
</tr>
<tr>
<td>
<b>IP:</b<?php echo $ip ?>
<br />
<b>Attention:</b<?php echo $attn ?>
<br />
<b>From:</b<?php echo $visitormail ?>
<br />
<b>Date:</b<?php echo $todayis ?>
<br />
<br />
Thank you <?php echo $visitor ?>, your message has been sent
successfully. We generally respond to emails within 24-48 hours.
Thanks for contacting us!
<br />
<br />
</td>
</tr>
<tr>
<td align="center">
<a href="/index.php"Home </a>
</td>
</tr>
</table>
=========================END OF SCRIPT==============================
I have briefly gone over the code you posted and there seem to be
several problems with it.

- email validation could still let an invalid mail go through. Better
use regexp for this
- there is nothing to stop the script from executing mail() if the
validation fails. Basically you just show the error message and merrily
go about trying to send the mail. A simple yet ugly solution would be to
insert a die() after an error was encountered.
- client-side validation, which you seem to be asking for, is usually
done using JavaScript. however never rely solely on client-side
validation as it can be tampered with (or not work at all if the client
browser has javascript turned off). that means while it may be nice of
you to implement it, you still have to have the server-side validation code

Something I might want to do with your code regarding the user interface
and error checking

- copy all the $_POST data into $_SESSION if error occurs, add a
$_SESSION["error"] = 1 as a flag. Also add a $_SESSION["messages"] with
all the error messages generated while validating
- do something along the lines of
header("Location: contact.php");
die()
after validation if any of the validation conditions have failed
The validation code might look like

$_SESSION["messages"] = "";
$errorflag = 0;

if () // email not valid
{
$_SESSION["messages"] .= "Email-invalid<br />";
$errorflag = 1;
}
if () // visitor name empty
{
$_SESSION["messages"] .= "Visitor name empty!<br />";
$errorflag = 1;

}
/*
other ifs
*/

if ($errorflag == 1)
{
$_SESSION["visitor"] = $visitor;
$_SESSION["visitormail"] = $visitormail;
// etc
$_SESSION["error"] = 1;
header("Location: contact.php");
die();
}

// the rest of sendemail.php script

- on contact.php check for existence of $_SESSION["error"], if it is
there print out the $_SESSION["messages"] error messages on an
appropriate location. Then use the saved $_POST members to fill the form
elements so that user does not have to retype everything, i.e. something
like

<input type="text" name="visitormail" value="<?php echo
$_SESSION["visitormail"]; ?>" size="35" />

and unset the session variables

The code is by no means perfect (not even tested) and it should by used
as a reference to what you may want to do. With something like this
implemented, you may decide to go on and implement client side
validation as well, but you're not dependant on your client actually
supporting it or someone deciding to circumvent it.
Jun 4 '07 #3

P: n/a


Jerry:

Wouldn't something like this help him on popups?

if (something)
echo "<script type='text/javascript'alert('something') </
script>";
Jun 7 '07 #4

P: n/a
Phil wrote:
>
Jerry:

Wouldn't something like this help him on popups?

if (something)
echo "<script type='text/javascript'alert('something') </
script>";

Sure, but that's not PHP. It's javascript - and probably won't be shown
until the page is completely loaded.

And it won't show at all if the user don't have javascript enabled.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.