We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32391
    • 37 Posts
    Hi there,

    I have a form with some processing code that I want to add to a page but I have no idea how to do this in a snippet!
    Here’s the code I want to transform:

    $messageSent = 0;
    
    if(isset($_POST['submit']))
    {
      include('processDetails.php');
    
      $processDetails = new ProcessDetails();
    
      if($processDetails->submitMessage()) {
         $messageSent = 1;
      }
      else {
        $processDetails->displayErrors();
      }
    }
    
    $detailsToken = $_SESSION['detailsToken'] = md5(uniqid(mt_rand(), true));
    
    if ($messageSent == 1) {
    	echo '<h2>Thank you!</h2>
    			<p>Thank you for filling out the form!</p>';
    } else {
    
    echo '
    <h2>Lovely Form</h2>
    <form id="quoteForm" method="post" action="'. $_SERVER['PHP_SELF'] .'">
            	<label>Frist Name &#42;</label>
                        <input type="text" class="required" name="firstName" value="'; if(isset($_POST['firstName'])) { echo $_POST['firstName']; } echo'" />
    ...etc...
                    
    			
    	    <input type="hidden" name="token" value="'. $detailsToken .'" />
    			
                <p><input id="send" type="submit" name="submit" value="Submit" /></p>
            </form>';
    }
    ?>


    Any pointers? Do I just have to replace the echos with outputs? Will the snippet be able to read the posted data or do I have to pass it through the snippet call?
    Is there an easier way to stick php on a page without using snippets?

    Thanks
      • 34193
      • 330 Posts
      As with many things in modx there are many ways to do many things the quickest but most nasty way is copy the code you have into a new snippet.

      Change
      echo '<h2>Thank you!</h2>
      			<p>Thank you for filling out the form!</p>';
      


      to

      return ('<h2>Thank you!</h2>
      	 <p>Thank you for filling out the form!</p>')
      


      and
      echo '
      <h2>Lovely Form</h2>
      <form id="quoteForm" method="post" action="'. $_SERVER['PHP_SELF'] .'">
              	<label>Frist Name &#42;</label>
                          <input type="text" class="required" name="firstName" value="'; if(isset($_POST['firstName'])) { echo $_POST['firstName']; } echo'" />
      ...etc...
                      
      			
      	    <input type="hidden" name="token" value="'. $detailsToken .'" />
      			
                  <p><input id="send" type="submit" name="submit" value="Submit" /></p>
              </form>'
      


      EDIT ok that code needs more work but to display somthing you just return it from the snippet instead of echoing.

      to

      '<h2>Thank you!</h2>
      			<p>Thank you for filling out the form!</p>';
      } else {
      
      return ('
      <h2>Lovely Form</h2>
      <form id="quoteForm" method="post" action="'. $_SERVER['PHP_SELF'] .'">
              	<label>Frist Name &#42;</label>
                          <input type="text" class="required" name="firstName" value="'; if(isset($_POST['firstName'])) { echo $_POST['firstName']; } echo'" />
      ...etc...
                      
      			
      	    <input type="hidden" name="token" value="'. $detailsToken .'" />
      			
                  <p><input id="send" type="submit" name="submit" value="Submit" /></p>
              </form>')
      


      I personally would look at using place holders and chunks see

      http://rtfm.modx.com/display/revolution20/Templating+Your+Snippets

      For more info on snippets see
      http://rtfm.modx.com/display/revolution20/Snippets

        • 34193
        • 330 Posts
        Or just look at formit
        http://rtfm.modx.com/display/ADDON/FormIt

        With a custom hook
        http://rtfm.modx.com/display/ADDON/FormIt.Hooks

        I did say there would be many ways smiley