Passing a Lectora Variable to an External HTML Object (PHP)

Another PHP issue.

I have created an External HTML object using PHP. I want to send a Lectora variable to the PHP script for processing. I have associated with the External HTML Objecta a Lectora variable using the Required Variable TAB.

My problem is that I can find no way of accessing the variable using php.

Any thoughts gratefully accepted

Andrew

Discussion (6)

With Javascript it would be simple: VarMyVariableName.getValue();

Using the Var in front of the actual variable name. I suppose something like that will do for PHP too...

Actually that might be the clue.. do not try and send it from Lectora, but get it from PHP...

The only way (I know) to do that is by submitting a form or a test. Just create a form with no form elements. Check "Include ALL variables (Test and Title)" in the Submission settings of the form. In the "Data Submission" group Check "Submit form" "Submit to: Custom Script", then click "Settings" and enter the URL of your script.

You can now send your variables with a "Submit Form" action.

Tim

Lectora variables are normal javascript front-end client-side variables. In order to pass one to a back-end server-side PHP script, you need to submit it via a POST/GET request.

The easiest (as in, writing less code) way to do so is to submit all variable values at once with a form or quiz results (step-by-step). You will just need to provide the URL of your PHP script that processes the request.

Yes I get it but then why have an option for what they call "Required variables". I guess I was just hoping they had built-in some of this so as to say work - especially as I see the variable declaration in the php file BUT in the JS area of the file.

Thank you Tim and Math,

Just to be clear. A so-called "external html object" is actually generated within a lectora file - so it is not really external (i.e. a separate external file though it can include one somehow). Second, it also has a setting for including what they called "Required Variables". I am wondering what that could possible mean. I have identified such a variable for a PHP "external html object". If I inspect the .php file generated by Lectora I can actually see a JavaScript statement defining the variable I want to include.

The PHP component of the file is actually between a undefined statement - but of course I cannot transfer the variable from the JS section to the PHP section.

Maybe I will actually have to do a get from the PHP section (at least simulate one) - or even use a truly external PHP file to achieve what I need to do.

I guess my point is: If it is so difficult to get JS to talk to PHP, what is the point of having the "Required Variables" option for an "external html object". Unfortunately the user manual is not very helpful on this topic.

Thanks again

Andrew

In Lectora Inspire/Publisher, none of your custom variables are created and initiated by default so their values cannot be accessed via JS. You have to do at least one of two things to make Lectora initialize the variables:

a) use them in an action or expression on the page

b) set them as "Required" for an object in the page

I usually choose the latter option because it seems more logical. If scripts in my Ext HTML object use a variable then this variable must be set as Required in that object's properties. It also has a benefit of carrying over from title to title, so if I copy the Object from one title to another, the required variables are carried over with it (I think).

P.S. None of the above applies to Lectora Online where all and any custom variables are initialized on every page by default so you don't have to think about it twice.

Discussions have been disabled for this post