I have a bit of a dilemma that I'd like to try and fix so it's much cleaner and uses less PHP files. Basically I just use a very basic email script -
[code]<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "";
$email_subject = "";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['IMEI']) ||
!isset($_POST['carrier']) ||
!isset($_POST['model']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$IMEI = $_POST['IMEI']; // required - added for iPhoneFactoryUnlocking
$carrier = $_POST['carrier']; // required - added for iPhoneFactoryUnlocking
$model = $_POST['model']; // required - added for iPhoneFactoryUnlocking
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($carrier) < 2) {
$error_message .= 'You must enter a cell phone carrier.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "The following is an order inquiry sent from iPhoneFactoryUnlocking.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Carrier: ".clean_string($carrier)."\n";
$email_message .= "iPhone Model: ".clean_string($model)."\n";
$email_message .= "Phone #: ".clean_string($telephone)."\n";
$email_message .= "IMEI #: ".clean_string($IMEI)."\n";
$email_message .= "Questions/Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_sender."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<?php include 'success.php'; ?>
<?php
}
?>[/code]
The instructions told me to use an HTML form in the following manner (the rest of it is omitted):
[code]
<form name="contactform" method="post" action="send_form_email.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name"><strong>Name</strong>: <span class="required">*</span></label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30" />
</td>
</tr>
</table>
</form>
[/code]
Then finally, a success page. The success page uses the same format/template as the other main pages of the website, nothing special.
It takes 3 files just to send an e-mail:
-send-form-email.php
-contact.php
-success.php
I'd really like to figure out a way to combine all of these files into one, allowing for more control over error messages, and just all around easier to edit. If anything, the first 2 php files could be combined and then success.php could just have a rewrite rule attached to it, so instead of success.php it would say contact.php?=success.; just a thought.
All help is greatly appreciated, I'm learning as I go along.
I'm a newbie but I think you could combine the scripts like this:
[CODE]<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "";
$email_subject = "";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['IMEI']) ||
!isset($_POST['carrier']) ||
!isset($_POST['model']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$IMEI = $_POST['IMEI']; // required - added for iPhoneFactoryUnlocking
$carrier = $_POST['carrier']; // required - added for iPhoneFactoryUnlocking
$model = $_POST['model']; // required - added for iPhoneFactoryUnlocking
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($carrier) < 2) {
$error_message .= 'You must enter a cell phone carrier.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "The following is an order inquiry sent from iPhoneFactoryUnlocking.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Carrier: ".clean_string($carrier)."\n";
$email_message .= "iPhone Model: ".clean_string($model)."\n";
$email_message .= "Phone #: ".clean_string($telephone)."\n";
$email_message .= "IMEI #: ".clean_string($IMEI)."\n";
$email_message .= "Questions/Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_sender."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<?php include 'success.php'; ?>
<?php
}
?>
<form name="contactform" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name"><strong>Name</strong>: <span class="required">*</span></label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30" />
</td>
</tr>
</table>
</form>[/CODE]
send-form-email.php and contact.php are to be put in one php file and [CODE]<form name="contactform" method="post" action="send_form_email.php">[/CODE] should be replaced with [CODE]<form name="contactform" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">[/CODE]
This is purely theoretical so I don't know if this will work or is it secure.
You can just use omit the action= to use the current file.
Thanks for the help. I ended up using this in the "action" field for the form -
[code]<?= $_SERVER['PHP_SELF'] ?>[/code]
Works great. Now I just need to properly display the success page :P
A possible long-shot; if you are looking for a single processing script then this is how I would personally approach it.
[PHP]
<?php
global $array;
$array=Array(
"error"=>Array(),
"print"=>true,
);
function check_fields(){
global $array;
$required_fields=Array(
"key",
);
foreach($required_fields as $key){
if(!isset($_POST[$key])){
$array['error']['form']="The '".$key."' is not filled.";
return false;
}
if(!strlen($_POST[$key])>0){
$array['error']['form']="'".$key."' is too short.";
return false;
}
}
return true;
}
if(isset($_REQUEST['process'])){
$array['print']=false;
if(check_fields()){
// Run processing script.
// Redirect to sucess page:
header('Location: http://www.google.com/sucess_page');
}
else{
$array['print']=true;
}
}
if($array['print']){
echo "<form method=\"post\">\n";
echo " <input type=\"hidden\" name=\"process\" value=\"1\"/>\n";
echo " <table border=\"1\" style=\"width:450px;\">\n";
if(isset($array['error']['form'])){
echo " <tr>\n";
echo " <td>Error:</td>\n";
echo " <td>".$array['error']['form']."</td>\n";
echo " </tr>\n";
}
echo " <tr>\n";
echo " <td><span>Name:</span></td>\n";
echo " <td><input type=\"text\" name=\"key\" value=\"value\"/></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td> </td>\n";
echo " <td><input type=\"submit\" value=\"post\"/></td>\n";
echo " </tr>\n";
echo " </table>\n";
echo "</form>\n";
}
[/PHP]
[QUOTE=camacazie638;38218168]A possible long-shot; if you are looking for a single processing script then this is how I would personally approach it.
[PHP]
<?php
global $array;
$array=Array(
"error"=>Array(),
"print"=>true,
);
function check_fields(){
global $array;
$required_fields=Array(
"key",
);
foreach($required_fields as $key){
if(!isset($_POST[$key])){
$array['error']['form']="The '".$key."' is not filled.";
return false;
}
if(!strlen($_POST[$key])>0){
$array['error']['form']="'".$key."' is too short.";
return false;
}
}
return true;
}
if(isset($_REQUEST['process'])){
$array['print']=false;
if(check_fields()){
// Run processing script.
// Redirect to sucess page:
header('Location: http://www.google.com/sucess_page');
}
else{
$array['print']=true;
}
}
if($array['print']){
echo "<form method=\"post\">\n";
echo " <input type=\"hidden\" name=\"process\" value=\"1\"/>\n";
echo " <table border=\"1\" style=\"width:450px;\">\n";
if(isset($array['error']['form'])){
echo " <tr>\n";
echo " <td>Error:</td>\n";
echo " <td>".$array['error']['form']."</td>\n";
echo " </tr>\n";
}
echo " <tr>\n";
echo " <td><span>Name:</span></td>\n";
echo " <td><input type=\"text\" name=\"key\" value=\"value\"/></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td>*</td>\n";
echo " <td><input type=\"submit\" value=\"post\"/></td>\n";
echo " </tr>\n";
echo " </table>\n";
echo "</form>\n";
}
[/PHP][/QUOTE]
Eww, echo'ing out HTML is not my cup of tea.
I would approach the html printing like this:
[php]
<?php
if($array['print']){
?>
<form method="post">
<input type="hidden" name="process" value="1"/>
<table border="1" style="width:450px;">
<?php
if(isset($array['error']['form'])){
?>
<tr>
<td>Error:</td>
<td><?php echo $array['error']['form']; ?></td>
</tr>
<?php
}
?>
<tr>
<td><span>Name:</span></td>
<td><input type="text" name="key" value="value"/></td>
</tr>
<tr>
<td>*</td>
<td><input type="submit" value="post"/></td>
</tr>
</table>
</form>
<?php
}
?>[/php]
[QUOTE=CarLuver69;38021090]Thanks for the help. I ended up using this in the "action" field for the form -
[code]<?= $_SERVER['PHP_SELF'] ?>[/code]
Works great. Now I just need to properly display the success page :P[/QUOTE]
Be aware that using the above method to print is not supported by all web-hosts.
[QUOTE=P1raten;38223149]Eww, echo'ing out HTML is not my cup of tea.[/QUOTE]
The approach is directed towards the nesting within the script, not how the html is outputted.
[php]<?php
if($condition) { ?>
<html>
etc.
</html>
<?php
} else { ?>
<html>
etc.
</html>
<?php } ?>
[/php]
That's how you'd nest it without all of those echos.
Sorry, you need to Log In to post a reply to this thread.