Results 1 to 9 of 9

Thread: Please check this script for sending some emails from a form

  1. #1
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Cool Please check this script for sending some emails from a form

    This is the first bit of php that I've come up with.

    Could someone more experienced please take a look for me and let me know if it's all good. If it's not could you tell me what's wrong with it. Thanks

    Firstly the form that calls it:

    HTML Code:
    <html>
    <body>
    
    <form method='post' action='password-sender.php'>
    <p>Please give your details below and a reminder of the password will be sent to you.</p>
      Email: <input name='email' type='text' /><br />
      Name: <input name='name' type='text' /><br />
      <input type='submit' />
      </form>
    
    </body>
    </html>
    And now password-sender.php:
    PHP Code:
    <?php


    //if "email" is filled out, check for "name"
    if (isset($_REQUEST['email']))
      {
      
    //if "name" is filled out, send a couple of mails
      
    if (isset($_REQUEST['name']))
        {
            
            
    $email $_REQUEST['email'] ; 
            
    $subject $_REQUEST['subject'] ;
            
    $message "A password reminder has been sent to <"$_REQUEST['name'">" $_REQUEST['email'] ;
            
    $username $_REQUEST['name'];
            
            
    mail("webmaster@torema.com.br""Password Reminder Requested"
              
    $message"From: noreply@torema.com.br";
              
            
    mail($email"Password Reminder""Here is a reminder of your password: <br><br> 
                    Username: "
    $username"<br>
                    Password: monkey<br>
                    Please login at http://torema.com.br/cases.php"

                    
    "From: noreply@torema.com.br";
        }
      else 
      
    //if "name" is not filled out, ask them to fill it out
      
    {
        echo 
    "please give your name";
      }
    else
    //if "email" is not filled out, ask them to fill it out
    {
      echo 
    "please fill out an email address";
      }
    ?>
    Basically, it's a form that posts the password "monkey" to the user and the email and name of the user to me.

    There are a few more questions... like how to land on another page after the form has been sent and how to get the echos appearing there...

    ...but for starter I'd be happy if I could just get the PHP right.

    Thanks a lot for any help

    d o g

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    There are a few items in the php above that need to be changed in order to get it to work.

    Code:
    <?php
    
    //if "email" is filled out, check for "name"
    if (isset($_REQUEST['email']))  {
    
    //if "name" is filled out, send a couple of mails
    
      if (isset($_REQUEST['name']))  {
            
            $email = $_REQUEST['email'] ;
    So far the PHP is good, but the following line makes me wonder.

    Code:
            $subject = $_REQUEST['subject'] ;
    Where in the HTML form do you have a subject field? Let's continue to the next item that needs to be fixed.

    This:
    Code:
            $message = "A password reminder has been sent to <"$_REQUEST['name'] ">" $_REQUEST['email'] ;
    Should be like this:

    Code:
            $message = "A password reminder has been sent to <".$_REQUEST['name']."> ".$_REQUEST['email'];
    Notice the dots that seperate (spelling?) the request variable from the message that's going to be sent.

    Code:
            $username = $_REQUEST['name'];
            
            mail("webmaster@torema.com.br", "Password Reminder Requested", 
              $message, "From: noreply@torema.com.br";
    The above is good, then we come to the same item as before (the dots that seperate the variables from the text). As well as a missing bracket before the last else statement.

    Code:
            mail($email, "Password Reminder", "Here is a reminder of your password: <br><br> 
                    Username: ".$username."<br>
                    Password: monkey<br>
                    Please login at http://torema.com.br/cases.php", 
                    "From: noreply@torema.com.br";
        }
    
      else  
    //if "name" is not filled out, ask them to fill it out
      {
        echo "please give your name";
      }
    
    }
    
    else
    //if "email" is not filled out, ask them to fill it out
    {
      echo "please fill out an email address";
      }
    ?>
    Other then that, you should be alright. Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Hope this helps.
    Thanks a lot! That reply was exactly what I needed.

    The 'subject' was in a different bit of PHP I was tinkering with, lets forget that.

    I'm wondering how to jump back to the page I was on and position the echos there after the form has been sent.

    Could you suggest anything?

  4. #4
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    hmmm... i just got this error when i tested it:

    Code:
    Parse error: parse error, unexpected ';' in /home/fhlinux178/t/torema.com.br/user/htdocs/password-sender.php on line 16
    Line 16 is:
    PHP Code:
    $message"From: noreply@torema.com.br"
    a part of one of the mail functions:
    PHP Code:
    mail("webmaster@torema.com.br""Password Reminder Requested"
              
    $message"From: noreply@torema.com.br"
    Should I not break the code onto several lines. I assumed it was like Javascript and spaces and line breaks were only used for layout.

    Advise please?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    by using header redirects you could acheive (spelling?) this. If you wanted to diplay a message first, then redirect the user, place this after the mail() function.

    Code:
    header("Refresh: 2; url=index.php");
    
    echo 'This is the message I wish to display.';
    Of course change the above to your liking. The 2 after the word "Refresh" is the number of seconds to wait before continuing (in this case redirecting to a different page. For more info on header, click here.

    Enjoy!
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry, posted before seeing your new post.

    The problem with that (that I forgot to mention) was the end of the mail() function.

    mail($email, "Password Reminder", "Here is a reminder of your password: <br><br>
    Username: "$username"<br>
    Password: monkey<br>
    Please login at http://torema.com.br/cases.php",
    "From: noreply@torema.com.br";
    There needs to be an ending parenthesis (spelling?) and that should fix that.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Cool thanks! I just spotted the same thing.

    i'll make that change and then give the header redirects a try.

    Am I right in thinking the echo can only be used on the password-sender.php page. There isn't any way I can get the echo to show up on the form page.

    I managed to do this with ASP once before. You can take a look at the result by sending me a message via www.dc5b.com/portfolio/contact.asp

    Thanks again for the help here.

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Well, you could make it echo on the form page, as long as the page with the form is a php file. Otherwise, you would have to either make a new page, echo in the script, or place the test in the html form itself. Any of those solutions would work.

    Another option would be the use of Javascript. Not sure how that would work though. Good luck.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    you could make it echo on the form page, as long as the page with the form is a php file.
    That sounds like a good option but I have no idea how to do it.

    I'm going to log off and check back in the morning.

    It feels good to get my first bit of PHP functioning. Thanks again for the help.

    I have some questions about formating the strings (terminology?) the php writes (e.g. the echos and the body of the email) but I think it's best to start them in a new thread.

Bookmarks

Posting Permissions

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