繁体   English   中英

从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 datepicker。 如果有人需要,我将保留此回复。

这是这样做的一种方式...

$('.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 cookie( 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的值是多少?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM