Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    mypointofview
    Guest

    Some small email form questions

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

    Code:
    <?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.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    Nashville
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    n's

    The \n represents a "New Line". This is essentially a <br> tag.

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    \n = newline
    \r = return carriage

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

    PHP Code:
    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
    Last edited by Nightfire; 09-05-2005 at 01:27 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    $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.

    Edit:
    There's the rest of nightfires

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    LOL Yeah, hit tab and enter and it posted my message before I was ready

  • #6
    mypointofview
    Guest

    Need more details on how to do it

    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?

    PHP Code:
    "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:

    PHP Code:
    <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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Stripslashes needs to be escaped from your string evaluation, as its a core function:
    PHP Code:
    $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:
    PHP Code:
    <p><?php echo $thankyou?></p>

  • #8
    mypointofview
    Guest

    How to implement the "Stripslashes" code?

    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?

    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

    PHP Code:
    "Name: $visitorName  Tel: $visitorPhone  $visitorEmail\r\nComments:\r\n$comments"
    with this:

    PHP Code:
    $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 Code:
    <?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

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Sorry, didn't realize you were using this in a mail function. I should have looked at it earlier.
    This will work then:
    PHP Code:
                   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.

  • #10
    mypointofview
    Guest

    I got it to work

    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.

    Martin.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •