SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mail and alert in PHP

    Hi,guys! I have a PHP script to send mail and prompt out alert if successfully sent.However,the alert is not displayed and all the form value become blank.How can I display alert without having a blank page?

    Secondly,the mailing script seems having some flaws,if form submitted,the previous sent form will be sent once again into the mailbox.What went wrong actualy?Thanks for you guys help!

    PHP Code:

    <?php
       
      $email 
    $_GET[email];
      
    $mailto "yourmail@mail.com";
      
    $mailsubj "Enquiry sent by visitor";
      
    $mailhead "From: $email";

      
    reset ($_GET);

      
    $mailbody "You have an enquiry from website:\n";

      while (list (
    $key$val) = each ($_GET)) {
            if(
    $key != 'submit'
            
    $mailbody .= "$key : $val\n"; }
          
      
    ini_set("SMTP",mail.yourmailserver.com.my);
      
    ini_set("smtp_port",25);
      
    ini_set("sendmail_from",$email);  
      
      
    $javascript "<script type=\"text/javascript\">";
      
      if (
    mail($mailto,$mailsubj,$mailbody,$mailhead)) {
         
    $javascript .= 'alert(\'Thank you for your enquiry, we shall contact you soon.\')';
    } else {
         
    $javascript .= 'alert(\'Email cannot be delivered.\')';
    }  
         
    $javascript .= '</script>';
       
    ?>
    Code:
    <FORM NAME="enquiry" ACTION="<?php echo htmlentities($HTTP_SERVER_VARS['PHP_SELF']); ?>" METHOD="GET">
    Last edited by devil_vin; Feb 15, 2008 at 03:29.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're storing values into $javascript, but not echoing it.

    At the end, echo $javascript.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,I do echoing $javascript.However,how to ensure that the data will not be cleared after alert prompted out?

    Secondly,when refresh page,how to avoid that form being submitted again as I could still receive a blank email.

    PHP Code:
    <?php

      $javascript 
    ""
      
    $email $_GET[email];
      
    $mailto "yourname@mail.com";
      
    $mailsubj "Enquiry sent by visitor";
      
    $mailhead "From: $email";

      
    reset ($_GET);

      
    $mailbody "You have an enquiry from website:\n";

      while (list (
    $key$val) = each ($_GET)) {
            if(
    $key != 'submit'
            
    $mailbody .= "$key : $val\n"; }
          
      
    ini_set("SMTP",mail.yourmailserver.com.my);
      
    ini_set("smtp_port",25);
      
    ini_set("sendmail_from",$email);  
      
      
    $javascript "<script type=\"text/javascript\">";
      
      if (
    mail($mailto,$mailsubj,$mailbody,$mailhead)) {
         
         
    $javascript .= "alert('Thank you for your enquiry, we shall contact you soon.')";
         
    //echo '<script type="text/javascript">alert("Thank you for your enquiry, we shall contact you soon.")</script>';
         
         //$javascript = 'alert(\'Thank you for your enquiry, we shall contact you soon.\')';
    } else {

         
    $javascript .= "alert('Email cannot be delivered.')";
         
    //echo '<script type="text/javascript">alert("Email cannot be delivered.")</script>';
         //$javascript = 'alert(\'Email cannot be delivered.\')';
    }  
         
    $javascript .= "</script>";
       
    ?>

    <?php 
    if(isset($javascript)){
        echo 
    $javascript;
    }
    ?>
    Last edited by devil_vin; Feb 15, 2008 at 09:16.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To do this, you should either:


    add:
    PHP Code:
    $javascript .= "\nwindow.location = '/';"
    before:
    PHP Code:
    $javascript .= "</script>"
    or

    change the if() to send a relocation header to a success page:
    PHP Code:
    header("location: mailsuccess.php"); 
    if the mail sends, and if not:
    PHP Code:
    header("location: mailfailed.php"); 
    and alert if it failed or succeded on those pages.

    This will mean that refreshing won't send another email.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,now I didn't use any $javascript in enquiry.php just change if() to

    PHP Code:
    if (mail($mailto,$mailsubj,$mailbody,$mailhead)) {
         
           
    header("location: mailsuccess.php"); 
    } else {

       
         
    header("location: mailfailed.php"); 

    This is the form action code

    PHP Code:
    ACTION="<?php echo htmlentities($HTTP_SERVER_VARS['PHP_SELF']); ?>"

    However,the alert in mailsuccess.php always display when page loaded and redirect to my homepage when clicking it.


    mailsuccess.php
    PHP Code:
    <?php
     $javascript
    "";
     
    $javascript "<script type=\"text/javascript\">";
     
    $javascript .= "alert('Thank you for your enquiry, we shall contact you soon.')";
     
    $javascript .= "</script>";
    ?>

    <?php
    if(isset($javascript))
    echo 
    "$javascript";
    ?>

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it not be better to just show the relevant messages on those redirect pages in big writing - 'Thank you - we recieved your message' ...

    I am just thinking about what happens if the user has JS disabled...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,I try to send a reloacation header if mail sent,however the page immediately redirect to that header page without showing the form.

  8. #8
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,first problem solved.I use isset($_GET["submit"]) to ensure the form is truly being submitted then only execute mail sending.However, I much more prefer to display the alert message in the same page without clearing the form data.

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though i did not see your form but i guess you are GETing the values from the URL but i would prefer to POST the form method="post" for security reason and why action="<?php echo htmlentities($HTTP_SERVER_VARS['PHP_SELF']); ?>" in action instead of action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" or just action="" (note blank) is enough to post the form in the same page.

    And check whether form was posted or not like this:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        
    // do sending mails

    And if you are sure that the user will have their browser JS enabled then do it like this:
    PHP Code:
    ...........
    $javascript "";
    if (
    mail($mailto,$mailsubj,$mailbody,$mailhead)) {
        
    $javascript "<script type=\"text/javascript\">\n";
        
    $javascript .= "alert('Thank you for your enquiry, we shall contact you soon.')\n";
        
    # redirect to success page if mail send was successful
        
    $javascript .= "document.location='mailsendsuccess.php';\n";
        
    $javascript .= "</script>\n";
    }
    else{
        
    $javascript "<script type=\"text/javascript\">\n";
        
    $javascript .= "alert('Email cannot be delivered.')\n";
        
    # just show alert and dont redirect to another page.
        
    $javascript .= "</script>\n";
    }
    if(isset(
    $javascript)){
        echo 
    $javascript;
    }
    ........... 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •