logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 08-17-2011, 12:49 PM   #1
happydunk
 
Join Date: Aug 2011
Posts: 17
Default Cant quite get my PHP correct

Hi All,

Can you have a look at my code, I have never written PHP before so this has been picked up by reading different sources. It doesn't quite work, I think at the very end were I send to my address something is wrong but I am not sure what. If I put all the code on one line it says to many parameters. Also I am not always using text boxes does this make a difference to the code. Thank you for any help

My form is at www.http://africanoverlanders.com/InfoPage.html

I know this isn't quite right either as when I place cursor in box it moves to another, any help greatly received.

My code is


<?
// First we set up some parameters for the receiver
$receiver_name = 'Duncan Johnson';
$receiver_email = 'africanoverlanders@live.com';
$mydomain = 'www.africanoverlanders.com';


// This is for the sender's name
if (! $_POST["Name"]){
// The sender didn't give us their name
$send_from = 'Anonymous';
}else{
$send_from = $_POST["Name"];
}

// This is for the sender's email
if ($_POST["Email"] != ''){
$send_from_email = $_POST["Email"];
}else{
// The sender didn't give us their email address
$send_from_email = 'noreply@' . $mydomain;
}

// This is for the message title or subject
if (! $_POST["Import_Export"]){
// The sender didn't provide a title
$send_import_export = 'Email from ' . $mydomain;
}else{
$send_import_export = $_POST["Import_Export"];
}

// This is for the message body
if (! $_POST["How_to_send"]){
// The sender didn't put anything in the body
$send_how_to_send = 'NO_BODY';
}else{
$send_how_to_send = $_POST["How_to_send"];
}

// This is for the message body
if (! $_POST["Deliver_to_port"]){
// The sender didn't put anything in the body
$send_deliver_to_port = 'NO_BODY';
}else{
$send_deliver_to_port = $_POST["Deliver_to_port"];
}

// This is for the message body
if (! $_POST["Share_container"]){
// The sender didn't put anything in the body
$send_share_container = 'NO_BODY';
}else{
$send_share_container = $_POST["Share_container"];
}

// This is for the message body
if (! $_POST["Departure_Port"]){
// The sender didn't put anything in the body
$send_departure_port = 'NO_BODY';
}else{
$send_departure_port = $_POST["Departure_Port"];
}

// This is for the message body
if (! $_POST["Depature_port_other"]){
// The sender didn't put anything in the body
$send_departure_port_other = 'NO_BODY';
}else{
$send_departure_port_other = $_POST["Depature_port_other"];
}

// This is for the message body
if (! $_POST["Import_port"]){
// The sender didn't put anything in the body
$send_import_port = 'NO_BODY';
}else{
$send_import_port = $_POST["Import_port"];
}

// This is for the message body
if (! $_POST["Import_Port_Other"]){
// The sender didn't put anything in the body
$send_import_port_other = 'NO_BODY';
}else{
$send_import_port_other = $_POST["Import_Port_Other"];
}

// This is for the message body
if (! $_POST["Length"]){
// The sender didn't put anything in the body
$send_length = 'NO_BODY';
}else{
$send_length = $_POST["Length"];
}

// This is for the message body
if (! $_POST["Width"]){
// The sender didn't put anything in the body
$send_width = 'NO_BODY';
}else{
$send_width = $_POST["Width"];
}

// This is for the message body
if (! $_POST["Height"]){
// The sender didn't put anything in the body
$send_height = 'NO_BODY';
}else{
$send_height = $_POST["Height"];
}

// This is for the message body
if (! $_POST["Value"]){
// The sender didn't put anything in the body
$send_value = 'NO_BODY';
}else{
$send_value = $_POST["Value"];
}

// This is for the message body
if (! $_POST["Carnet"]){
// The sender didn't put anything in the body
$send_carnet = 'NO_BODY';
}else{
$send_carnet = $_POST["Carnet"];
}

// This is for the message body
if (! $_POST["Insurance"]){
// The sender didn't put anything in the body
$send_insurance = 'NO_BODY';
}else{
$send_insurance = $_POST["Insurance"];
}

// This is for the message body
if (! $_POST["Value"]){
// The sender didn't put anything in the body
$send_value = 'NO_BODY';
}else{
$send_value = $_POST["Value"];
}
// This is for the message body
if (! $_POST["Comments"]){
// The sender didn't put anything in the body
$send_comments = 'NO_BODY';
}else{
$send_comments = $_POST["Comments"];
}
// This allows us to set the from header, the reply to header, and the x-mailer
$addl_Headers = "From: " . $send_from_email . "\r\n" . "Reply-To: " . $send_from_email . "\r\n" . "X-Mailer: PHP/" . phpversion();

// Now we need to send the message
$deliver = mail($receiver_email, $send_import_export, $send_from, $send_from_email, , $send_how_to_send);
$deliver = mail($send_deliver_to_port, $send_share_container, $send_departure_port, $send_departure_port_other, $send_import_port);
$deliver = mail( $send_import_port_other, $send_length, $send_width,$send_height, $send_carnet);
$deliver = mail($send_insurance, $send_value, $send_comments, $addl_Headers);

// the PHP mail() function return true if the message was processed so we check for this below.
if ($deliver == TRUE){
echo "<p>Your message has been sent. Thank you for your comments.</p>";
}else{
echo "<p>We had a problem sending your request. Please e-mail your thoughts to us at <?=$africanoverlanders@live.com?>.</p>";
}
?>
happydunk is offline   Reply With Quote
Old 08-18-2011, 04:13 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

few things

Code:
// This is for the sender's email
if ($_POST["Email"] != ''){
$send_from_email = $_POST["Email"];
}else{
// The sender didn't give us their email address
$send_from_email = 'noreply@' . $mydomain;
}
wont work like this, its better just to sen via mail() trying to use the visitors own mail address would need a mailto approach , and dont see any benefit there

Code:
// Now we need to send the message
$deliver = mail($receiver_email, $send_import_export, $send_from, $send_from_email, , $send_how_to_send);
$deliver = mail($send_deliver_to_port, $send_share_container, $send_departure_port, $send_departure_port_other, $send_import_port);
$deliver = mail( $send_import_port_other, $send_length, $send_width,$send_height, $send_carnet);
$deliver = mail($send_insurance, $send_value, $send_comments, $addl_Headers);
the parameters should be

for example

Code:
 mail($email_to, $email_subject, $email_message, $headers)

being mail address, subject,message ,headers 

you can name them differently, but not as you have done
Code:
 better to use <?php  not <?
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-18-2011, 09:05 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

ok heres a generic form which should suit you , i added a NO Body for empty posts as you had, i also added a validation based on a form field being called Email. this might be useful for others


Code:
<?php

// Edit these values

$email = "your email.com";
$reply = "your email.com";
$from = "your email.com";
$subject ="mail form";


// This allows us to set the from header, the reply to header, and the x-mailer
 $headers="Content-Type: text/html; charset=windows-1252\n";
$headers.= "From: " . $from . "\r\n" ;
$headers."Reply-To: " ;
$headers.$reply . "\r\n" ;
$headers. "X-Mailer: PHP/" . phpversion();

// validate email form field $_POST['Email']
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $_POST['Email'])) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} 




// Now we need to send the message


$text = "Results from form:\n\n";

foreach ($_POST as $key => $value)
{
$line= "<br>";
$space = " ";

if ($value == '')
{$value="No body"."\n";
}
else {

$conc = "$key.$space.$value.$line";
}
$conc = "$key.$space.$value.$line";
$text .= $conc;


 
 }

 
if (mail($email , $subject, $text, $headers)) {
  echo "<h4>Thank you for sending email</h4>";}
 else {
  echo "<h4>Can't send email</h4>";
} 

  echo  "Your Message: ".$line. $text;



?>
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-18-2011, 05:32 PM   #4
happydunk
 
Join Date: Aug 2011
Posts: 17
Default Thank you

Wow that was easy, brilliant thank you.

I have one more question is it possible to send the email from the email address they enter? On the other form it was done I believe by the $addl_Headers, if not no worries I will copy and paste, I am just being extremely lazy.

Thank you again for your help
happydunk is offline   Reply With Quote
Old 08-19-2011, 06:19 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

no the function is by mail() that is on your domain. to use there email you would need mailto. and thats not good frankly.
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-22-2011, 10:26 AM   #6
happydunk
 
Join Date: Aug 2011
Posts: 17
Default

Thank you edbr, no worries and great advice

Thanks
happydunk is offline   Reply With Quote
Old 09-05-2011, 04:22 PM   #7
happydunk
 
Join Date: Aug 2011
Posts: 17
Default

Hi Edbr

One last question I have it all uploaded and seems to be running nearly correctly, when i put in an email address its perfect however when I don't enter an email I get back this

Site is http://africanoverlanders.com/InfoPage.html

Invalid email address

Back

Thank you for sending email

Is there a way to get rid of the Thank you for sending mail part, this is used in the positive reponse so if I remove, I remove from both.

Thank you again
happydunk is offline   Reply With Quote
Old 09-06-2011, 02:49 AM   #8
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

ok missed that option try this

Code:
   <?php

// Edit these values

$email = "your email.com";
$reply = "your email.com";
$from = "your email.com";
$subject ="mail form";


// This allows us to set the from header, the reply to header, and the x-mailer
 $headers="Content-Type: text/html; charset=windows-1252\n";
$headers.= "From: " . $from . "\r\n" ;
$headers."Reply-To: " ;
$headers.$reply . "\r\n" ;
$headers. "X-Mailer: PHP/" . phpversion();


 if(empty($_POST['Email'])) {
      die("you did not enter an email address");
   }


// validate email form field $_POST['Email']
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $_POST['Email'])) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} 




// Now we need to send the message


$text = "Results from form:\n\n";

foreach ($_POST as $key => $value)
{
$line= "<br>";
$space = " ";

if ($value == '')
{$value="No body"."\n";
}
else {

$conc = "$key.$space.$value.$line";
}
$conc = "$key.$space.$value.$line";
$text .= $conc;


 
 }

 
if (mail($email , $subject, $text, $headers)) {
  echo "<h4>Thank you for sending email</h4>";}
 else {
  echo "<h4>Can't send email</h4>";
} 

  echo  "Your Message: ".$line. $text;



?>
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 09-06-2011, 03:02 PM   #9
happydunk
 
Join Date: Aug 2011
Posts: 17
Default

Thank you EDBR

Works a treat, Going live so hopefully it will be put to use.

Thanks again

Duncan
happydunk is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:50 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com