Forums

JavaScript and php

chriso0258 16 Sep, 2015
Hello,

Is it possible to use a javaScript value in php code? I have 3 drop down menus.
Drop down 1 is dynamic and pulls three values from a table. Base on which selection is made in drop down 1, drop down 2 loads the options from another table using the following code:
<?php
$siteoptions = array();
if ( !$form->data['sites'] || count($form->data['sites']) < 1 ) {
  // no result was found
  $siteoptions[] = 'Please select a site';
} else {
foreach ($form->data['sites'] as $d ) {
    $siteoptions[$d['site_id']] =  $d['site'];
    }
}
echo json_encode($siteoptions);
?>

I want my third drop down to load its options from another table based on the selection of drop down 2. However, as I'm very new to JS and see that a function called json_encode is actually filling in the options, I will need the value of the selection passed to the db read in order to get the options for the third drop down.

Is this even possible or do I need to remake my forms to limit only two dynamic drop downs?

Thanks for any insights.
Chris
GreyHead 20 Sep, 2015
Answer
Hi Chris,

I don't quite follow the logic here. It is possible to create a triple drop-down, though sticking to two can be simpler.

All of your code here is PHP. The JavaScript is used in the browser to send back a request to the new event in the browser. The PHP in the event is used to read the data that has been sent, look up the new values, and then to send a set of options back to the browser.

The data passed back is automatically added to the $form->data[''] array as you can see in the $form->data['sites'] in your example. ChronoForms sends all of the current form data so you should be able to pick out what you need.

What is actually sent is an array of data that will be used to build the options, when it is sent it is packaged in a standard way called JSON encoding. The json_encode() method does that packaging seethe tutorial here for more info.

Bob
chriso0258 21 Sep, 2015
Thanks for your explanation Bob 😀. I was able to create the third drop down. One other question, in the picture below you will see that the third drop down menu says "Please select a building". To show a list of buildings for BCCX-1 I have to first select another site besides BCCX-1 (those building options come up just fine), then re-select BCCX-1.
[attachment=0]third selection.jpg[/attachment]

Can you think of a way to either add the empty selection of "Please select a site" to the site drop down? I've tried adding this option to the region drop down event in the designer view but only hide, show, enable, and disable seem to have any affect. If I choose the "set options" option and put in Select a site, nothing happens.

The other possibility would be to have the building options default to BCCX-1 selection but I'm not sure how to do this if it can be done.

Any suggestions?

Thanks again for your explanation. That really cleared up what was happening.

Chris.
chriso0258 21 Sep, 2015
Wow, I need to post here more often. I racked my brains out over the above question and then no sooner posted than I figured out a solution.

Thanks.

Chris.
This topic is locked and no more replies can be posted.