We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19872
    • 1,078 Posts
    I have a site that I'm not quite ready to set up in MODx, but stumped on a mail form issue. So, I'm reaching out for resource suggestions. I have an html form, and a PHP processing script that I built a long time ago. I'm not a PHP coder and I only understand bits and pieces of the PHP. Everything I'm finding on the internet, even at w3schools provides just enough information for me to hang myself. I tend to be tenacious and have decided that I am going to learn how to build my script to process my form, but would appreciate an suggestions for resources for doing so. Is Treehouse a good place to start?

    My html form has error CSS classes — using Zurb Foundation 3 framework. I've love to be able to right a processing script that calls up the error classes and allows the form to display them as needed.

    I figured out how to modify my current script to sanitize the fields, but if the user doesn't fill out a required fields, my design falls apart with the display of a white page, and small black type in the upper left hand corner with the error message. Now... I could live with this scenario and assume that most users know to fill out the fields, and would never see the error message, but that would be like only painting the top of the porch railing and not the underside.



      • 6629
      • 60 Posts
      You should take a look at FormIt. It's the most popular modx addon regarding forms and form processing.
      Example contact form
        • 19872
        • 1,078 Posts
        Quote from: DasItsch at Aug 08, 2013, 09:36 AM
        You should take a look at FormIt. It's the most popular modx addon regarding forms and form processing.
        Example contact form

        FormIt is totally awesome. At this juncture though, my project is not big enough, and there's not time for me to move it into MODx. Eventually, that would be the goal.

        With this current project I would just like to be able to build this myself so that I have a better understanding of the ins and outs. I may just enroll at the Treehouse and run through their tutorials for PHP.
          • 3749
          • 24,544 Posts
          You might look at the code of the Subscribe extra. It uses JavaScript to validate the form so it can't be submitted until it's correct. The code should work fine outside of MODX, though you'll have to modify the JS to match your form and users with JavaScript turned off will still see the messy page.

          That said, the problem is most likely that your code is not redisplaying the full form and loading the CSS when there's an error.

          The typical logic (in pseudocode) would be:



          Load CSS
          
          if (Form submitted) {
             check for errors
             if (error) {
                echo error
                display form
             } else {
                Process form
                echo success message or forward user to another page
             }
          } else {
              display form
          }
          


          Some scripts turn this upside down (yours might), which would be:

          Load CSS
          
          if (not form submitted) {
              display form
          } else {
            check for errors
            if (error) {
                echo error
                display form
             } else {
                Process form
                echo success message or forward user to another page
             }
          }
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 19872
            • 1,078 Posts
            Hi Bob:
            Thanks for the tips. Foundation 3 does have a .js file for the form, but I'm pretty sure for it to function, there needs to be an application (i.e. PHP) used in conjunction with it. Their documentation specifically says so. At this juncture—going live with my 3 page site later today, I've opted to bypass any front end PHP, or attempting to connect the error CSS, etc, and hope that users just fill in all 3 fields like their supposed to. I have default text in each form field —"Email Required".... etc.

            I've tested my current PHP script and it processed the form, only allows an email address in the email field, and it successfully sends the form to the intended email address. So far so good.

            Where's the best place for me to jump in an learn some PHP for doing all this form stuff? In the end, it might even help me with MODx by having a better understanding of the PHP.

            Here's my script. Please don't yell. It's got a bunch of echo this and that at the end that I'm not even sure it's actually doing anything. I want to understand it enough to feel secure that the script is safe and secure. And if something does go awry, I want to be able to confidently fix it.

            <?php
            /* Set e-mail recipient */
            $myemail  = "[email protected]";
            $subject = "Website Inquiry"." ".$_REQUEST['name'];
            
            /* Check all form inputs using filter_input function */
            
            $name    = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); // Visitor Name
            
            $email   = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); // Visitor Email
            
            $comments = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_STRING); // Visitor Message
            
            
            /* If e-mail is not valid show error message */
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
            {
                show_error("E-mail address not valid");
            }
            
            
            /* Let's prepare the message for the e-mail */
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
            $headers .= "From: [email protected]" . "\r\n";
            $headers .= "Reply-To: ".$email."\r\n";
            $headers .= "Return-Path: ".$email."\r\n";
            $message = "
            <html>
            <body>
            <p>Hello!</p>
            
            <p>Your website form has been submitted by:</p>
            
            <p><strong>Name:</strong> $name</p>
            <p><strong>E-mail:</strong> $email</p>
            
            <p><strong>Comments:</strong></p>
            <p>$comments</p>
            
            End of message
            </body>
            </html>
            ";
            
            /* Send the message using mail() function */
            mail($myemail, $subject, $message, $headers);
            
            /* Redirect visitor to the thank you page */
            header('Location:http://www.mydomain.com/_mailman/thankyou.html');
            exit();
            
            /* Functions we used */
            function check_input($data, $problem='')
            {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                if ($problem && strlen($data) == 0)
                {
                    show_error($problem);
                }
                return $data;
            }
            
            function show_error($myError)
            {
            ?>
                <html>
                <body>
            
                <b>Please correct the following error:</b><br />
                <?php echo $myError; ?>
            
                </body>
                </html>
            <?php
            exit();
            }
            ?>


              • 3749
              • 24,544 Posts
              You shouldn't need any PHP to make the JS work. You'd just put the JS in your form (between script tags) and add an onClick function to your submit button.

              To ease the transition to MODX (and generally make things easier to maintain), I'd recommend adding all the output to a variable called $output, like this:

              $output = ''; /* put this at the top */
              
              $output .= $msg;


              Anything that gets shown on the screen should be a string that gets added to that variable.

              Then at the end, do this:

              echo $output;


              That way, the code would work as a MODX snippet just by changing that last line to:

              return $output;


              There are plenty of good PHP primers on the web. There's a nice one (at least I think so) in the Appendix of my book: http://amzn.to/Qfsl1Z. wink

              The key thing to know if you'll be using MODX, is that MODX doesn't allow you to mix PHP and HTML as you've done above.

              PHP goes in snippets, and any HTML to be output is usually either in a chunk, or in strings in the PHP code that will be returned in the $output variable.

              Most often, the way this works is you put all the HTML in a chunk which contains placeholder tags for anything you might want to change in the HTML. The PHP code in the snippet gets the chunk, replaces the placeholders, and returns the finished code in the $output variable.

              BTW, your two functions *would* do something, but as far as I can see, they are never called in your PHP code, so the code in them will never execute. They would also need to be at the top of the code rather than the bottom.

              TBH, teaching someone how to use PHP outside of MODX is pretty far outside the scope of this forum. wink






                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 10208 ☆ A M B ☆
                • 1,780 Posts
                Without knowing what the 3 page site entails that might be special, I have to ask why not serve it with MODX? You can put any html and css into modx without leveraging anything special within MODX. Just put your html in a template, or even three templates, assign 3 resources/pages, and serve it right up.

                Then you can use formIt and forget all this finagling with your form. FormIt will handle your errors and validation. I'd chunk out the form html though, rather than placing it in the template for that page. Easy enough.
                  Frogabog- MODX Websites in Portland Oregon
                  "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                  Having server issues? These guys have MODX Hosting perfected - SkyToaster
                  • 19872
                  • 1,078 Posts
                  I know what I'm doing at the moment is outside the forum. I just figured that since MODx is PHP and many of it's users seem pretty well-versed in PHP, they could point me in a direction.

                  The JS can take care of itself. What I have is a PHP file that collects the contents of the form fields after the user hits the SEND buton and emails them to an email address that I have designated in the PHP. Right now it works, but I lie awake at night wondering if it's truly validating and blocking spammers. How do I know that some freak isn't tapping in and sending out random images from my clients domain. I mean if Anthony Weiner knows PHP, I am in big trouble:)

                  So I'm just looking for resources that can provide more than a snippet of code that only accomplishes a fraction of the task at hand.

                  I'm headed over to Treehouse for the weekend and will see what I can learn.

                  M

                  Quote from: BobRay at Aug 09, 2013, 04:25 PM
                  You shouldn't need any PHP to make the JS work. You'd just put the JS in your form (between script tags) and add an onClick function to your submit button.

                  To ease the transition to MODX (and generally make things easier to maintain), I'd recommend adding all the output to a variable called $output, like this:

                  $output = ''; /* put this at the top */
                  
                  $output .= $msg;


                  Anything that gets shown on the screen should be a string that gets added to that variable.

                  Then at the end, do this:

                  echo $output;


                  That way, the code would work as a MODX snippet just by changing that last line to:

                  return $output;


                  There are plenty of good PHP primers on the web. There's a nice one (at least I think so) in the Appendix of my book: http://amzn.to/Qfsl1Z. wink

                  The key thing to know if you'll be using MODX, is that MODX doesn't allow you to mix PHP and HTML as you've done above.

                  PHP goes in snippets, and any HTML to be output is usually either in a chunk, or in strings in the PHP code that will be returned in the $output variable.

                  Most often, the way this works is you put all the HTML in a chunk which contains placeholder tags for anything you might want to change in the HTML. The PHP code in the snippet gets the chunk, replaces the placeholders, and returns the finished code in the $output variable.

                  BTW, your two functions *would* do something, but as far as I can see, they are never called in your PHP code, so the code in them will never execute. They would also need to be at the top of the code rather than the bottom.

                  TBH, teaching someone how to use PHP outside of MODX is pretty far outside the scope of this forum. wink






                    • 19872
                    • 1,078 Posts
                    I was under a tight timeframe and while MODx is great, I've only had one or two instance where the install goes lickity split. Between setting up the database, and all that goes with putting the pieces together, in this scenario to start with it was much faster for me to just code a traditional site. Next week I'll put it into MODx and then maybe I can get FormIt to do its thing. There is madness in my method—I know.

                    Quote from: frogabog at Aug 09, 2013, 07:16 PM
                    Without knowing what the 3 page site entails that might be special, I have to ask why not serve it with MODX? You can put any html and css into modx without leveraging anything special within MODX. Just put your html in a template, or even three templates, assign 3 resources/pages, and serve it right up.

                    Then you can use formIt and forget all this finagling with your form. FormIt will handle your errors and validation. I'd chunk out the form html though, rather than placing it in the template for that page. Easy enough.
                      • 3749
                      • 24,544 Posts
                      It looks like your form is a simple contact form, in which case SPForm should work for you right out of the box in Revolution with no coding whatsoever.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting