october cms ajax and options values

840 views Asked by At

Is it possible to send value from options on example like this where x need to be value from option. This select im using on partial and ajax works with manualy added value as x. THX!

<select data-request-data = "id: x " class="form-control custom-select" data-request="onChangeValue">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="15">15</option>
              <option value="20">20</option>

2

There are 2 answers

0
Muhammad Shehroz Sajjad On

Yes you can send value from select. But for this you will need to put it inside a form and then call data request on it. Value will be sent as part of post(). It can't be sent dynamically like the way you are using. if you can elaborate your external conditions more I can suggest other solutions too.

0
Tschallacka On

Just give your select a name, an unique name.

name="some_unique_name"

<select name="my_super_special_name_for_select" class="form-control custom-select" data-request="onChangeValue">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="15">15</option>
              <option value="20">20</option>
</select>

Then when you change the value the value of the field is sent along in post

The result of:

public function onChangeValue() 
{
    traceLog(post());
}

In the log you will see then a result corresponding to

["my_super_special_name_for_select" => 5 ]

So you can fetch it with post('my_super_special_name_for_select') or whatever name you have given the select element to get the value.

public function onChangeValue() 
{
    $id = post('my_super_special_name_for_select');
}