Forums

set a date in Calendar control through button in javascript

gix.vax 30 Jan, 2023
Hi

i have 2 calendar controls for start and end date

i want, through a button, populate both with dates
eg last year
start_date 01/01/0222
end_date 31/12/2022

with this code i populate box but when i send form, value don't go in {data:}

code for my custom button is in a CUSTOM HTML VIEW

<div id="interoAnnoScorsoButton" onclick="interoAnnoScorso()" class="ui button tiny blue ">anno scorso</div>

<?php
$dadata=date(date('Y', strtotime('-1 year'))."-01-01 00:00:00");
$adata=date(date('Y', strtotime('-1 year'))."-12-31 00:00:00");
?>

<script>
function interoAnnoScorso(){
    document.getElementById("dadata").value="<?php echo $dadata; ?>";
    document.getElementById("adata").value="<?php echo $adata; ?>";
}
</script>

fields seems correctly populated but no data when i send form and if i watch firebug i have value="" in calendar control
is right .value assignment for calendar ui control?
gix.vax 30 Jan, 2023
maybe i have to trigger a selectdate?
if yes, there's an easyer way?
gix.vax 30 Jan, 2023
struggling with this

i'm able to set value of my filed with id=dadata but the field that is passed on submit is an hidden filed with name="dadata" and no id
i tryed combinations

document.getElementsByName("dadata")[0].value= "<?php echo $oggi; ?>";

document.getElementsByName("dadata")[0].setAttribute('value', "<?php echo $oggi; ?>");

document.getElementById('dadata').setAttribute('selectDate', "<?php echo $oggi; ?>");

No error in log but i can't populate hidden field called "dadata" and i think is that that is passed on submit

someone can help me?
gix.vax 31 Jan, 2023
FOUND IT!

Values passed in {data:} by calendar are in hidden fields named like your calendar (i use the same id and name in view)
eg. my filed name is dadata, my hidden filed is dadata

the trick is this
- create a custom html element named same as your calendar id and name and call it at the top of your form view
this way

custom html element:
name= my_calendar_fields_hidden_value
<input type="hidden" name="dadata" value="">

form view
{view.my_calendar_fields_hidden_value}
{view.my_buton}
{view.my_calendar}

my_button ustom html element code

<div id="oggiDaDataButton" onclick="oggiDaData()" class="ui button tiny blue ">oggi</div>
<?php
// set a date in php so i can use a mysql date or wathever i want here
$oggi=date("Y-m-d 00:00:00");
?>

<script>
function oggiDaData(){
    // value viwwed in form
    document.getElementById("dadata").value = "<?php echo $oggi; ?>";
    // value in hidden passed by form in {data:dadata}
    document.getElementsByName("dadata")[0].value="<?php echo $oggi; ?>";
}
</script>





With the same method i create start date and end date too, using 2 hidden filed
dadata
adata

SOLVED
You need to login to be able to post a reply.