我想从js到php获取变量。从$('。form_datetime')(js)到$ TimeFrom(php)。我知道这很简单,但还没有任何效果。我不想更改URL中的参数(POST,GET,$ _ REQUEST)。
<div class="container">
<form action="" class="form-horizontal" role="form">
<fieldset>
<div class="form-group">
<label for="dtp_input1" class="col-md-2 control-label">Time from </label>
<div class="input-group date form_datetime col-md-5" data-date="1979-09-16T05:25:07Z" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
<input class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
<input type="hidden" id="dtp_input1" value="" /><br/>
</div>
</fieldset>
</form>
</div>
<?php
$TimeFrom = 0;
/* Get time from datetimepicker */
echo " My selected time is: " . $TimeFrom;
?>
在脚本中,我需要创建用于传递值的函数。
<script type="text/javascript">
$('.form_datetime').datetimepicker({
language: 'fr',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 1
});
</script>
答案 0 :(得分:0)
除非您发出新请求或制作Ajax,否则您无法将JavaScript传递给PHP以执行某些操作。 JavaScript运行客户端,PHP运行在服务器端。
如果你想自动更改,你可以修改日期选择器的日期选择器,如果日期改变...用web上的jquery更新......
$('.form_datetime').datetimepicker({
language: 'fr',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 1
}).on('dp.change', function (e) {
$('#selected-date').html(e.date);
});
<?php
$TimeFrom = 0;
echo ' My selected time is: <span id="selected-date">';
?>
答案 1 :(得分:0)
也许我解决了这个问题。
<div class="container">
<form action="" class="form-horizontal" role="form">
<fieldset>
<div class="form-group">
<label for="dtp_input1" class="col-md-2 control-label">Time from </label>
<div class="input-group date form_datetime col-md-5" data-date="1979-09-16T05:25:07Z" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
<input onchange="GetDataFrom()" class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span onclick="" class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
<input type="hidden" id="dtp_input1" value="" /><br/>
</div>
</fieldset>
</form>
</div>
<div id="print-date">
<?php
echo "print date: 0";
?>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript" src="./js/locales/bootstrap-datetimepicker.cs.js" charset="UTF-8"></script>
<script type="text/javascript">
function GetDataFrom()
{
var date = $( ".form_datetime" ).datetimepicker("getDate"); //to store it in a variable.
$("#print-date").load("test.php",{SelectedDate: date});
}
$('.form_datetime').datetimepicker({
language: 'cs',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 1
});
</script>
在test.php中
<?php
$SelectedDate = $_POST['SelectedDate'];
echo "print date: : " . $SelectedDate;
?>