how to fill php date from javascript keypress event -
i want add automatically dates php array using javascript keypress event. here's part of code:
date array:
$period = new dateperiod(new datetime("2015-07-03"), new dateinterval('p1m'), new datetime("2018-07-09")); foreach ($period $date) { $dates[] = $date->format("m y"); }
text input:
<input type="text" id="jumlah" size="3" maxlength="3" onkeyup="myfunction()" onkeypress="return hanyaangka(event)"/> <p id="demo"></p>
i want echo
july 2015 if input 1, august 2015 if input 2, etc..
how can that?
this i've tried, it's not working:
<script> function myfunction() { var x = document.getelementbyid("jumlah").value; var i='<?php echo $dates[x];?>'; document.getelementbyid("demo").innerhtml = i; } </script>
the problem try mix server side , client sides variables.
var x = document.getelementbyid("jumlah").value; var i='<?php echo $dates[x];?>';
x
javascript variable, can not use in php. <?php echo $dates[x];?>
won't know, x
on client side.
you need use ajax. send x
parameter ajax.php
, , returns propery item of array.
something this:
$('#jumlah').on('keyup', function() { $.get('ajax.php', {x: $(this).val()}, function(response) { $('#demo').html(response) }); });
in php
$period = new dateperiod(new datetime("2015-07-03"), new dateinterval('p1m'), new datetime("2018-07-09")); foreach ($period $date) { $dates[] = $date->format("m y"); } echo $dates[$_get['x']];
Comments
Post a Comment