Run js in php on calc plugin

Status
Not open for further replies.

redant

Member
Hi

Please can someone explain how to run a xx/xx.js file from within php in the calc plugin. It seems the key I am missing is in this wiki entry http://fabrikar.com/wiki/index.php/Form_and_element_javascript but that link is broken.

Although I understand the basics of this from php procedural development, I just cannot get it to work. Possibly my syntax is borked ...

Essentially what I am trying to do is call a small js script from within php on the calc element.

Guidance would be appreciated!
 
Last edited:
Well, first thing is that php runs on the server and javascript runs in the browser so you don't run javascript inside php.

What is it you are trying to accomplish?
 
I recently added the ability when doing "AJAX calc" to embed a script tag in the returned calc value, which the calc element's JS will execute whenever it updates the calc.

I added it specifically for someone who was using the calc element to produce 'plotalot' graphs, which she wanted to update on the fly when altering dropdown selections. The problem was that although we could generate the plotalot plugin code from the calc, it also required the plotalot evalScripts() function to be run if the graph was changed on the fly.

So by allowing the calc response to include ...

Code:
$response = "all the other visible stuff for the plotalot graph";
$response .= '<script type="text/javascript">evalScripts();</script>';
return $response;

... after the visible part of the returned calc value, and having the calc element JS look for and run any embedded script tags, we were able to achieve it.

So ...best suggestion is put the bulk of the code you want to run wrapped in a function in your form_X.js file, then just call that function from a script tag embedded in your calc value.

However, this may not be the best way to achieve what you need to do, so do explain in more detail what you are trying to achieve.

And yes, that's a very old and out of date wiki link, the new one is ...

http://fabrikar.com/forums/index.php?wiki/javascript/

-- hugh
 
Thanks, Hugh

Your explanation provides the missing piece of the puzzle...

Essentially what I have been trying to achieve is:
I have 2 dropdowns on a form.
Dropdown 1 = From
Dropdown 2 = To
Using the selection from those 2 forms I need to calculate the driving distance between the 2 points using the Google distance matrix ( https://developers.google.com/maps/documentation/distance-matrix/start ) I then want to use the result to populate a hidden field and update the database record with that value when the form is submitted.

@achartier - Thanks, yes I do know the difference between php and js. Sorry. I guess I should rather have said "include a JavaScript function call in my output."

Thanks for the help guys. Appreciated as always.
 
Last edited:
I wouldn't use a calc element for that, I'd create a custom form_X.js file, put a function in that, and call it from the 'onChange' event of the from and to elements.

Although of course if you need to grab other data about the from/to from the server, it might make sense to use a calc.

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top