Same form thank you message?

Discussion in 'PHP' started by le007, May 13, 2009.

  1. #1
    Hi all -
    I'm a novice phper and I made a tell a friend form that sends out an email. Its working fine, but I want to refine it so that the thank you message just appears under the submit button. I've tried but it's not working and just ends up not working at all. Can someone please edit my code to make the thank you, your friend has been emailed etc come up under the submit button on the same page - friendpage.php?
    Thanks!

    'friendpage.php
    <form action="friendscript.php" method="POST">
    <fieldset>
    <legend>Tell a friend</legend><br />
    Your email<br />
    <input type="text" name="your_name" value="Your name" /><br /><br />
    Your Friends email<br />
    <input type="text" name="friend_email" value="Your friends email" />
    <input type="Submit" value="Tell a friend" name="Submit" />
    </fieldset>
    </form>
    
    
    
    
    
    'friendscript.php
    <?php
    
    
    if (isset($_POST['Submit'])) {
    // This will check to see if the form has been submitted
    $senders_name = $_POST['your_name'];
    $headers .= "From: <no-reply@Villa.com>\n";  // your email client will show the person's email address like normal
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n"; // sets the mime type
    $subject = "Your Friend, $senders_name, has recommended Villa.com"; // this is the subject of the email
    // The person who is submitting the form
    $recipient_friend = $_POST['friend_email'];
    
    // The forms recipient
    mail($recipient_friend, $subject, "Dear $recipient_friend,\n\n
    Your friend $senders_name, recommends this site.\n\n
    Please click here:\nhttp://www.Villa.com
    \n\nRegards\n\nVilla.com", $headers);
    
    if (isset($_POST['your_email'])) {
    echo "<br>Your friend $recipient_friend has been contacted <br><br>Thank you $senders_email";
    }}
    ?>
    Code (markup):
     
    le007, May 13, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just try this:
    
    <?php
    if (isset($_POST['Submit'])) {
    // This will check to see if the form has been submitted
    $senders_name = $_POST['your_name'];
    $headers .= "From: <no-reply@Villa.com>\n";  // your email client will show the person's email address like normal
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n"; // sets the mime type
    $subject = "Your Friend, $senders_name, has recommended Villa.com"; // this is the subject of the email
    // The person who is submitting the form
    $recipient_friend = $_POST['friend_email'];
    
    // The forms recipient
    mail($recipient_friend, $subject, "Dear $recipient_friend,\n\n
    Your friend $senders_name, recommends this site.\n\n
    Please click here:\nhttp://www.Villa.com
    \n\nRegards\n\nVilla.com", $headers);
    } else { ?>
    <form action="friendpage.php" method="post">
    <fieldset>
    <legend>Tell a friend</legend><br />
    Your email<br />
    <input type="text" name="your_name" value="Your name" /><br /><br />
    Your Friends email<br />
    <input type="text" name="friend_email" value="Your friends email" />
    <input type="Submit" value="Tell a friend" name="Submit" />
    </fieldset>
    </form>
    
    <?php }
     if (isset($_POST['your_email'])) { ?>
       <p>Your friend <?php echo $recipient_friend; ?> has been contacted</p>
       <p>Thank you <?php echo $senders_email"; ?></p>
    <?php }} ?>
    
    PHP:
    Not tested, but should in theory work as intended.
     
    PoPSiCLe, May 13, 2009 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Hi PoPSiCLe - thanks for the reply, nope its not working? A blank page appears? Any ideas?
     
    le007, May 14, 2009 IP
  4. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Parse error: parse error, expecting `','' or `';'' in C:\wamp\www\tellafriend.php on line 31

    Any ideas please?
     
    le007, May 14, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    There was a double quote somewhere that was wrong - try the following:
    
    <?php
    if (isset($_POST['Submit'])) {
    // This will check to see if the form has been submitted
    $senders_name = $_POST['your_name'];
    $headers .= "From: <langbakk@gmail.com>\n";  // your email client will show the person's email address like normal
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n"; // sets the mime type
    $subject = "Your Friend, $senders_name, has recommended Villa.com"; // this is the subject of the email
    // The person who is submitting the form
    $recipient_friend = $_POST['friend_email'];
    
    // The forms recipient
    mail($recipient_friend, $subject, "Dear $recipient_friend,\n\n
    Your friend $senders_name, recommends this site.\n\n
    Please click here:\nhttp://www.Villa.com
    \n\nRegards\n\nVilla.com", $headers);
    } else { ?>
    <form action="test.php" method="post">
    <fieldset>
    <legend>Tell a friend</legend><br />
    Your email<br />
    <input type="text" name="your_email" value="Your email" /><br /><br />
    Your Friends email<br />
    <input type="text" name="friend_email" value="Your friends email" />
    <input type="Submit" value="Tell a friend" name="Submit" />
    </fieldset>
    </form>
    
    <?php }
     if (isset($_POST['your_email'])) { ?>
       <p>Your friend <?php echo $recipient_friend; ?> has been contacted</p>
       <p>Thank you <?php echo $senders_email; ?></p>
    <?php } ?>
    
    PHP:
    I've fixed some errors in the code - name-values not corresponding and such. As it is now, the code will show the thank-you message in the same page, but not the form again - the form will be gone when the thank-you message is shown.

    There is lots of things that should be done with this form and processing though - error-handling (not an email-adress entered, nothing entered, etc.).
     
    PoPSiCLe, May 14, 2009 IP
  6. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    Hi PopSiCLe,

    It is working now perfectly! Thank you very much!
    If I wanted to do the same type of thing for a contact page? Name, telephone, email, comment(textarea) --> is it along the same lines?
     
    le007, May 14, 2009 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    It is definitely along the same lines, yes. Although, with something like a contact form, you would want to do some errorchecking, and maybe add some kind of captcha to avoid spam.

    I have several contact-forms I have been using on different pages, and they are between 150-250 lines in length, plus javascript for different visual references and messages. Most of it, however, is errorchecking and functions to check for valid phonenumbers and email-adresses.

    I would post one, but I don't have any available in English, and I'm a bit too tired to translate and "clean" the code right now. Hope you can figure it all out yourself - there are lots of good tutorials and coding examples online, and you have the working form to work from, so it shouldn't be too hard.
     
    PoPSiCLe, May 14, 2009 IP
  8. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #8
    Ok PoPSiCle - many thanks!
     
    le007, May 14, 2009 IP