I’m learning WordPress and PHP and building a website at www.sunriselodging.com
When trying to enter data in a form, I get a PHP error warning saying ‘header may not contain more than a single header’ on line 6. I’ve tried solutions like checking the URL which are not related to PHP. I read a few solutions here that are PHP dependent and hence I cannot figure out what needs to be done here.
My code is below:
<?php
$reservation_message = $_POST['reservation_message'];
// Redirect back
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
echo "<meta charset='utf-8'>";
// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];
$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];
$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];
if($reservation_email_switch == 'on'){
// EMAIL TO CLIENT
// SET info to email
// From in format NAME <email>
$from = $blog_name.'<'.$blog_email.'>';
// Reply to in format NAME <email>
$reply = $blog_name.'<'.$blog_email.'>';
// Subject
$subject = $reservation_email_subject;
// Message
$message = $reservation_email;
//
$to = $email;
$headers = 'From: '. $from . "rn" .
'Reply-To: '. $reply . "rn" .
'X-Mailer: PHP/' . phpversion();
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO CLIENT
// ---------------------
}
// EMAIL TO RESERVATION CREW
$message = $_POST['reservation-message'];
$headers = 'From: '. $name . '<' . $email . '>' . "rn" .
'Reply-To: '. $name . '<' . $email . '>' . "rn" .
'X-Mailer: PHP/' . phpversion();
$subject = 'Reservation for room #' . $room;
$message = 'Name: ' . $name . '
Room: #' . $room . '
Check in: ' . $checkin . '
Check out: ' . $checkout . '
Number of people: ' . $people . '
Email: ' . $email . '
Phone: ' . $phone . '
' . $message;
$to = $blog_email;
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO RESERVATION CREW
// ---------------------
exit();
?>
The URL for the form is here – http://www.sunriselodging.com/reservation/. Apparently the form hasn’t been sending emails either. Can someone please look at this and advise what’s wrong with the code here?
I’ll also appreciate if someone can direct me towards a good online PHP course for beginners (great if free).
Thank You,
Aadi
The whole problem with this here snippit is that you have 1) no conditions 2) no
mail()
function.If you are going to use a header, you should probably have a condition. You should also probably have a condition to execute the emailer. Finally, you need to run this code before everything on the page (likely before the
get_header()
if on the page). Your workflow should go something similar to this:I was able to solve this after some research on Stack Overflow.
I added urlencode to the last string on the line containing header. I made:
to look like this:
To enable the mail function to send email I changed line 54 from
to this
& line 70 from
to
Hope this will help anyone who’s stuck in a situation similar to mine. A big THANK YOU to all the people who helped.