PDA

View Full Version : Cant quite get my PHP correct


happydunk
08-17-2011, 11:49 AM
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>";
}
?>

edbr
08-18-2011, 03:13 AM
few things

// 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

// 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

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

better to use <?php not <?

edbr
08-18-2011, 08:05 AM
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


<?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;



?>

happydunk
08-18-2011, 04:32 PM
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

edbr
08-19-2011, 05:19 AM
no the function is by mail() that is on your domain. to use there email you would need mailto. and thats not good frankly.

happydunk
08-22-2011, 09:26 AM
Thank you edbr, no worries and great advice

Thanks

happydunk
09-05-2011, 03:22 PM
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

edbr
09-06-2011, 01:49 AM
ok missed that option try this

<?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;



?>

happydunk
09-06-2011, 02:02 PM
Thank you EDBR

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

Thanks again

Duncan