codebugs10840
codebugs10840

Hi All,

Is it possible to retrieve and send an input filed value in data-request-data attibute?
For example:
data-request-data:"var: $('#sample').val(), other: 'xyz'"

Thanks in advance for your help!

mjauvin
mjauvin

All form fields values should be available to the AJAX handler with this syntax:

$var = post('var');
mjauvin
mjauvin

Otherwise, you can pass static values/dynamic values as you described. It's all explained here:

https://octobercms.com/docs/ajax/attributes-api#data-attributes

codebugs10840
codebugs10840

mjauvin said:

Otherwise, you can pass static values/dynamic values as you described. It's all explained here:

https://octobercms.com/docs/ajax/attributes-api#data-attributes

Hi mjauvin,

Thanks for trying to help but actually only the posted form fields are available by post('var').
I won't be able to retrieve any field value until it not posted to the AJAX request - but the field I would like to get is not available in the request due to it is not in the (same) form that I am posting...also I am not using JavaScript code to call request (I know I could achieve what I want by JavaScript by I would like to use DATA attributes.

Ps.: There is no a single word in the documentation about how to post "dynamic" (mean query selector) in data attributes called data-request-data.

mjauvin
mjauvin

you could use the "data-request-form" attribute to get all fields from the other form, but I never tried this. You probably wouldn't get the current form's data, though.

mjauvin
mjauvin

Otherwise, use the JavaScript API to get more control.

codebugs10840
codebugs10840

mjauvin said:

you could use the "data-request-form" attribute to get all fields from the other form, but I never tried this. You probably wouldn't get the current form's data, though.

Hi mjauvin,

Thanks for help. This "data-request-form" attribute worked fine - however I actually did not use any form on the page (I have different input fields on different places) so I have created an empty form and then assigned the fileds to it with form="formname" attribute on input elements then set data-request-form attribute on button and it worked! Thanks for the idea.

Ps.: I know JavaScript API would be prettier and simpler but I try to reduce JS codes...

PennyLliang
PennyLliang

In my experience, you can just add name attribute in the tag, like this:

<input name="var" type="checkbox" data-request="onSwitchStatus"/>

Then in onSwitchStatus function, you can just use post('var') to get the input value.

1-8 of 8