8 years ago
Topic

Hi there,

I´m new to Seblod but I´m loving to learn It. 

Now I am trying to adapt to Seblod an Ajax cascade code between state and city fields  I originally have done to use with breezing forms.

I put the code in JS of a state field. Everything aparently worked well, BUT, I don´t know why, when I submit the form It do not stores in database the content of city field. In the form everything works well when I change the State, a new list of cities appears in the city field, but It is not stored on submission.

Am I missing something to make it work? 

Thanks for any help.

Get a Book for SEBLOD
4229 Posts
Kadministrator
8 years ago
2
Level 1

Hi,

I don't know how your script works, but keep in mind you can only change selected values or filtler down options on existing fields using javascript, you can't add new fields to form or add options to select.

8 years ago
1
Level 2

Hello Klas,

Thanks for you fast response! 

My javascript code use jquery to clear the city select field options, then populates it again with the new options (based in the state field choice). You said I can´t do that in your answer, so I created another text field that receives the city select option value as a turn around. It is not elegant but works. 

Please, let me know if you have a better advice.

Thanks in advance

4229 Posts
Kadministrator
8 years ago
0
Level 3

What I have done in the past is to have all possible options in the second cascade, then remove those not matching, based on the choice from the first select. Ofcourse this would only work when there is a reasonable amount of possible options.

8 years ago
0
Level 1

Thank you Klas for your help!

I will let the steps solution I took if someone else need something like that:

For this solution I used BeforeRender plugin and changed Dynamic select field on the fly as I need in the server side (I am not a programmer, so I´m not using "the best practices", but just the shortest way I found).

1) I discovered that $fields['mydynamicfieldname']->form gives me a string with all html markup of the dynamic field like this:

<select id="est_city" name="est_city" style="width:268px; height: 34px;"> // First Line
<option value="">- Select-</option>
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>


2) I took this string and extracted the first line of It using these php lines:

$options=$fields['est_city']->form;
$options = substr($options, 0, strpos($options, "<option")-1) // this line extracts the very first line from the string above



3)Then I make a simple sql statement to retrieve the new content to the dynamic field from my custom database:

$namestate=$fields['est_state']->value;
$selected=$fields['est_city']->value;
//If $namestate is empty, I will use default state as "SP" and default city as "São Paulo"
If ($namestate==''){
$namestate='SP';
$selected='São Paulo'; }
$db = JFactory::getDbo();
$query = "Select name_cidty From #__cities Where name_state='".$namestate."'" ; // retrieves all cities from specific state in the state field of your form
$cities= $db->loadColumn();


4)Use a foreach php loop to complete the string with all new options you need:

$options .= '<option value="">- Select -</option>'; // put a "Select" label to the string - Second line
foreach ($cities as $city) {
if ($city<>$selected){
$options .= '<option value="'.$city.'">'.$city.'</option>'; }
else { $options .= '<option value="'.$city.'" selected="selected">'.$city.'</option>';}
}
$options .= '</select>' ; // Last line closing the tag
$fields['est_city']->form=$options;



Hope It helps someone!This solution makes the dynamic storage works. No need to use auxiliar fields.

EDIT: Very important! To all this code work the dynamic city field MUST load originally ALL city options, like suggested by Klas!

Get a VIP membership