从javascript变量获取php会话


getting php session from javascript variable

我在php网站中使用JQuery日期和时间选择器。我想把javascript变量保存为php会话,我已经在这个网站上查看了以前的答案,并尝试了一些建议,但似乎对我不起作用。有人能告诉我缺少什么吗?

这是我的jquery日期和时间选择器,获取选定的日期和时间,并发布ajax:

<input type="text" name="date2" value="">
<script type="text/javascript">
    $(function(){
$('*[name=date2]').appendDtpicker({"inline": true,
"allowWdays": [1, 2, 3, 4, 5], // 0: Sun, 1: Mon, 2: Tue, 3: Wed, 4: Thr, 5: Fri, 6: Sat
"futureOnly": true,
"autodateOnStart": false
});
$('#btn_input').on('click', function(){
    var input = $('*[name=date2]').handleDtpicker('getDate');
    console.log(input);
    jQuery.ajax({
        url: 'backend.php',
        type: 'POST',
        data: {
            'input': input,
        },
        dataType : 'json',
        success: function(data, textStatus, xhr) {
            console.log(data); // do with data e.g success message
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log(textStatus.reponseText);
        }
    });
});
});
</script>
<input type="submit" class="btn" id="btn_input" value="Confirm">

这是我发送到的backend.php:

<?php
session_start(); 
$_SESSION['input'] = $_POST['input'];
echo ($_SESSION['input']);
?>

非常感谢您的帮助!

新答案

HTML

<!DOCTYPE html>
<html>
<head>
<!-- include jquery here -->
<script type="text/javascript" src="jquery.simple-dtpicker.js"></script>
<link type="text/css" href="jquery.simple-dtpicker.css" rel="stylesheet"/>
</head>
<body>
      <input type="text" class="myDatepicker"/>
</body>
</html>

JS-

$(function(){
    $('.myDatepicker').appendDtpicker({ //please note that it requires an element that fits this selector
        'inline' : true,
        'allowWdays' : [1, 2, 3, 4, 5], 
        'futureOnly' : true,
        'autodateOnStart' : false
        'onHide': function(handler){
             $.ajax({
                type: 'POST',
                url: 'backend.php',
                data: 'input='+ handler.getDate(), //the selected value is being sent to your php, where the session variable is set accordingly
                success: function(response){
                     console.log(response); //in case you have any output (e.g. error messages) in backend.php we will output them to the console for debugging purposes.
                     }
                });
            }
        });
    });

PHP(backend.PHP)

 <?php
 session_start();
 $_SESSION['input'] = $_POST['input'];
 echo $_SESSION['input'];
 ?>

完整的脚本通常看起来像:

index.php/index.html

<!DOCTYPE html>
<html>
<head>
<!-- include jquery here -->
</head>
<body>
      <input type="text" class="myDatepicker"/>
</body>
</html>
<script type="text/javascript">
$(function(){
    $('.myDatepicker').appendDtpicker({ //please note that it requires an element that fits this selector
        'inline' : true,
        'allowWdays' : [1, 2, 3, 4, 5], 
        'futureOnly' : true,
        'autodateOnStart' : false
        'onHide': function(handler){
             $.ajax({
                type: 'POST',
                url: 'backend.php',
                data: 'input='+ handler.getDate(), //the selected value is being sent to your php, where the session variable is set accordingly
                success: function(response){
                     console.log(response); //in case you have any output (e.g. error messages) in backend.php we will output them to the console for debugging purposes.
                     }
                });
            }
        });
    });
</script>

请注意,backend.php的路径表明index.php/index.html和backend.php位于同一文件夹中。

原始答案:最初我以为我们在谈论jQuery ui日期选择器。如果有人需要,我会留下这个回复。

这是一种方法…

$('.myElement').datepicker({
     minDate: 0, //dates from today and onwards
     onSelect: function(date){ //"date" will have the selected value of the datepicker
        $.ajax({
           type: 'POST',
           url: 'backend.php',
           data: 'input='+ date, //the selected value is being sent to your php, where the session variable is set accordingly
           success: function(response){
                console.log(response); //in case you have any output (e.g. error messages) in backend.php we will output them to the console for debugging purposes.
                }
           });
      });

是否也应该使用jquery cookies(jquery cookies)。。因此,在您调用ajax并成功后,您将通过传递backend.php获得返回会话,然后将结果存储到jquery cookie中。之后,您可以使用Cookie进行下一步操作。。

尝试:

<script>
jQuery(function($) {
    $(document).on('click', '#btn_input', function(){ // edit here
        var input = $('*[name=date2]').handleDtpicker('getDate');
        console.log(input);
        jQuery.ajax({
            url: 'backend.php',
            type: 'POST',
            data: {
                'input': input,
            },
            dataType : 'json',
            success: function(data, textStatus, xhr) {
                console.log(data); // do with data e.g success message
            },
            error: function(xhr, textStatus, errorThrown) {
                console.log(textStatus.reponseText);
            }
        });
    });
});
</script>

这行得通吗?如果没有,控制台中input的值是多少?