We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17905
    • 7 Posts
    i’m rolling my own form processor [as the built-in ones don’t work for me]. the form is validated on one page [say] "form.php" and on submission the user is taken to [say] "thanks.php" where they are presented with a personalised thank-you using the data submitted in the form. the "thanks.php" also does the actual emailing of the form info. when doing this on a ’hand-written’ web page, i do it as follows:

    <?php
    //// php mail() function to email form info
    }
    ?>
    <!DOCTYPE  ........ etc........>
    <html>
    
    page content with "thank you" message
    
    </html>
    


    however, i’m finding it hard to get this to work with modx:

    if i create a snippit with the PHP mail() function and then include that snippet in the page template, it generates a PHP parse error [presumably modx doesn’t like a snippet being put before the "doctype" declaration?]

    if i make a new template specific for that page and add the full PHP mail() function directly into the code for the template, i don’t get any error messages, but the code isn’t parsed as PHP, as i can see the PHP code if i view the source for the page.

    if i put the PHPcode in an external file and include this with an "@EVAL" call, i just get the "@EVAL" line written directly into my page. again, it seems that modx doesn’t like to parse an "@EVAL" occurring before the "doctype".

    is there anyway round this by using a different technique from within modx?

    if there isn’t, i’ll just have to build my own page and add it to my site "assets" folder, but i’m worried this will turn into a major hassle, trying to get it to properly integrate with the rest of the site.

    all i really want is to be able to include a chunk of PHP code in a page, before the "doctype" declaration and have modx leave it as it is and let the server take care of the rest huh
    • Quote from: madra at Apr 04, 2006, 02:46 PM

      however, i’m finding it hard to get this to work with modx:

      if i create a snippit with the PHP mail() function and then include that snippet in the page template, it generates a PHP parse error [presumably modx doesn’t like a snippet being put before the "doctype" declaration?]

      PHP is contained only in snippets in MODx. And you do not use <?php tags inside of snippets. You cannot include PHP directly in your templates either, because this is treated as standard HTML content. All you need to do is put your code in the snippet, then call it in the template:

      [!MyFormProcessor!]
      <!DOCTYPE ... >
      ...
      
      


      If that’s not working then there is a problem with your PHP code, thus the parser error. Feel free to post the specific parser error you are getting. I think you’ve almost got it.
        • 17905
        • 7 Posts
        well, i tried my PHP snippet both with and without "<?php --- ?>" tags, as i wasn’t sure which was right. i seem to have solved the problem by just adding a manually created "thanks.php" page into my "assets/docs" folder and having that as the ’action’ of the form on the "form.php" page. it doesn’t seem to have broken anything else [i wondered if it might screw up the navigation links, but it seems OK], so i’ll probably leave it at that for now [just glad to have got it working! smiley]. i can come back to trying to "snippet" it later, when i’ve got more ’tinkering’ time.

          • 11413
          • 203 Posts
          that or a plugin that runs in the BeforeRender event or something like that
            Blaise Bernier

            www.medialdesign.com - Solutions for small business, hosting, design and more!
            • 26435
            • 1,193 Posts
            First of all, I apologise if this seems a bit hack-ish, but I have been a HTMLer for years who is just recently learning the basics of php. If there is a better way to do this, I would be eternally greatful to a proper PHP coder who would show me the way.

            that said, in your html form, add a hidden input like this:

            <form method="post" action="[~[*id*]~]">
            <!-- The next line executes the sendmail function only after the "submit" button has been pressed. -->
            	<input type="hidden" name="send" value="true" />
            	<input name="[+tagname+]" type="hidden" value="on" />
            	Subject:<br /><input name="subject" type="text" size="40" value="" /><br />
            	Comment:<br /><textarea name="comment" cols="50" rows="8"></textarea><br />
            	<input name="send" type="submit" value="Submit" />
            </form>


            then in your php:

            $postSend= isset($_POST['send'])? $_POST['send']: 'false';
            			if ($postSend == 'false') {
            				
            		// return  your html form
            		return $yourFormVariable;
            		
            		} else {
            
                        (mail($recipient, $subject, $msg, "From: $email\n")); // Send the message.
                         return $thankYouMessage;


            That works for me anyway.
            I hope it helps you!

            -sD-
            scottyDelicious
              Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
              All of the above... in no specific order.


              I send pointless little messages