javascript - how to pass jquery date parameters in php -
please me.. have following codes..
//get value of start , end of week $('#weeklydatepicker').on('dp.change', function (e) { var value = $("#weeklydatepicker").val(); var firstdate = moment(value, "mm/dd/yyyy").day(0).format("mm/dd/yyyy"); var lastdate = moment(value, "mm/dd/yyyy").day(6).format("mm/dd/yyyy"); $("#weeklydatepicker").val(firstdate + " - " + lastdate); });
now want pass firstdate , lastdate php. how should this. , how can retrieve passed value in php. i'm not familiar javascripts. hope can me. in advance.
here full code.. js tags
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" rel="stylesheet" <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
html
<div class="input-group" id="datedemo"> <input type='text' id='weeklydatepicker' placeholder="select week" class="form-control" /> <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
js
$("#weeklydatepicker").datetimepicker({ calendarweeks:true, format: 'mm/dd/yyyy' }); //get value of start , end of week $('#weeklydatepicker').on('dp.change', function (e) { var value = $("#weeklydatepicker").val(); var firstdate = moment(value, "mm/dd/yyyy").day(0).format("mm/dd/yyyy"); var lastdate = moment(value, "mm/dd/yyyy").day(6).format("mm/dd/yyyy"); $("#weeklydatepicker").val(firstdate + " - " + lastdate); alert(firstdate); $.post("sample1.php",{"fdate" : firstdate ,"ldate" : lastdate}); }); $('#datedemo').on('dp.change', function (e) { var kk = $("#weeklydatepicker").val(); $("#output").html( " week number: " + moment(kk, "mm/dd/yyyy").week() + " of " + moment(kk, "mm/dd/yyyy").weeksinyear() ); });
sample1 code
<?php echo $_post["fdate"]; //for firstdate echo $_post["ldate"]; //for lastdate ?>
i use $.ajax()
function, let handle error status well, because both practice handle errors users , indicator of going wrong script.
$.ajax({ url: 'test.php', type: 'post', data: {fdate : firstdate ,ldate : lastdate}, datatype: 'text', beforesend: function(){ // initiate spinner or loading infobox // inform user ajax request }, success: function(response){ alert(response); // may use result standard argument // , call or trigger other functions according value }, error: function(jqxhr, textstatus, errorthrown){ alert(textstatus + ': ' + errorthrown); // call error handler or nothing }, complete: function(){ // close spinner or loading box } });
in php file, variables $_post['fdate']
, $_post['ldate']
. once complete query , results, echo
response that:
echo 'success';
or
echo 'failure';
according result of query. may use string, not success or failure. don't forget handle in success
part of $.ajax()
function , not error
part of it. error
part whatever goes wrong request (e.g. file not found or server error).
well, here tricky part: if thing want return status, may use datatype: 'text'
in $.ajax()
function did in example. however, if want return object useful data, such has gone wrong php query or has been accomplished php query, you'd better use datatype: 'json'
, json response. in such case, end of php script change too.
$result = ['statuscode'=>1,'details'=>'some details according query here']; echo json_encode($result);
the success handler of $.ajax()
function object argument (i.e. response
object) can use this:
alert(response.statuscode); // alert 1
Comments
Post a Comment