• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Passing data from PHP to JS in PHP code list plugin

Status
Not open for further replies.

bascherz

Member
I have a table that contains user ids for which I can easily write PHP to do a row-by-row join query to get an array of email addresses. The PHP part I can handle. And I can prepare the entire "mailto:" link that I want to send to JS so it will do window.open(themailtolink).

So, how do I get that PHP variable's data into the JS in the plugin? Does the PHP execute before the JS or vice versa? From reading the help, it seems the JS is run at the end. Good.

So just envision in the PHP code, this has happened:
Code:
$mailtolink = "mailto:username@example.com?subject=Subject&body=message%20goes%20here";

Now I want to do this in the JS area:
Code:
windows.open(mailtolink);

How do I get that string from PHP to JS? I have tried outputting the JS with PHP embedded in an echo statement in the PHP code, but that didn't work.

Thanks anyone in advance. I'm stuck.
Bruce
 
Where is your PHP placed, and where your JS? Unless in the same script, or the PHP variable is a global one, it will likely not work, indeed.
https://www.w3schools.com/php/php_variables_scope.asp

Once that's sorted, you should indeed be able to place it in JS with <?php echo $variable; ?>

P.S.: Are you really using old J! 2.5 and Fabrik 3.0 since you're posting in this forum?
 
Oh, I didn't realize I was posting in a J!2.5 forum. No, I always have the latest of everything on my sites.

Thanks for the hint. I'll give it a try.
 
Well, I would have assumed the PHP and JS in the same PHP form would be output to the same page and this would work, but it doesn't. I've tested my PHP code in a non-Fabrik command line environment and the code is producing a proper "mailto" link. And I've verified I am properly referencing the element in the list passed into the PHP form via the JS console. But when I put all that together into the PHP section and try to output the Javascript from the PHP code, I get nothing. And when I try doing what lousyfool suggested, a browser window opens with "<?php echo $mailtolink; ?>".

So I don't keep getting the message across incorrectly, here's what I've tried.

PHP:
global $mailtolink;

// Get the current user's record
$user = JFactory::getUser();
$mailtolink = "mailto:".$user->email."?subject=".rawurlencode(htmlspecialchars_decode("You have been matched!"))."&bcc=";

// Need database object to get info of OTHER users whose IDs are in this table
$mydb = JFactory::getDbo();
$recipients = array();
foreach ($ids AS $myid)
{
    $row = $model->getRow($myid);
    $mamaid = $row->requests___matchmama_raw;
    $mydb->setQuery("SELECT email FROM #__users WHERE id=".$mamaid);
    $mamaemail = $mydb->loadResult();
    if ($mamaemail)
    {
        if (array_search($mamaemail,$recipients) === false)
        {
            $recipients[] = $mamaemail;
        }
    }
}
$mailtolink .= implode(";",$recipients);
?>

I've tried outputting the JS in the above PHP code with this right after assigning $mailtolink.

PHP:
echo "<script type='text/JavaScript'>
     window.open('$mailtolink');
     </script>";

But that seems to do nothing at all. The pop-up comes up, which usually means the PHP didn't have an error.

And I've also tried (instead) putting the following in the JS window.

JavaScript:
window.open('<?php echo $mailtolink; ?>');

The latter causes the browser window to open with that exact text in the address bar.

I can enter this in the JS window as verification the email client does, indeed, launch with things in the right places.

window.open('mailto:someaddress@somedomain.com?subject=EncodedSubject&cc=myemail@mydomain.com');

I just cannot seem to get the correctly-formatted $mailtolink string to get into that window.open call.

What am I missing?

Thanks in advance. As you may tell, Javascript is not my strongest language. I do apologize for that.

Bruce
 
Last edited:
Where is this PHP code located? In a form plugin? In the element advanced tab? This shouldn't be that hard, I insert JS all the time into my forms.
 
The PHP is right in the form as is the JS. But again I am no Javascript coder and I certainly don't know how that PHP and JS code in the same form can share data. Could it be a template issue? I am using Gantry 5.
 
Few things are wrong in your code.
And, unless I missed something, you still haven't said where exactly you are using/placing your code.

First, if you're having it all in the same script (e.g. a custom form template), there's no need for a global variable.
So then, please try this:
Code:
// Get the current user's record
$user = JFactory::getUser();
$mailtolink = "mailto:".$user->email."?subject=".rawurlencode(htmlspecialchars_decode("You have been matched!"))."&bcc=";

// Need database object to get info of OTHER users whose IDs are in this table
$mydb = JFactory::getDbo();
$recipients = array();
foreach ($ids AS $myid)
{
    $row = $model->getRow($myid);
    $mamaid = $row->requests___matchmama_raw;
    $mydb->setQuery("SELECT email FROM #__users WHERE id=".$mamaid);
    $mamaemail = $mydb->loadResult();
    if ($mamaemail)
    {
        if (array_search($mamaemail,$recipients) === false)
        {
            $recipients[] = $mamaemail;
        }
    }
}
$mailtolink .= implode(",",$recipients);

echo "<script>
     window.open(".$mailtolink.");
     </script>";

Please note:
- I haven't touched the loop and query... trust you have that working or know how to fix it if not.
- Multiple email address in "mailto" should be separated by commas.
- You had a syntax error in the echo line.
- You can use the usual methods to debug your PHP.
- You can also verify the output of the variable inside the script tag by viewing the page source.
- If, for some reason, you need to use global PHP variables, please do so correctly, including when accessing them.
 
My code is entered in the PHP section of the PHP form plugin. I am pretty sure I said that before, but if not now I have. Below is what that looks like. There's a JS section below this editing area. Before I started writing any code here, I verified a hard-coded mailto string would properly launch Outlook if issued from the JS window, but it does not work as you have prescribed here (I tried that first, actually). That is why I have been trying to get the $mailtolink passed into the JS section below this PHP section.

When doing what you suggest Outlook is not launched. The front end refreshes, but no Outlook window opens. But if I enter that mailto link in a window.open call in the JS window, that does launch Outlook.

Thanks for your suggestions so far!

PHPpluginScreenshot.png
 
My code is entered in the PHP section of the PHP form plugin. I am pretty sure I said that before, but if not now I have.
Sorry but, nope, you had not said where exactly you had placed your code, and certainly you had not mentioned "PHP form plugin" (where the next question would have been at which trigger). There's a hint on PHP list plugin in the thread's subject line but later, when asked, you're clearly referring to "form", more than once and most recently as quoted here, though not exactly where in the form.

Anyway... obviously, your screenshot shows a PHP list plugin (not form). So, I'm still not sure -- now actually rather confused -- when/where/how you want your code to execute.

If it is a list, and the PHP list plugin, please see the Wiki and the plugin field tooltips. And then, in the "JS Code" field, please enter just that: JS code. No <script> tags etc, and e.g. PHP variables properly inserted as usual in JS, like <?php echo $variable; ?>

However, still hard to say more, or if you're on the perfectly right way.

BTW, where is $ids defined?
 
I've read every wiki help page on this interface and none of it really answers the issue I am having. Echoing a variable in the JS box of the list plugin code section like you suggests literally outputs that string to the address box of a new browser window. It doesn't pick-up the $mailtolink variable set in the PHP code window. I literally see <?php echo $mailtolink; ?> in the browser address box. And when I put it in the PHP code as you see above, the code executes but the email client window doesn't open.

The $ids and $model variables are provided by Fabrik in this interface. That part of the code I have verified using the JS console in Chrome. It is correctly fetching the data from the rows selected in the list. The list contains user IDs that I have to query the #__users table from which to get the email addresses. I've even used the JS console to show I am getting those email addresses. I've even verified that complete mailto link. I just can't get it to come out on the page and launch the email client.

By the way, this code executes when you select some rows and click the itty-bitty button by the X in the upper right end of the list heading. That part works fine. Output is the problem.
 
Ok, so the PHP list plugin it is.

Before anything else:
The $ids and $model variables are provided by Fabrik in this interface.
I haven't tried it, but according to Wiki these lines should provide $ids:
Code:
$app = JFactory::getApplication();
$ids = $app->input->get('ids', array(), 'array');
I don't see them in your code... but that's likely irrelevant... because:

If you read the Wiki (and the "JS Code" field tooltip) -- again, carefully -- it says:
JS code - Any JavaScript code code entered here will be run when the plugin button is clicked (at the end of this plugin buttonAction() method).
If your code returns false, the button will not fire the request to run the PHP code.
Means:
1. The click correctly executes your JS and opens a new browser window with the expression in the parentheses as URL. So, the plugin does what it's supposed to do.
2. The PHP code is run after the JS.

So, the PHP list plugin works correctly as described and, hence, however your previous assumptions originated, you need to approach this differently.
 
You’re right about $ids. That code used to be in there. And I did not read that the PHP code runs after the JS. That means there’s no passing of data from PHP to JS.

But let's focus on the actual problem instead of the code I've been hacking around with for a week. I’ll repeat something I am absolutely certain I’ve said on here before. When I first started looking into this, I just tried outputting a hard-coded mailto link from both the JS code and the PHP code in that plugin. It only worked when outputting it from JS, the same exact link in both tests, of course with <script> tagging in the PHP. So I am wondering if this will ever do what I need it to do.

I just tried outputting the link from the PHP code again and nothing is output. Below is now the only line in the list's PHP plugin code:

Code:
echo "<script type='text/javascript'> window.open('mailto:myemail@gmail.com?subject=You%20have%20been%20matched!'); </script>";

Yet putting this all by itself in the JS code window with no PHP code does, indeed, launch Outlook.

Code:
window.open('mailto:myemail@gmail.com?subject=You%20have%20been%20matched!');

This is the problem I need to solve.
 
(...)
I just tried outputting a hard-coded mailto link from both the JS code and the PHP code in that plugin. It only worked when outputting it from JS
(...)
putting this all by itself in the JS code window with no PHP code does, indeed, launch Outlook.

The Wiki and I haven't said anything different than you here, so we're all right: it's the JS being executed on click and, with what you're doing, putting whatever into a browser URL, no matter if it's a text string in form of a PHP expression/variable, or a hardcoded "mailto:..." link.

Again: "JS code - Any JavaScript code entered here will be run when the plugin button is clicked." And that's what it is doing... it does exactly what you're telling it to do.

So, yeah, this plugin may not be able to do what you want it to do. Neither is the email list plugin, I reckon. Even Fabrik can only do so much out of the box. For your very specific needs, maybe you need to start thinking of a custom solution, inside a custom list template, or in form of a custom plugin, or so.
 
Makes me wonder why there's a PHP code block in here. And again, I've tested an earlier version of this PHP code (via the browser debug console) that did in fact run from this plugin, so it's getting executed.
 
You're right again: of course, the PHP does get executed, see examples in the Wiki. Fine e.g. for updating/inserting DB data, just not for providing an href for the button(s) or so.
 
Seems odd that PHP code that runs would not output to the same DOM (did I use that term correctly?). Just seems like this should be possible. I mean, sending emails from this type of list just seems like one of the reasons a plugin like this would exist. For now it will have to suffice to send an email from the site to the user with a list of email addresses they need to copy/paste into the TO field of a message they compose themselves. Sigh... Thanks for your engagement on this. Maybe I'll eventually solve it and post back.
 
When all else fails you might look at RegularLabs ReReplacer to scan for the button and insert the onclick event that you want. Some things are just easier using brute force at times.
 
If it were only that simple...
ReReplacer will be of no help here either, I fear.

The OP has something more complicated in mind:
1) Make a selection of Fabrik list rows, or have it applied to all rows, possibly depending on (pre-)filters: JS (= browser).
2) Some query in another DB table based on selection. PHP (= server).
3) Query results as part of the button "action" (since it's "mailto:...", best as href): JS (= browser).

Again, and as far as I know, not something that either Fabrik PHP list (or other) plugin or RL ReReplacer or anything else can do out of the box.
Sure, would be nice "if", and I'd happily stand corrected. :)

------------------------------------------------------------
The only other thing I can think of (but not knowing everything about OP's setup, I can't know for sure):
- Join #__users with the main list (publish only needed #__users field and hide them from being displayed, of course).
- Generate the button link entirely in the JS Code field of the PHP list plugin.
No need for PHP then, obviously.
One key question being if the OP could live with this needed #__users data somehow being in the HTML (even if hidden... privacy?).
 
Hmm... I actually thought about that. It's a secure (SSL-protected) site and, of course, only site members with certain elevated access can even get to this table. Might be worth investigating. It would be awesome if I can get this to work.

That said, not being Javascript literate myself, how do I get what would be a list of email addresses from the JS code block into a mailto link? Sorry, I really should take a course on Javascript. I've only reused code I've googled and, I can usually understand what it does. But I lack the basics to do anything from scratch myself. I'm working on it...
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top