PHP noob, how to I display/run multiple varialbes in 1 line?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
f22raptorX1
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 8:36 pm

PHP noob, how to I display/run multiple varialbes in 1 line?

Post by f22raptorX1 »

Hi all,

Need some help here just started dabbling in PHP. I have a contact form.html sending to a mailer.php (which sends a MIME html email)
Now I have (which I presume stores my form data into variables starting with $

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$message = $_POST['message'];

My problem is that I would now like to display the values of these variables using PHP, but I am really struggling how to work it out! Do i use print, $_POST?. I see \r means line return if someone could show me how to write this out would be much apprecaited.

I'm trying to get something like this:

Name: (displays my $name_field variable)
Email: (displays my $email_field variable)
Phone: (displays my $phone_field variable)
Message: (displays my $message variable)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by requinix »

Display in the email? On the screen?

If you want it in the email it's simple, but easier to explain if you post what code you have now.
For the screen, echo and print are like functions except you don't use parentheses. They print stuff to the browser.

Might I suggest you try using the online manual? And, of course, Google.
f22raptorX1
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 8:36 pm

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by f22raptorX1 »

tasairis heres my full code. Yes I am trying to get the values of my variables to show in the generated email. All the examples I've looked at only show one variable and dont show how I can include another variable on the same line.

<?php
if(isset($_POST['submit'])) {

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$message = $_POST['message'];

$to="<info@design.com>";
$from=$_POST['email'];
$subject="Enquiry Form";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"\r\n";
$headers.= "From: $from\r\n";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .

"<font face=arial size=2 color=#666666>" .
<where I'm trying to get my variables to display>
"</font>";
if (mail($to, $subject, $message, $headers))
header('Location: /form_complete.html');
else
echo "Failed to send message, please click back.";

} else {

echo "blarg!";

}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by requinix »

Watch the highlighting:

Code: Select all

"You said $phone_field was your phone number."
 
"You said " . $phone_field . " was your phone number."
The first example shows how you can put variables directly into strings. The second shows concatenation: taking a string and adding the variable to it.
Same effect but two different ways of doing it.
You can pick either one, it doesn't matter.

More information about strings and variables.
f22raptorX1
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 8:36 pm

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by f22raptorX1 »

well that was just too easy tasairis, I can see myself liking PHP once I get into it if it keeps up like that!

From the script do you have any idea why when I go to change my plain text line:

"HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" .

I was going to put the same variables into here as into my HTML part of the message eg put in:

"<br>Name: $name_field <br> Email: $email_field <br> Phone: $phone_field <br> Enquiry: $message" . (my html part of the message has additional html elemetns, font tags etc)

for whatever reason it breaks the MIME HTML format and display both the plain text and HTML messages but in plain text format.

Any ideas???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by requinix »

Code: Select all

"HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" .
That's the line you're changing, right?

There are two things you need to know.
First, the \n at the end is important. It needs to be there. The reason is that --{$mime_boundary} on the next line: that part has to be immediately after a \n. Without it the client won't interpret the email correctly.

Second, that part is the text portion of the HTML. Like it says. That's what email readers should show if they can't handle HTML, so it should be regular text without any HTML.

You should be modifying that and the

Code: Select all

"<font face=arial size=2 color=#666666>" .
/* <where I'm trying to get my variables to display> */
"</font>";
so the text part and the HTML part will show the same info.

The end result could look like

Code: Select all

$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"Name: $name_field\nEmail: $email_field\nPhone: $phone_field\nEnquiry: $message\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
 
"<font face=arial size=2 color=#666666>" .
"<br>Name: $name_field <br> Email: $email_field <br> Phone: $phone_field <br> Enquiry: $message" .
"</font>";
Notice how there wasn't a \n after the edit in the HTML part: that's because there was no --{$mime_boundary} following it so it wasn't necessary.
f22raptorX1
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 8:36 pm

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by f22raptorX1 »

thanks for the help tasairis you were born a true legend.

Any other useful resources or websites you might know of for a PHP new comer apart from the original PHP manual? I went out and bought learning PHP5 by O'Reilly.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP noob, how to I display/run multiple varialbes in 1 line?

Post by requinix »

f22raptorX1 wrote:Any other useful resources or websites you might know of for a PHP new comer apart from the original PHP manual? I went out and bought learning PHP5 by O'Reilly.
I like O'Reilly, I'd suggest them.

Google and forums are always good, tutorials and "learn PHP" websites are hit-or-miss (not all are good, not all may be good for you)... I'd say to search around until you find a place that you like. Find two or three and learn from all of them, comparing how one place does something versus another.

I'm afraid I can't give you any advice from personal experience, textbooks and tutorials don't work for me.
Post Reply