<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>
<?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!!
}
?>
[!eForm? &formid=`EmailForm` eFormOnBeforeFormParse=`beforeFormParseFunction` &subject=`[+subject+]` &to=`[email protected]` &ccsender=`1` &tpl=`tpl_buyform` &report=`tpl_buyform_email` &gotoid=`72` !]
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.
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.
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.
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?