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

    I am quite new with modx and still struggling.

    Situation:
    1 - I got a page where the user can see different products.
    2 - The user can click to one of them and a form will show up. In this form the user can introduce his details and buy the product.

    How can I pass the information of the product to the form?

    I would like to show the information of the product in the same page as the form, for example showing the picture of the product, price, etc.

    I am using eForm.

    Here is the way I call eform

    [!eForm? &formid=`EmailForm` &subject=`[+subject+]` &to=`[email protected]` &ccsender=`1` &tpl=`tpl_buyform` &report=`tpl_buyform_email` &gotoid=`72` !]

    If I was programming with PHP I would have passed the $id of the product in a $_GET and then I would have read it in my form page. I’ve tried to do something similar in Modx environment but I couldn’t work it out.


    Thanks a lot,
      • 30223
      • 1,010 Posts
      Hove a look at the documentation that comes with eForm about form events or browse the wiki (see link in my signature).

      You can do this by writing an event function that will run on the eFOrmOnBeforeFormParse eventand adding some placeholders in your form template. Here’s a rough example...

      form template;
      <div>[+productImage+]</div>
      <div>[+product_name+]<span>[+product_price+]</span></div>
      
      [+validationmessage+]
      <form id="EmailForm" name="EmailForm" method="post">
      	<input type="hidden" name="product_id" value="[+product_id+]" />
      		
      <p>	<label for="cust_name">Name:</label>
      <input type="text" name="cust_name" id="cust_name" eform="Customer Name:text:1" /></p>
      	<!-- more fields -->
      	<input type="submit" name="submit" value="Buy Now!" />
      </form>


      The function:
      <?php
      function beforeFormParseFunction(&$fields,&$templates){
         global $modx;
         //get product id from GET or POST (from hidden field)
         $pr_id = isset($_REQUEST['product_id']) ? $_REQUEST['product_id'] : 0;
         
         //set the value for the hidden field direcly in the form template
         //you need to do this so that the value propagates and so that eForm can validate it automatically
         //(even if no product was selected) 
         $templates['tpl'] = str_replace('[+product_id+]',$pr_id,$templates['tpl']);
         
         //get the product details - write that yourself!!
         $product_details = getProductDetails($pr_id);
         //set the appropriate values in the $fields variable
         //they will be replaced when the form is displayed
         //the advantage of doing this is that they will also be available to your report template
         $fields['productImage'] = $product_details['html_image_tag'];
         $fields['product_name'] = $product_details['name'];
         $fields['product_price'] = $product_details['price'];
         //expand as necessary
      
         return true; //Important!!
      }
      ?>
      

      And the snippet call:
      [!eForm? &formid=`EmailForm` eFormOnBeforeFormParse=`beforeFormParseFunction` &subject=`[+subject+]` &to=`[email protected]` &ccsender=`1` &tpl=`tpl_buyform` &report=`tpl_buyform_email` &gotoid=`72`  !]


      How you tie all this together and expand you can learn from the documentation and examples already mentioned..

      Good luck
        • 31133
        • 22 Posts
        Thank you very much. It was the first time I have posted a topic and really amaze by your great answer.

        I am going to try it right now.

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: uri at Oct 17, 2007, 09:07 AM

          Thank you very much. It was the first time I have posted a topic and really amaze by your great answer.

          I am going to try it right now.
          Also note that the id $_GET parameter is reserved (for now) in MODx, as that is used to tell the parser what document you are requesting. q is another $_GET parameter that should be avoided for the time being as well. Otherwise, you can pass and use $_REQUEST variables in MODx snippets as you normally would in a PHP script.
            • 31133
            • 22 Posts
            Alright, I will not use $_GET.

            Another thing is: Where shall I put the code of beforeFormParseFunction?

            I guess it could go just before the eform snippet call. Is this the right place?



              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: uri at Oct 17, 2007, 09:28 AM

              Alright, I will not use $_GET.
              That’s not at all what I just said. I said using $_GET[’id’] or $_GET[’q’] is bad, as is $_POST[’id’] or $_POST[’q’] -- using $_GET and $_POST in general is fine.
                • 31133
                • 22 Posts
                Ok. I misunderstood.
                  • 30223
                  • 1,010 Posts
                  Another thing is: Where shall I put the code of beforeFormParseFunction?
                  I guess it could go just before the eform snippet call. Is this the right place?


                  That is all revealed in the documentation... have a read. You can find it in the assets/snippests/eform/docs/ folder