1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

PHP in Email template does not work when it should

Discussion in 'Standard Support' started by jimkeane, May 23, 2013.

  1. jimkeane

    jimkeane Member

    Level: Community
    Hello,

    I have successfully used HTML email templates on form submission for some time and needed to do some dynamic stuff so changed to PHP.

    Essentially I want to show one lot of text for condition A and another set of text for condition B. it works if I hardcode the values but not when the same value is pulled from a placeholder.

    Here is my original version of the PHP:

    Code (Text):
    <?php
    $confidentiality = '{dsq_confidentiaity_share_raw}';
    if ($confidentiality=='1')
        {
        echo 'You gave us permission to share information related to you and your support needs with people who, in the course of their work, have a valid reason to be informed.';
        }
    else
        {
        echo 'You did not give us permission to share information related to you. As a result we may be unable to meet all of your support needs. <BR><BR> <a href="https://sss-vm.hull.ac.uk/webforms/index.php?option=com_fabrik&view=form&fabrik=8&___first_name={first_name}&___family_name={family_name}&___DSQ_Address={DSQ_Address}&___DSQ_Telephone={DSQ_Telephone}&___DSQ_Email={DSQ_Email}&___d_o_b={d_o_b}&___dsq_courses={dsq_courses}&___dsq_ft_pt={dsq_ft_pt_raw}&___dsq_ug_pg={dsq_ug_pg_raw}&___applicant_number={applicant_number}&___dsq_disability={dsq_disability_raw}&___dsq_disability_details={dsq_disability_details}&___dsq_support_types_needed={dsq_support_types_needed}&___dsq_uoh_accomm={dsq_uoh_accomm}&___dsq_uoh_accomm_any_reqs={dsq_uoh_accomm_any_reqs}&___dsq_uoh_accomm_reqs={dsq_uoh_accomm_reqs}&___dsq_further_info={dsq_further_info}&___dsq_confidentiaity_share=Yes" style="color:#990000">Click here if you wish to resubmit your questionnaire changing this.</A>';
        }
    ?>
    I then took a look at the debug.php and came up with this:
    Code (Text):
    <?php
    $s1 = "{dsq_confidentiaity_share_raw}";
    $s2 = "1";
    if ($s1 == $s2) {?>
    You gave us permission to share information related to you and your support needs with people who, in the course of their work, have a valid reason to be informed.
    <?php } else {?>
    You did not give us permission to share information related to you. As a result we may be unable to meet all of your support needs.
    <?php }?>
    I've tried several versions of the code, debugged it by echoing the values, removing the hyperlink, changed from using the normal value to a numeric raw value, but it always returns the else condition, unless I hard code the value of dsq_confidentiaity_share_raw

    I know I could do several versions of the template and send them conditionally via a set of submission plugins but that would just be ugly!

    Any ideas folks?

    Many thanks :) ,

    Jim
     
  2. jfquestiaux

    jfquestiaux Well-Known Member

    Level: Community
    Maybe it's a problem with placeholders but just in case, are you sure it's "dsq_confidentiaity_share_raw" and not "dsq_confidentiality_share_raw"?
     
  3. jimkeane

    jimkeane Member

    Level: Community
    Sadly it is that badly spelled! I have echoed out the value returned an it is correct :(

    On some further reading it may not be handling type juggling correctly. I'll test further...
     
  4. jimkeane

    jimkeane Member

    Level: Community
    I've just run this code:

    Code (Text):
    $conf_form = '{dsq_confidentiaity_share_raw}';
    $confidentiality = (int) $conf_form;
        echo '{dsq_confidentiaity_share_raw}';
        echo '<br> that was the fabrik variable, now echoing the PHP Variable:<br>';
        echo '$confidentiality';
        echo '<br> and now echoing the PHP Variable withouth quotes<br>';
        echo $confidentiality;
        echo '<br>and that is what kind of value?<br>';
        echo gettype($confidentiality);

    Which returns the following:
    I'm bamboozled by this, 1 is becoming 0.

    Any thoughts welcome!
     
  5. jimkeane

    jimkeane Member

    Level: Community
    A bit more testing, I have tried using strpos and strcmp instead:

    strpos:
    Code (Text):
    <?php
    $confidentiality = '{dsq_confidentiaity_share}';
        echo '<BR><i>DEBUG INFO:<BR>';
        echo '{dsq_confidentiaity_share}';
        echo '<br> that was the fabrik variable, now echoing the PHP Variable:<br>';
        echo '$confidentiality';
        echo '<br> and now echoing the PHP Variable without quotes<br>';
        echo $confidentiality;
        echo '<br>and that is what kind of value?<br>';
        echo gettype($confidentiality);
        echo '<br><br></i>';
     
        if (strpos($confidentiality,'Yes') !== false)
        {
        echo 'You gave us permission to share information related to you and your support needs with people who, in the course of their work, have a valid reason to be informed.';
        }
        else
        {
        echo 'You did not give us permission to share information related to you. As a result we may be unable to meet all of your support needs. <BR><BR> <a href="https://sss-vm.hull.ac.uk/webforms/index.php?option=com_fabrik&view=form&fabrik=8&___first_name={first_name}&___family_name={family_name}&___DSQ_Address={DSQ_Address}&___DSQ_Telephone={DSQ_Telephone}&___DSQ_Email={DSQ_Email}&___d_o_b={d_o_b}&___dsq_courses={dsq_courses}&___dsq_ft_pt={dsq_ft_pt_raw}&___dsq_ug_pg={dsq_ug_pg_raw}&___applicant_number={applicant_number}&___dsq_disability={dsq_disability_raw}&___dsq_disability_details={dsq_disability_details}&___dsq_support_types_needed={dsq_support_types_needed}&___dsq_uoh_accomm={dsq_uoh_accomm}&___dsq_uoh_accomm_any_reqs={dsq_uoh_accomm_any_reqs}&___dsq_uoh_accomm_reqs={dsq_uoh_accomm_reqs}&___dsq_further_info={dsq_further_info}&___dsq_confidentiaity_share=Yes" style="color:#990000">Click here if you wish to resubmit your questionnaire changing this.</A>';
        }
    ?>
    Returns:
    strcmp:
    Code (Text):
    <?php
    $confidentiality = '{dsq_confidentiaity_share}';
        echo '<BR><i>DEBUG INFO: - strcmp version<BR>';
        echo '{dsq_confidentiaity_share}';
        echo '<br> that was the fabrik variable, now echoing the PHP Variable:<br>';
        echo '$confidentiality';
        echo '<br> and now echoing the PHP Variable without quotes<br>';
        echo $confidentiality;
        echo '<br>and that is what kind of value?<br>';
        echo gettype($confidentiality);
        echo '<br><br></i>';
     
        if (strcmp($confidentiality,'Yes') == 0)
        {
        echo 'You gave us permission to share information related to you and your support needs with people who, in the course of their work, have a valid reason to be informed.';
        }
        else
        {
        echo 'You did not give us permission to share information related to you. As a result we may be unable to meet all of your support needs. <BR><BR> <a href="https://sss-vm.hull.ac.uk/webforms/index.php?option=com_fabrik&view=form&fabrik=8&___first_name={first_name}&___family_name={family_name}&___DSQ_Address={DSQ_Address}&___DSQ_Telephone={DSQ_Telephone}&___DSQ_Email={DSQ_Email}&___d_o_b={d_o_b}&___dsq_courses={dsq_courses}&___dsq_ft_pt={dsq_ft_pt_raw}&___dsq_ug_pg={dsq_ug_pg_raw}&___applicant_number={applicant_number}&___dsq_disability={dsq_disability_raw}&___dsq_disability_details={dsq_disability_details}&___dsq_support_types_needed={dsq_support_types_needed}&___dsq_uoh_accomm={dsq_uoh_accomm}&___dsq_uoh_accomm_any_reqs={dsq_uoh_accomm_any_reqs}&___dsq_uoh_accomm_reqs={dsq_uoh_accomm_reqs}&___dsq_further_info={dsq_further_info}&___dsq_confidentiaity_share=Yes" style="color:#990000">Click here if you wish to resubmit your questionnaire changing this.</A>';
        }
    ?>
    Returns:
    I'm stuck as it seems to be defying logic o_O
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Just do ...

    PHP:
    if ((int)'{dsq_confidentiaity_share_raw}' == 1)
    {
        echo 'You gave us permission to share information blah blah.';
    }
     else
    {
        echo 'You did not give us permission to share information related to you blah blah';
    }
    -- hugh
     
  7. jimkeane

    jimkeane Member

    Level: Community
    Thanks Hugh, but this always returns the else statement.


    I echoed the values to clarify this:
    PHP:
    if ((int)'{dsq_confidentiaity_share_raw}' == 1)
    {
        echo 'You gave us permission to share information blah blah.';
    }
    else
    {
        echo 'You did not give us permission to share information related to you blah blah';
    }
    echo '<BR><BR>';
    echo 'This is being returned for dsq_confidentiaity_share_raw as an integer: ';
    echo (int)'{dsq_confidentiaity_share_raw}';
    echo '<BR><BR>';
    echo 'This the placeholder value for dsq_confidentiaity_share_raw : ';
    echo '{dsq_confidentiaity_share_raw}';
    This returns:

    Any ideas?
     
  8. jfquestiaux

    jfquestiaux Well-Known Member

    Level: Community
    Try
    PHP:
    echo (int){dsq_confidentiaity_share_raw};
     
  9. troester

    troester Well-Known Member Staff Member

    Level: Community
    Can you do a
    var_dump($your-variable);
    instead of echo to get some more info?
     
  10. jimkeane

    jimkeane Member

    Level: Community
    I had to enclose the placeholder in quotes or it would not parse the PHP. This is the code I used:
    PHP:
    echo 'And now a var_dump:<BR>';
    $confidentiality = '{dsq_confidentiaity_share_raw}';
    var_dump($confidentiality);
    And this was the output:
     
  11. troester

    troester Well-Known Member Staff Member

    Level: Community
    So it seems your _raw contains 30 characters, e.g. a lot of HTML formatting (something like <span class="xx">1</span>; can you check the page source code of the var_dump page?)
    This is the reason why (int) is 0.
     
  12. jimkeane

    jimkeane Member

    Level: Community
    Thanks troester. This is getting me somewhere!

    I'm guessing the string is reporting as 30 characters as that is the length of the placeholder including the brackets: {dsq_confidentiaity_share_raw}

    I'll do some more experimenting and will post back!
     
  13. jimkeane

    jimkeane Member

    Level: Community
    It seems that the PHP is run before the value is inserted from the place holder, as evidenced:
    PHP:
    echo 'And now a var_dump:<BR>';
    $confidentiality = '{dsq_confidentiaity_share_raw}';
    var_dump($confidentiality);
    echo '<BR><BR>';
    echo 'And now a var_dump of the trimmed variable:<BR>';
    $trim_conf = substr($confidentiality,0,1);
    var_dump($trim_conf);
    Which outputs:

    The output of the second var_dump shows that the PHP is run before the placeholder is replaced with its value.

    I think this is a bug ;)
     
  14. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Try using the full element name.

    During email processing, I thought short element names should work as well, but try the long name (with tablename___ prepended).

    -- hugh
     
  15. jimkeane

    jimkeane Member

    Level: Community

    Sorry Hugh, I don't think that's the issue, I've tested this as follows:

    Following your suggestion I replaced {dsq_confidentiaity_share_raw} with {___dsq_confidentiaity_share_raw}, I'm not storing the data in a table, simply sending it via email.

    I've tried the place holder directly in the code, E.G. if ((int)'{dsq_confidentiaity_share_raw}' == 1) and loaded to a variable ($confidentiality = '{dsq_confidentiaity_share_raw}';). Neither works.

    Data is being passed to the placeholder as a straightforward var_dump of the $confidentiality shows:
    • Short element name returns: string(30) "1"
    • Long element name returns: string(33) "1"
    However if I use substr to trim the contents of the variable and then var_dump it exposes its real contents: string(1) "{"
    Any ideas?
    Jim
     
  16. jfquestiaux

    jfquestiaux Well-Known Member

    Level: Community
    Not saving to the database may be the issue (although Rob did some fixing on recent GitHub).
    I have not tested it in an email template but in a PHP form plugin, this works:
    PHP:
    $app = JFactory::getApplication();
    $extension = $jinput->get('___extension_id', array(), 'array');
    In my form, this code returns an array with the values of my databasejoin element "extension_id".
    So you can try something along this way (Note : with arrays, you have to use "print_r()" instead of "echo" to print the data).
     
  17. troester

    troester Well-Known Member Staff Member

    Level: Community
    I just realized this is Fabrik2 forum.
    So you are using Joomla1.5/Fabrik2?

    Ok, you didn't use var_dump;exit; (this would have shown the problem earlier) but just var_dump into the email.
    Placeholder replacement seems to be done in a later step (on the complete "echoed" string"), you can't use placeholders to calculate in the php file.
    Use $this->data['___dsq_confidentiaity_share']; (or maybe with _raw)
     
    jimkeane likes this.
  18. jimkeane

    jimkeane Member

    Level: Community
    Thank you troester, this go me over the line!

    The final code looks like this:
    PHP:
    $confidentiality = $this->data['___dsq_confidentiaity_share'];
    if ($confidentiality == 'Yes')
        {
        echo 'You gave us permission to share information related to you and your support needs with people who, in the course of their work, have a valid reason to be informed.';
        }
    else
        {
        echo 'You did not give us permission to share information related to you. As a result we may be unable to meet all of your support needs.';
        }
    Putting $this->data['___dsq_confidentiaity_share'] within the if statement did not work, the result needs to be placed in a variable and the value of the variable then queried by the statement.

    Thanks to all who helped with this :)
     
  19. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Oh crap, yes, didn't realize it was a table-less form, and that it was 2.1.

    As for:

    You must have had some kind of syntax error when you tried using the array in the condition, as there is absolutely no reason for that to be the case. Of that I am 100% sure, as it's something I do dozens of times a day.

    -- hugh
     

Share This Page