Jump to content

Php Script Not Working: Blank Page Result


GPSJane

Recommended Posts

I have just started using php - literally right now.I got a php script for sending mail from my ISP. But unfortunatley it just displays a blank page after I submit the form which calls it. I think that I have set my variable correctly but maybe I am missing something. It could even be a basic syntax error. Maybe someone could help me?

<?php// To email address$email = "rsvp@domain.com";$email_name = "ToName";// From email address$from = "website@domain.com";$from_name = "From Name";// The message$subject = "RSVP";$message = "".$_POST["name"] has submitted an RSVP online."Attending: "".$_POST["coming"]";$message_html = "<HTML><BODY><P>Someone<br /><b>First Name</b>: ".$_POST["name"]."<br /><b>submitted an RSVP online."".$_POST["number"]".$_POST["coming"]".$_POST["message"]</P></BODY></HTML>";/***********************************************//* No need to modify anything down here *//* Note that these are needed to send the mail *//***********************************************/// Generate text + html version$random_hash = md5(date("r", time()));$mailmessage = "--PHP-alt-".$random_hash."Content-Type: text/plain; charset=\"iso-8859-1\"Content-Transfer-Encoding: 7bit$message--PHP-alt-".$random_hash."Content-Type: text/html; charset=\"iso-8859-1\"Content-Transfer-Encoding: 7bit$message_html--PHP-alt-".$random_hash."--";// Headers// To send HTML mail, the Content-type header must be set$headers .= "From: ".$from_name." <".$from.">" . "\r\n";$headers .= "Reply-To: ".$from_name." <".$from.">" . "\r\n";$headers .= "Date: ".date("r") . "\r\n";// Additional headers$headers .= "MIME-Version: 1.0" . "\r\n";$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n";$headers .= "Message-Id: <" . md5(uniqid(microtime())) . "@" . $_SERVER["SERVER_NAME"] . ">\r\n";// Send the mailmail($email, $subject, $mailmessage, $headers);echo "Thank you. Your response has been received.";?>

Link to comment
Share on other sites

I think it might be in the part where its creating the header for the message, where it creates a <body> etc and adds in all the variables. I think it may be missing some closing quotation marks. Specifically closing quotation marks around number, coming and message.

Link to comment
Share on other sites

You have some majorly misplaced quotation marks, and they cause a parsing error. Parsing errors are not caught when error reporting is switched on in the same script, because the script is invalidated on the first pass. That is, error_reporting() never executes, because nothing executes when the interpreter detects a parse error.You could stare at the quotation marks and dots long enough to straighten them out.I personally go nuts when I stare at a lot of quotes with lots of dots and the text continues on multiple lines . . . ecch.A simpler way is to use heredoc syntax.You can read about that here. (For those who care, I did a simple Google and found this one quite randomly, but it looks okay, so here it is.) The second example at this link illustrates how to combine variables with heredoc.To clarify. You are not using heredoc syntax currently. But if you changed things so that you were, you could save yourself some confusion, because it allows you to eliminate external quotation marks. Some developers find it convenient, when using heredoc, to assign array-element values to scalar variables because that also simplifies the task. I mean doing this before the heredoc:$thing = $_POST['thing'];And then embedding $thing in the heredoc.

Link to comment
Share on other sites

I think it might be in the part where its creating the header for the message, where it creates a <body> etc and adds in all the variables. I think it may be missing some closing quotation marks. Specifically closing quotation marks around number, coming and message.
Ok, actually I am confused about using POST arrays. How exactly should it look? It's the "s that are getting to me!
Link to comment
Share on other sites

You have some majorly misplaced quotation marks, and they cause a parsing error. Parsing errors are not caught when error reporting is switched on in the same script, because the script is invalidated on the first pass. That is, error_reporting() never executes, because nothing executes when the interpreter detects a parse error.You could stare at the quotation marks and dots long enough to straighten them out.I personally go nuts when I stare at a lot of quotes with lots of dots and the text continues on multiple lines . . . ecch.A simpler way is to use heredoc syntax.You can read about that here. (For those who care, I did a simple Google and found this one quite randomly, but it looks okay, so here it is.) The second example at this link illustrates how to combine variables with heredoc.To clarify. You are not using heredoc syntax currently. But if you changed things so that you were, you could save yourself some confusion, because it allows you to eliminate external quotation marks. Some developers find it convenient, when using heredoc, to assign array-element values to scalar variables because that also simplifies the task. I mean doing this before the heredoc:$thing = $_POST['thing'];And then embedding $thing in the heredoc.
Ok so not using heredoc would mean I must use quotation marks around every line? I couldn't find a resource describing how to make a variable equal to many lines like that.
Link to comment
Share on other sites

Ok, actually I am confused about using POST arrays. How exactly should it look? It's the "s that are getting to me!
".$_POST["number"]".$_POST["coming"]".$_POST["message"]

Yeah, in this section though you're just making a giant string based of text, php variables, and HTML tags that will be outputted in the email (stored in the message part of the mailto() functions call. PHP is making the string and pulling variables out of the post array and then the email client will (theoretically) parse the HTML tags as HTML and thus format the text in a more readable format within the email client, as opposed to one giant run on sentence. Basically, close the opening quotations. And you may have more, unless DD was only pointing out these ones.

Link to comment
Share on other sites

".$_POST["number"]".$_POST["coming"]".$_POST["message"]

Yeah, in this section though you're just making a giant string based of text, php variables, and HTML tags that will be outputted in the email (stored in the message part of the mailto() functions call. PHP is making the string and pulling variables out of the post array and then the email client will (theoretically) parse the HTML tags as HTML and thus format the text in a more readable format within the email client, as opposed to one giant run on sentence. Basically, close the opening quotations. And you may have more, unless DD was only pointing out these ones.

Ok so someone help me with php syntax here. When a string goes over many lines I need to enclose each line in quotation marks?Eg:$somestring = "Text "".$_POST["string"]"" more text.";
Link to comment
Share on other sites

Ok, someone please help me here because I am no closer to fixing this thing.First of all since my ISP made this available I assumed it should be correct. Surely they tested it? No matter what I do I get a blank page. Even with the error handling code. Surely if there's an error I should see it?Finally, I am confused about the variables from POST. In the script there is a double quotation and full stop before the function but in the tutorial here on W3schools there is none. Ie. ".$_POST["name"]as opposed to$_POST["name"]Ok so my fixed (I hope) code is as follows with strings simplified:

<?phperror_reporting(E_ALL);ini_set('display_errors', 1);// To email address$email = "rsvp@domain.co.za";$email_name = "To Name";// From email address$from = "website@domain.co.za";$from_name = "From Name";// The message$subject = "RSVP Wedding";$message = ".$_POST["name"] has submitted an RSVP online.";$message_html = "<HTML><BODY><P>First Name</b>: .$_POST["name"]</P></BODY></HTML>";/***********************************************//* No need to modify anything down here *//* Note that these are needed to send the mail *//***********************************************/// Generate text + html version$random_hash = md5(date("r", time()));$mailmessage = "--PHP-alt-".$random_hash."Content-Type: text/plain; charset=\"iso-8859-1\"Content-Transfer-Encoding: 7bit$message--PHP-alt-".$random_hash."Content-Type: text/html; charset=\"iso-8859-1\"Content-Transfer-Encoding: 7bit$message_html--PHP-alt-".$random_hash."--";// Headers// To send HTML mail, the Content-type header must be set$headers .= "From: ".$from_name." <".$from.">" . "\r\n";$headers .= "Reply-To: ".$from_name." <".$from.">" . "\r\n";$headers .= "Date: ".date("r") . "\r\n";// Additional headers$headers .= "MIME-Version: 1.0" . "\r\n";$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n";$headers .= "Message-Id: <" . md5(uniqid(microtime())) . "@" . $_SERVER["SERVER_NAME"] . ">\r\n";// Send the mailmail($email, $subject, $mailmessage, $headers);echo "Thank you";?>

Link to comment
Share on other sites

$message = ".$_POST["name"] has submitted an RSVP online.";Should be:$message = $_POST["name"]." has submitted an RSVP online.";When accessing associative arrays, they have to be out of the string. If you want it with a string, you have to concatenate it using period. Another way to leave it inside the string, is by setting the array value into a variable first.$name = $_POST['name'];$message = "$name has submitted an RSVP online.";

Link to comment
Share on other sites

$message = ".$_POST["name"] has submitted an RSVP online.";Should be:$message = $_POST["name"]." has submitted an RSVP online.";When accessing associative arrays, they have to be out of the string. If you want it with a string, you have to concatenate it using period. Another way to leave it inside the string, is by setting the array value into a variable first.$name = $_POST['name'];$message = "$name has submitted an RSVP online.";
Ah hah! Thank you I understand :)
Link to comment
Share on other sites

To clarify. The concatenation operator (the dot) can join two strings. That means it cannot be a part of a string. And to make this very obvious in your code, I STRONGLY recommend putting spaces around it, like so:

$name = "John";$msg = "Hello, " . $name;

It is possible to interpolate array values in double-quoted strings if they are delimited by braces:

$msg = "<p>{$_POST['name']}</p>";

I only do this sometimes, though, since it's clunky to read.And I rarely use values directly from a $_POST array, since they need to be cleaned up and validated and what-not, so their values end up assigned to scalar variables most of the time anyway.

Link to comment
Share on other sites

It helps me if I just keep variables out of strings all together and just use the concatenation operator (dot) to combine variables and strings:

$name = 'jkloth';echo "My name is " . $jkloth;

I'm used to thinking of strings as just strings so I get confused if there are variable names inside strings.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...