We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29181
    • 480 Posts
    I’m using a snippet from earlier on in this forum, and have just noticed that it doesn’t quite work as intended.

    <?php
    function populate(&$fields, &$templates){
    $names = array(
        	"Person 1" => 1,
        	"Person 2" => 2,
        	"Person 3" => 3,
        	"Person 4" => 4,
    );
    global $modx; // Access to DocumentParser
    $evt =& $modx->event; // The Snippet call event (not eFormOnBeforeFormParse event) 
    // Replicate $isPostBack variable
    $validFormId = ($evt->params['formid'] == $_POST['formid']);
    $isPostBack = ($validFormId && count($_POST) > 0); 
    // (bool)true if the form is being updated
    $mail_id=trim($_REQUEST['sel']);
    //save the value of selected to a variable
    if (!$isPostBack) {
    // Check for NOT postback, cuz we want to populate an empty form
    $chosen = isset($_REQUEST['sel'])?$_REQUEST['sel']:99;
    $optionTags ="<select name=\"recipient\" id=\"cfRegarding\" eform=\"Form Subject::1\"><option value=\"\">Please Select</option>";
    foreach ($names as $name => $value){
    	$selected=($value == $chosen)? ' selected' : '';
    	$optionTags .= "<option value = \"".$value."\"".$selected.">".$name."</option>\n";
    }
    $optionTags.="</select>";
    }
    $templates['tpl'] = str_replace('[+SelectBox+]',$optionTags,$templates['tpl']);
    } else {
    // The form has beed posted, let's not disturb the data.
    }
    return true; // A value of false will stop the eForm parser
    }
    ?>


    which works fine, but if the person submits the form without filling out the required fields then I get the following fields need to be filled out...but the recipient dropdown is no longer visible. I suppose this is due to

    $isPostBack = ($validFormId && count($_POST) > 0); 
    if (!$isPostBack) {


    but if I remove the if statement then I get a 404 doc not found.

    My second problem is
    <select name=\"recipient\" id=\"cfRegarding\" eform=\"Form Subject::1\">


    will still allow me to submit even if it doesn’t have any value.

    Thanks for any pointers!
    Taff
      Adrian Lawley: www.adrianlawley.com