We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33238
    • 388 Posts
    Quote from: bennyb at Mar 02, 2017, 12:31 PM
    Quote from: Ysanmiguel at Mar 02, 2017, 05:10 AM


    But can I use [[+price]] and [[+date]] placeholders if they are coming from the JS?:

    You'll need to have a hidden input for both price and date. Then set their values with JS. These will then be available as placeholders in the emailTpl.

    Ok I understand but the JS is calling a JSON string, Price and Date might be the values of one product, but it is possible that there are 2 or more products with the same vales, eg:

    here the string for one product (in that case is easy create an input field for every value.):
    {"SCI-1":{"id":"SCI-1","price":75,"date":"Wednesday, 8 March, 2017"}}


    but how can I do it with more than one product?:
     {"SCI-1":{"id":"SCI-1","price":75,"date":"Wednesday, 8 March, 2017"},"SCI-2":{"id":"SCI-2","price":45,"date":"Thursday, 9 March, 2017"}}


    If you see the JS it's getting all values and put them all inside one <input field, no matter how many different items the string has, it works but the HTML labels get lost in the process:
    function submitCheckout(){
        var finalplanString = "";
        var elements = JSON.parse(localStorage.local_items);
        for(var plan in elements){
        finalplanString += "<div class=\"wrap\">";
        finalplanString += "price: $" + elements[plan].price + "<br>"; 
        finalplanString += "date: " + elements[plan].date + "<br>"; 
        finalplanString += "</div>";
        }
        document.getElementById('hiddenInput').value = finalplanString;
        $('#checkoutForm').submit();
    };
      --
      ysanmiguel.com
      • 36926
      • 701 Posts
      Quote from: Ysanmiguel at Mar 03, 2017, 03:29 AM

      but how can I do it with more than one product?:
       {"SCI-1":{"id":"SCI-1","price":75,"date":"Wednesday, 8 March, 2017"},"SCI-2":{"id":"SCI-2","price":45,"date":"Thursday, 9 March, 2017"}}


      A couple of ways, you could pass the JSON in via a hidden input field and the in you emailTpl have a snippet that deals with that. But you'll need to do some encoding to do this, see here:
      http://stackoverflow.com/a/18443279/7446898

      Or create a hidden field for each price and date and also one for the total products. You can then run a snippet in you emailTpl generate the placeholders. An example would look something like.
      emailTpl would contain a snippet
      [[productInputs? &total=`[[+total]]`]]
      

      Then the productInputs snippet would look something like
      $output = "";
      $x = 1;
      while($x <= $total) {
          $output .= "<br>Product $x price is [[+price".$x."]] and date is[[+date".$x."]]";
          $x++;
      } 
      return $output;
      


      The hidden fields you create would be look like
      <input name="price1" value="100"><input name="date1" value="01 01 2017">
      <input name="price2" value="100"><input name="date2" value="01 01 2017">
      <input name="price3" value="100"><input name="date3" value="01 01 2017">
      


      The snippet could be improved but it should give you the idea.
        • 33238
        • 388 Posts
        Well good info, I think the encode will be a good idea and I really like the way to build the placeholders using the [[productInputs? snippet.

        Now I have another question:

        In case that users add 10 products X 8 fields per product (date and price are just 2 out of 8 fields in total) it will be 80 hidden input, there is another way to send this data into FormIt to avoid this insane amount of hidden input fields?

        That was the reason I tried to put everything in one input field before, or at least 1 hidden input per product that means 8 input fields with the corresponding data of each product inside.

        what do you think?
        and thank you for your help.
          --
          ysanmiguel.com
          • 36926
          • 701 Posts
          Quote from: Ysanmiguel at Mar 06, 2017, 03:39 AM


          In case that users add 10 products X 8 fields per product (date and price are just 2 out of 8 fields in total) it will be 80 hidden input, there is another way to send this data into FormIt to avoid this insane amount of hidden input fields?

          The JSON option might be best then.

          Although if each product always has a set amount of fields an idea could be to have just hidden inputs for each product field. So you'd have one for date and one for price and their values could be a comma separated list.
          So for example.
          <input name="price" value="100,200">
          <input name="date" value="01-01-2017,02-02-2017">
          

          Then within the snippet create 2 arrays for price and date. Both arrays should in theory be the same length and the index position should be the same for both.
          So in the example above to get product 1 you could do.
          'Product: '.$product[0].' Price: '.$price[0];