I need some php scripting help for a web form

djrobuk

New Member
Messages
3
Reaction score
0
Points
0
I have a php script on my website to handle a form on a webpage, but I'm a bit clueless when it comes to php scripting. I have entered my email address in the php script (this address is what the form will be sent to). When I trial the form and submit, it says successfully sent but I don't recieve any emails. I understand that the web space host needs to be able to accept php to do this, which it is. I'm not sure how a host is able to handle a request to send an email though.

My script is as follows:

<?php

$my_email = "MY EMAIL ADDRESS IS PLACED HERE";

$continue = "MY WEB ADDRESS HERE";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Check all fields for an email header.

function recursive_array_check_header($element_value)
{

global $set;

if(!is_array($element_value)){if(preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i",$element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_header($value);}

}

}

recursive_array_check_header($_REQUEST);

if($set){$errors[] = "You cannot send an email header";}

unset($set);

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or a colon";}

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($_REQUEST);

if(!$set){$errors[] = "You cannot send a blank form";}

unset($set);

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."";

$message = stripslashes($message);

$subject = "FormToEmail Comments";

$headers = "From: " . $_REQUEST['email'];

mail($my_email,$subject,$message,$headers);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<title>Thank You - Your Message Has Been Sent</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" text="#000000">

<div>
<center>
<b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue; ?>">Click here to continue</a></p>
</center>
</div>

</body>
</html>
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
One thing you can try is make sure the variables contain what you want.
Try testing:

HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
 
<head>
<title>Thank You - Your Message Has Been Sent</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body bgcolor="#ffffff" text="#000000">
 
<div>
<center>
<b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue; ?>">Click here to continue</a></p>
</center>
</div>
 
DEBUG information: <br/>
<?php echo "Variables: $my_email <br /> $subject <br /> $message <br /> $headers <br />" ; ?>
 
 
 
</body>
</html>

Second, your ISP might consider the email spam, since the 'From' address might have no relationship to the true sender, your x10 account. If that is the case, you should make the 'From' address your x10 account and add the user's email address to the body of the message.
 

djrobuk

New Member
Messages
3
Reaction score
0
Points
0
I have added the debug information to the code and also used my x10hosting email address but to no avail. For some reason I am still unable to receive the information in my web form.

My HTML page where the web form is, is as follows:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<LINK REL="SHORTCUT ICON"
       HREF="favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ROB'S MOBILE DISCO - AVAILABLE FOR HIRE IN NORTH EAST WALES &amp; SURROUNDING AREAS.</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<style type="text/css">
<!--
.style1 {
 font-size: 18px;
 font-weight: bold;
}
-->
</style>
</head>
<body>
<div id="container" align="center">
<div id="page" class="text" align="left">
<div id="header"></div>
<div id="links" align="center"><a href="index.html" onmouseover="window.status='Return to Homepage'; return true;" onmouseout="window.status=''; return true;">Home</a> <a href="quote.html" onmouseover="window.status='Get a quote online'; return true;" onmouseout="window.status=''; return true;">Quote</a> <a href="occasions.html" onmouseover="window.status='For information on occasions catered for and extras to make the occasion special'; return true;" onmouseout="window.status=''; return true;">Occasions</a> <a href="#" onmouseover="window.status='Click to sign or view the Guest Book'; return true;" onmouseout="window.status=''; return true;">Guest Book</a> <a href="music.html" onmouseover="window.status='For information on the music genres available and some examples'; return true;" onmouseout="window.status=''; return true;">Music</a> <a href="equipment.html" onmouseover="window.status='The equipment that I will use at your event'; return true;" onmouseout="window.status=''; return true;">Equipment</a> <a href="links.html" onmouseover="window.status='A selection of useful links can be found here'; return true;" onmouseout="window.status=''; return true;">Links</a></div>
<div id="content">
  <h2>Quotation</h2>
  <h4>Please enter your details below and I will get back in touch with you as soon as possible with a no obligation quote. </h4>
  <form action="FormToEmail.php" method="post" name="contactform">  
      <label>Name:</label>
   <br />
   <input name="name" type="text" id="name" size="40" />
   <br />
   <br />
   <label>Address:<label>
   <br />
   <textarea name="" cols="40" rows="8"></textarea>
   <br />
   <label>Email Address:<label>
   <br />
   <input name="email" type="text" id="email" size="40" />
   <br />
   <label>Occasion:<label>
   <br />
   <input name="occasion" type="text" id="occasion" size="40" />
   <br />
   <label>Venue:<label>
   <br />
   <textarea name="venue" cols="40" rows="8" id="venue">Please enter address details of your chosen venue here... (If you are not yet sure of a venue then enter a rough location)</textarea>
   <br />
   <label>Start Time:<label>
   <br />
   <input name="start" type="text" id="start" value="Please enter in 24 hours (e.g. 14:00)" size="40" />
   <br />
   <label>End Time:<label>
   <br />
   <input name="end" type="text" id="end" value="Please enter in 24 hours (e.g. 14:00)" size="40" />
   <br />
   <br />
   <input name="recipient" type="hidden" id="recipient" value="EMAIL ADDRESS HERE" />
      <input type="submit" name="Submit" value="Submit" />
      <input type="reset" name="Reset" value="Reset" />
    </p>
  </form>
  <p class="style1">PLEASE DO NOT ENTER DETAILS JUST YET. WEB FORM STILL UNDER CONSTRUCTION </p>
  </div>
<div id="clearfloats"></div>
<div id="footer" align="left"><div id="footerT"><img src="images/footerT.jpg" alt="" width="349" height="256" border="0" /></div><div id="footerC"></div>
</div>
</div>
</div>
</body>
</html>
 

djrobuk

New Member
Messages
3
Reaction score
0
Points
0
After hours of looking into it. Finally sorted it out I deleted the following attribute:

HTML:
$headers = "From: " . $_REQUEST['email'];

It works fine now!
 
Top