weixin_33738555 2019-07-21 22:20 采纳率: 0%
浏览 32

将ajax变量传递给php

I'm using fullCalendar and I want to use the start variable from the "select" callback as a PHP variable.

This is my JavaScript:

select: function(start, end) {
  $('#ModalAdd #start').val(moment(start).format('YYYY-MM-DD '));
  $('#ModalAdd #end').val(moment(end).format('YYYY-MM-DD '));
  $('#ModalAdd').modal('show');

  var start = val(moment(start).format('YYYY-MM-DD ')) ;
  $.ajax({
    type: 'POST',
    data: {start:start},
  });
},

This is my PHP script :

<?php
if( isset($_POST['start']) ){
  $start = $_POST['start'];
  echo json_encode($start);
}
else echo "string";
?>

This is my HTML input:

<input type="text" name="start" class="form-control" id="start" readonly>

but I get "string" as a result.

  • 写回答

2条回答 默认 最新

  • weixin_33696822 2019-07-22 11:54
    关注

    Echo the $['start'] so you see what it returns first. you can format strings in php to date using the following formats

    $time = strtotime('10/16/20019');
    
    $newformat = date('Y-m-d',$time);
    echo $newformat;
    

    or in your case $time = strtotime($_POST['start']);

    $newformat = date('Y-m-d',$time);
    echo $newformat;
    
    评论

报告相同问题?