Welcome Guest, Not a member yet? Register   Sign In
HTML data-* attributes containing JSON using form_*
#1

[eluser]Aeisor[/eluser]
I'm trying to create a form where some elements have data-settings attributes to be used in a JS widget. The settings are stored in a JSON string, such as
Code:
{"events":null}

Using form_input, the attribute's value is surrounded by double quotes, which breaks the string. Escaping doesn't seem to help.

Code:
form_input(array('name'=>'name', 'value'=>get_cookie('filters-customers-name'), 'data-settings'=>json_encode(array("events"=>NULL))));

Code:
form_input(array('name'=>'name', 'value'=>get_cookie('filters-customers-name'), 'data-settings'=>'{"events":null}'));

The output from these is something similar to this in the browser (note that data-settings is closed as "{", then the rest is garbage.

Code:
<input type="text" name="name" value="" data-settings="{" events":null}"="">

I would like to keep the creation of HTML elements to these functions, as I believe the values are cleaned. Short of changing the Form Helper, can this be done?

CodeIgniter: 2.1.3

Thanks in advance.

[Edit] I've just checked the Form Helper and it only appears to htmlspecialchars, then str_replace single and double quotes for their entities. That's making me think that simply writing the HTML for the inputs is going to be a lot less hassle.
#2

[eluser]Aeisor[/eluser]
Slept on it. I can either:

Code:
form_input(array('name'=>'id', 'value'=>get_cookie('filters-customers-id'), 'data-settings'=>'{"events":null}'));

or

Code:
form_input(array('name'=>'id'), get_cookie('filters-customers-id'), " data-settings='{\\"events\\":null}'");




Theme © iAndrew 2016 - Forum software by © MyBB