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

    I would like to add a value in an input with value="test" but on frontend, value is displaying empty.

    Why ? Is there a way to display the value ?

    Thanks smiley
      • 30223
      • 1,010 Posts
      That’s eForm for you... eForm works by replacing all value attributes with a placeholder, which when the form is posted and needs to be displayed again get filled with the posted values (eg. when a validation error occurs)

      The first time the form is displayed eform discards the value of text fields. Dumb, I know but that’s how eForm works at the moment. An oversight you might call it, although in some circumstances it’s proven to be quite handy smiley

      A bit of a workaround perhaps but you could use the OnBeforeFormMerge event to manually add the value to the $fields array, this way eForm will replace the placeholder with the value you want.

      //<?php
      //Paste in a new snippet and include this on the same page before the eform call
      function addDefaultValue(  &$fields, &$templates ){
          global $modx;
          //only if form is not posted...
          $postback = ( count($_POST)>0 && $fields['formid'] == $modx->event->params['formid'] )? true:false;
          if(!$postback) $fields['name_of_field'] = "My Default Value";
          return true;
      }
      //?>


      [!eForm? &OnBeforeFormMerge=`addDefaultValue` ....!]
      



        • 23050
        • 1,842 Posts
        Hi TobyL, thanks for explanation smiley

        I try to add your code, but it doesn’t seem to work.

        Here is the snippet (valueMail) I created :

        <?php
        //Paste in a new snippet and include this on the same page before the eform call
        function valueMail(  &$fields, &$templates ){
            global $modx;
            //only if form is not posted...
            $postback = ( count($_POST)>0 && $fields['formid'] == $modx->event->params['formid'] )? true:false;
            if(!$postback) $fields['email'] = "Votre e-mail";
            return true;
        }
        ?>


        Then, I call it before eForm :

        [!valueMail!]
        [!eForm? &formid=`form` &tpl=`tplNewsletter` &eFormOnBeforeFormMerge =`valueMail` ... !] 


        And here is the template tplNewsletter :

        [+validationmessage+]
                	<h2><span>Newsletter</span></h2>
                    <form method="post" action="[~[*id*]~]">
                        <div><input type="hidden" name="formid" value="form" /></div>
                    	<label for="form-email">e-mail</label>
                        <input id="form-email" type="text" name="email" maxlength="255" size="19" eform="e-mail:email:1" />
                        <input src="assets/css/submit-newsletter.jpg" type="image" name="submit" value="Entrez" id="saveForm" />
                    </form>


        I tried to comment
        $postback = ( count($_POST)>0 && $fields['formid'] == $modx->event->params['formid'] )? true:false;
            if(!$postback)

        to see if the problem comes from the if part : no, it doesn’t... without $postback, value isn’t displayed

        I tried &eFormOnBeforeFormMerge and &OnBeforeFormMerge (I use 1.4.4.5) : no difference

        I tried to call the snippet valueMail cached and uncached : no difference

        I cleaned the cache : no way...

        I don’t know what to test laugh

        Note that I already run a eform2db snippet but I removed it from my call to run these tests

        Thanks for your help wink
          • 30223
          • 1,010 Posts
          Hmm, I’ll have a quick look at it when I get a chance, may not be for another couple of days.. Out to a job which has nothing to do with computers...
            • 23050
            • 1,842 Posts
            Ok TobyL thanks for your help... it’s not urgent so I can wait ! smiley
              • 30223
              • 1,010 Posts
              Don’t wait... read and experiment! You might find the solution yourself afterall.
                • 30223
                • 1,010 Posts
                Ok,... Perhaps we were simply missing something.... Try adding value="[+email+]" to the email field in your form and see if that works. What version of eForm are you using?

                If it doesn’t work post your form template chunk and we’ll see if we can work it out.
                  • 23050
                  • 1,842 Posts
                  I swear I did try a lot of things..; but nothing helped ! embarrassed

                  Finally, I found with debug mode that my fonction valueMail wasn’t called in the snippet. Maybe a mistake on the way I wrote eFormOnBeforeFormMerge huh

                  Anyway, now it works !

                  And I added value="[+email+]" too in my chunk

                  Thanks TobyL ! kiss