...

View Full Version : Some small email form questions



mypointofview
09-05-2005, 01:41 AM
Hi all - I'm making progress with my first steps into PHP and my mailscript is sort of working. Thanks IOTA for the script which I'm using.

Now I want to personalize this code and have some questions..


<?php
$sendto="myname@mydomain.com";
$emailsubject="mysubject";
$thankyou="this is the text to be displayed when your visitors submit the form";

if ($submit_x) {

if ($visitorName <= "")
$message="Please enter your name.";
else if ($visitorPhone <= "")
$message="Please enter your phone number.";
else if ($visitorEmail <= "")
$message="Please enter your email address.";
else if ($comments <= "")
$message="Please write something in the message field.";
if ($message)
echo ($message);
else {
mail("$sendto",
"$emailsubject",
"\r\nName: $visitorName\r\nTel: $visitorPhone\r\nEmail: $visitorEmail\r\nComments:\r\n$comments",
"From: $visitorName <$visitorEmail>");
echo ("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thankyou");
die();
}
}
?>

1. What do the r and n characters mean -- they are escaped by a backslash, like \r\n ?

2. If somebody writes with an apostroph, like in "That's right", then in the mail which I get, it looks like "That\'s right" -- how to avoid this?

3. How do I position the $message inside my html at a place of my choice? For the moment these messages just display at the very top of the page.

4. In case of good delivery a thank you message appears. It makes the form go away. How can I make the thank you message appear inside the html at a position of my choice, like the other messages -- or maybe -- how can I have the thank you message appear on a completely new page?

Thanks, Martin.

PS: I changed "submit" to "submit_x" because I'm using and image as input.

macmonkey
09-05-2005, 01:48 AM
The \n represents a "New Line". This is essentially a <br> tag.

Nightfire
09-05-2005, 02:23 AM
\n = newline
\r = return carriage



<?php
$str = 'that\'s right';
echo $str .'<br>'.stripslashes($str);
?>
// Use stripslashes() to remove slashes





else {
mail("$sendto",
"$emailsubject",
"\r\nName: $visitorName\r\nTel: $visitorPhone\r\nEmail: $visitorEmail\r\nComments:\r\n$comments",
"From: $visitorName <$visitorEmail>");
//echo ("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thankyou");
//die();
}

//Wherever you want the thank you message putting, use $thankyou. eg:
<body>
<p><?php echo $thankyou?></p>
</body>
// Must be used after the mail is sent, can't use it above the mail script

Fou-Lu
09-05-2005, 02:31 AM
Nightfires seems to be cut off here.
To remove your escaped value, you need to use stripslashes() on the string in question.
As for positioning, you need to use html for that, as well your current set is not marked up. Depending on if you want html emails sent (which require different headers BTW), you will probaby need to make a new set of variables which include your current variables, but wrapped in formatted div tags. You may want to.. whats the word... concatinate or something like that, your message variables together.


$message .= 'string';

This depends on if you want to use more than one message for your variable, which also means you would need to change your if / else if statements to ifs instead.


There's the rest of nightfires :p

Nightfire
09-05-2005, 02:50 AM
LOL Yeah, hit tab and enter and it posted my message before I was ready :p

mypointofview
09-05-2005, 04:46 AM
First of all thank you. I understand the answer to my question 1 but unfortunately not the others..

The main reason why I have difficulties is that I don't know where to put the code.

a) As for to avoid the backslash if a visitor uses an apostroph in his comment, I did the following but it's crap I guess. How do I do it right?


"Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\nstripslashes($comments)",

b) As for how to position the thank you message at a point of my choice inside my html, I tried to put the following code after the html form in my html but it's also crap because it shows permanently the thank you echo:


<p><?php echo $thankyou?></p>

Has it perhaps to do WHERE in the html code the php code is placed? Currently I place the php code right above my html form.

Could you point me to my mistakes or tell me where to put what? Keep in mind that I'm unfortunately still a beginner - maybe my initial question that started this thread was not clearly written?

Thanks again, Martin.

Fou-Lu
09-05-2005, 05:52 AM
Stripslashes needs to be escaped from your string evaluation, as its a core function:


$str = "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n";
$str .= stripslashes($comments) . ",";
// OR
$str = "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n" . stripslashes($comments) . ",";

As for this one:
<p><?php echo $thankyou?></p>

You need to terminate your echo, otherwise php doesn't know where to stop:


<p><?php echo $thankyou; ?></p>

mypointofview
09-05-2005, 08:35 AM
Again a big Thank You for helping me since I a'm a beginner. I realized (after lots of frustration) that the COMPLETE php code part has to be at the location where I want my thank you messages or the other messages to be.

So I'm happy with this. However one last thing is still confusing me -- maybe one of you guys can help me with this: How to implement the "Stripslashes" code? :confused:

I don't know where to put the code that Fou-lu suggested in context with my existing php code. I replaced my initial line


"Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n$comments",

with this:


$str = "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n";
$str .= stripslashes($comments) . ",";

but it does not work (I get a parse error - unexpected semicolon...)

So can somebody simply insert the correct "Stripslashes" code into my existing code (see below) so that it can be used?

Here's what I have so far:


<?php
$sendto="myname@mydomain.com";
$emailsubject="Mail from website";
$thankyou="Thank you for your message";

if ($submit_x) {

if ($visitorName <= "")
$message="Please enter your name.";
else if ($visitorPhone <= "")
$message="Please enter your phone number.";
else if ($visitorEmail <= "")
$message="Please enter your email address.";
else if ($comments <= "")
$message="Please write something in the message field.";
if ($message)
echo ($message);
else {
mail("$sendto",
"$emailsubject",
$str = "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n";
$str .= stripslashes($comments) . ",";
"From: $visitorName <$visitorEmail>");
echo ("$thankyou");
die();
}
}
?>

Martin

Fou-Lu
09-05-2005, 11:35 AM
Sorry, didn't realize you were using this in a mail function. I should have looked at it earlier.
This will work then:


mail("$sendto",
"$emailsubject",
$str = "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments:\r\n";
$str .= stripslashes($comments) . ",";
"From: $visitorName <$visitorEmail>");
// Change this block to:
mail("$sentdto", "$emailsubject", "Name: $visitorName Tel: $visitorPhone $visitorEmail\r\nComments: \r\n" . stripslashes($comments) . "", "From: $visitorName <$visitorEmail>");

Try that out.

mypointofview
09-06-2005, 02:15 AM
Thanks Fou-Lu, I really appreciate your help. I just had to deal with one typo - you wrote $sentdto as opposed to $sendto ;)

But those things are good. That made me look and study the code.

:thumbsup: Martin.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum