从javascript变量获取php会话

时间:2015-03-27 18:12:09

标签: javascript php jquery ajax

我在我的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']);
?>

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

新答案

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.
                }
           });
      });

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

尝试:

<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的值是多少?