• form snippet does not refresh page#

  • alejaaandro Reply #1, 8 months, 3 weeks ago

    Reply
    This is very strange:

    I have created a snippet to make a small contact form. I have the code saved in a file on the server and call it using an "include" snippet. ( I read somewhere this is done so you can work on your favorite IDE until your code is done so you avoid coding on modx manager)

    The code works great.

    As soon as i copy the code to a snippet in modx and call, i get this behaviour:
    • When loading the page for the first time it works fine, all the template is rendered and of course the form as well.
    • When submiting the form, it does not get processed (since no email is sent) and the only thing that renders is the form. The rest of the page (the template) is not shown

    Any ideas?


  • BobRay Reply #2, 8 months, 3 weeks ago

    Reply
    Make sure the form's action is
    "[[~[[*id]]]]" 
    and make sure the snippet is called uncached:
    [[!SnippetName]]
    (I'm assuming that you're using MODX Revolution --- for Evolution the tags would be different).

    Beyond that, I think we'd have to see the code to help you any further. Feel free to post it (if you put a <?php tag at the top, it will be easier to read).

    You might also look at SPForm, which creates a contact form for you or eForm (Evolution) and FormIt (Revolution, which can be used to create a contact form.)


  • alejaaandro Reply #3, 8 months, 3 weeks ago

    Reply
    the action is generated using
    $modx->makeUrl
    so i don't think that's the problem

    I'm aware of SPForm, i used it until recently but wanted to add the functionality to double-check one's email after sending the msg cause i get lots of wrong emails and i can't get back to them :-(
    I'll take a look at FormIt

    here's my code (php newbie, so some things might look odd)
    
    <?php
    
    //OPTIONS
    //    email = which mail to send to (default: info@athenslindyhop.com)
    //    subject = prepend this text to the subject (optional)
    //    successMsg = message to print when successfully sent email (Defaul as set below))
    
    //SET VARS
    //    $sender = the user must insert that in contact form and i will get this with _Post
    $email = $modx->getOption('email',$scriptProperties,'info@athenslindyhop.com');
    $successMsg = $modx->getOption('successMsg',$scriptProperties,'<i>Check that the above address is correct, or else we will not be able to contact you back.(If it isn\'t, send us your message again)</i><br><strong>We will get back to you as soon as possible!</strong>');
    
    
    
    //Print the contact form
    echo '<form method="post" action="'.$modx->makeUrl($modx->resource->get('id'),'','','http').'">';
    echo '<p>Your name:<br /><input name="name" /></p>';
    echo '<p>Your email:<br /><input name="email" /></p>';
    echo '<p class="antispam">Leave this empty:<br /><input name="url" /></p>';
    echo '<p>Subject:<br /><input name="subject" /></p>';
    echo 'Message:<br><textarea rows="10" cols="25" name="message"></textarea>';
    echo '<br><input type="submit" value="Send" />';
    echo '</form>';
    
    
    
    //insert the antispam css 
    $modx->regClientStartupHTMLBlock('<!--Next lines are inserted by contactForm snippet for antispam reasons -->
          <style type="text/css">
          .antispam { display:none;}
          </style>');
          
    
    
    //Check if the form has been posted or if it's the first time the page has been loaded
    if (!isset($_POST['name'])) {return ;}
    
    
    
    //Test for validations
    $ok = true;
    $errorMsg = 'Mail was not sent. Please fix the following error(s):<ul>';
    if (empty($_POST['name'])) {$ok = false; $errorMsg .= '<li>Name is empty. Please enter your name.</li>';}
    if (!validEmail($_POST["email"])) {$ok = false; $errorMsg .= '<li>The email you entered is not valid. Please make sure it\'s correct so we can answer to you.</li>'; }
    if (empty($_POST['message'])) {$ok = false; $errorMsg .= '<li>Message is empty. Did you forget what you wanted to tell us?</li>';}
    $errorMsg .= '</ul>';
    if (!$ok){return $errorMsg;}
    
    
    //Send the actual messages
    if(isset($_POST['url']) && $_POST['url'] == ''){
          //Send mail to us      
          // Build the text to be sent
          $msg = $_POST["name"].' <'.$_POST["email"].'> wrote:'."\n\n";
          $msg .= $_POST["message"];
          
          //Build headers
          $headers = 'From:'.$_POST["email"] . "\r\n" .
                'Reply-To:'.$_POST["email"]  . "\r\n" ;
          
          // email
          mail( $email,  $subject." ".$_POST["subject"], $msg, $headers);
          
          //Send a copy to the visitor
          // Build the text to be sent
          $msg = "You successfully sent the following message to Athens Lindy Hop.\nThank you for contacting us, we will get back to you as soon as possible.\n\nΣÏείλαÏε εÏιÏÏÏÏÏ Ïο ÏαÏακάÏÏ Î¼Î®Î½Ïμα ÏÏοÏÏ Athens Lindy Hop.\nÎÏÏαÏιÏÏοÏμε για Ïην εÏικοινÏνία και θα αÏανÏήÏοÏμε Ïο ÏÏνÏομÏÏεÏο.\n\n---------\nOriginal message: \n\n";
          $msg .= $_POST["name"].' <'.$_POST["email"].'> wrote:'."\n\n";
          $msg .= '> '.str_replace("\n", "\n> ", $_POST["message"]);
          
          //Build headers
          $headers = 'From:noreply@athenslindyhop.com' . "\r\n" .
                'Reply-To:noreply@athenslindyhop.com'  . "\r\n" ;
          
          // email
          mail( $_POST['email'],  "Message sent to Athens Lindy Hop", $msg, $headers);
          
          
          //Display a confirmation message on screen
          return '<p style="font-size:larger"> <span style="color: green"> Message successfully sent from:</span> <b>'.$_POST['email'].'</b></p>'.$successMsg;
          
         
          
    }
    
    
    /**
    Validate an email address.
    Provide email address (raw input)
    Returns true if the email address has the email 
    address format and the domain exists.
    */
    function validEmail($email)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) && !$atIndex)
       {
          $isValid = false;
       }
       else
       {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);
          if ($localLen < 1 || $localLen > 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen < 1 || $domainLen > 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if
    (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                     str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/',
                 str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }
          if ($isValid && !(checkdnsrr($domain,"MX") || 
     âªcheckdnsrr($domain,"A")))
          {
             // domain not found in DNS
             $isValid = false;
          }
       }
       return $isValid;
    }