SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Return after mail function call

    Good day,

    I am a very novel programmer, just making my first website for a new idea we are starting.

    From "contact us" page, we have a form to get information, and when press send button, send_email.php function is called.
    All works fine, I receive the e-mail generated by the function.

    My question is, how can i return to my website from the PHP mail() function?

    In PHP code I added a line:

    echo "<div style='position:absolute; top:215px; left:200px; color:#FF4F10'><br><br><h2>Your request has been successfully sent</h2></div>";

    But the message is showed in a blank page.
    I have created includes in my HTML website, in order to avoid repeating code. I have includes for header, menu and footer sections.

    Id like the message generated after mail() function sent the e-mail is showed in a web page with these three includes.

    Thanks a lot!!!

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Can you post the code of send_email.php? (preferably between [php ] tags)

    That way we can see what's happening in your script.

  3. #3
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Of course:

    <?

    $cuerpo = "Formulario enviado\n";
    $cuerpo .= "Nombre: " . $_POST['name'] . "\n";
    $cuerpo .= "E-mail" . $_POST['email'] . "\n";
    $cuerpo .= "Asunto: " . $_POST['subject'] . "\n";
    $cuerpo .= "Comentario: " . $_POST['comment'] . "\n";


    //mando el correo...
    mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $cuerpo);

    //doy las gracias por el envío
    echo "<div style='position:absolute; top:215px; left:200px; color:#FF4F10'><br><br><h2>Su Solicitud ha sido Enviada Satisfactoriamente</h2></div>";
    ?>

    and it is called from:
    http://www.libromania.cl/spanish/con..._spanish.shtml
    when press "Enviar" button.

    Thanks!!!

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    There's a couple of things you could do:

    1) have send_mail.php show an entire page (so also include your header and footer includes)
    2) redirect to a different page with the 'Su Solicitud ha sido Enviada Satisfactoriamente' message

    The second options is probably the best, as it has the added benefit of not sending a new mail out again if someone presses F5

    PHP Code:
    $cuerpo "Formulario enviado\n";
    $cuerpo .= "Nombre: " $_POST['name'] . "\n";
    $cuerpo .= "E-mail" $_POST['email'] . "\n";
    $cuerpo .= "Asunto: " $_POST['subject'] . "\n";
    $cuerpo .= "Comentario: " $_POST['comment'] . "\n";


    //mando el correo...
    mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía"$cuerpo);

    header('Location: http://www.libromania.cl/spanish/gracias.html');
    die(); 
    And then you create a new page gracias.html which contains the 'Su Solicitud ha sido Enviada Satisfactoriamente' message.

  5. #5
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great!!! I will check and post results!!!

  6. #6
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Immerse,

    Thanks a lot for your answer!!
    I applied your instructions and now it is working properly.
    You can see here directly:
    http://www.libromania.cl/spanish/answer_spanish.shtml

    Or after send me a message in:
    http://www.libromania.cl/spanish/con..._spanish.shtml

    Thanks!!!

  7. #7
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Immerse,

    Now the next question. I hope you can help me again...

    Today, when press "Enviar" button, a message is generated, no matterthe information in the form. How man make the four fields mandatory? It is, not sending the e-mail if all four fields are not filled.

    Here the script again:

    <?
    $message_body = "Formulario enviado\n";
    $message_body .= "Nombre: " . $_POST['name'] . "\n";
    $message_body .= "E-mail: " . $_POST['email'] . "\n";
    $message_body .= "Asunto: " . $_POST['subject'] . "\n";
    $message_body .= "Comentario: " . $_POST['comment'] . "\n";


    mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $message_body);

    header('Location: http://www.libromania.cl/spanish/ans...h.shtml&#39;);
    die();

    ?>


    Thanks!!!!

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You can check the contents of the variables like this:

    PHP Code:
    if(empty($_POST['name'])) {
        
    $error 'Please enter your name.';

    This uses the function empty()to test if the variable $_POST['name'] is, um, empty.

    Now you need to figure out how to display the errors that people might get

  9. #9
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great, thanks. How can I give some format to that error message? I mean position, font size, color ...

    Thanks!!!!!

  10. #10
    SitePoint Enthusiast derokorian's Avatar
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wrap the error in a span tag, and style that ie:
    PHP Code:
    $error '<span style="font: red Times 12pt;font-weight: bold;">Please enter your name.</span>'

  11. #11
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    wow, great move. Is it possible to put these styles in a CSS file? (considering this is a PHP script and not an HTML file?)

    Thanks!!!!

  12. #12
    SitePoint Enthusiast derokorian's Avatar
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure is!
    style.css
    Code:
    .error {
       font: red Times 12pt;
       font-weight: bold;
    }
    php file:
    PHP Code:
    $error '<span class="error">Please enter your name.</span>'
    You'll just have to remember to include the style.css even when displaying errors. Usually done with custom error handling instead of using die($error); for ex.

  13. #13
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Super!!! I'll check and post results!!!!

  14. #14
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Im afraid I am not being able to make it works.
    When press "send" button, it jumps to the page where thanks for the successful message. There is no error message.
    You can see it in:
    http://www.libromania.cl/spanish/con..._spanish.shtml

    The PHP function is:

    <?
    $message_body = "Formulario enviado\n";
    $message_body .= "Nombre: " . $_POST['name'] . "\n";
    if(empty($_POST['name'])){
    $error = '<span class="error">Please enter your name.</span>';
    }
    $message_body .= "E-mail: " . $_POST['email'] . "\n";
    $message_body .= "Asunto: " . $_POST['subject'] . "\n";
    $message_body .= "Comentario: " . $_POST['comment'] . "\n";



    mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $message_body);


    header('Location: http://www.libromania.cl/spanish/ans...h.shtml&#39;);
    die();

    ?>

    And in styles.css file I added:

    .error{
    font-size: 150%;
    color: red;
    font-weight: bold;
    }


    Thanks!!!!!


Tags for this 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
  •