Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript变量获取php会话_Javascript_Php_Jquery_Ajax - Fatal编程技术网

从javascript变量获取php会话

从javascript变量获取php会话,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我在php网站上使用JQuery日期和时间选择器。我想将javascript变量保存为一个php会话,我已经在这个网站上查看了以前的答案,并尝试了一些建议,但它似乎对我不起作用。谁能告诉我我错过了什么 这是我的jquery日期和时间选择器,用于获取选定的日期和时间以及发布ajax: <input type="text" name="date2" value=""> <script type="text/javascript"> $(function(){ $('

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

非常感谢您的帮助

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

尝试:

<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>
这行吗?如果没有,控制台中的输入值是多少?

新答案:

HTML

PHP backend.PHP

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

 echo $_SESSION['input'];
 ?>

您有任何错误吗?是的,对不起,当我打开backend.php页面查看回显消息时,它说它无法识别$_POST['input'];在单击范围之外声明输入变量。。然后在单击范围内更改它的值;以及jQuery.ajax之后缺少的一个;对不起,我不知道你的意思,把第二个脚本和ajax帖子放在按钮后面,并与按钮点击功能分离?抱歉我不太喜欢这个我试过了,我收到的唯一错误是相同的:未定义的索引:input1在C:\wamp\www\gp\backend.php的第3行pologies上,我只是更改了:data:{'input1':input},为了确保错误发生在posted变量上,而不是会话变量上。那么,输出console.loginput是什么呢?我在backend.php页面上没有看到除了这个错误以外的其他错误吗?我是说在您的页面中控制台进行ajax调用。data:'input='+data,?不应该是数据:{input:data},好吧,我承认数据是变量名的糟糕选择。但是,像这样发送“输入=”+日期是完全可以的。我将把变量名改为date。为什么会有否决票?难道$.ajax中的数据属性不是一个对象吗?你们能像这样把绳子传过去吗?它非常好用。如果你有疑问,你应该试试。例如,使用jqueryserialize方法将实际创建一个类似以下foo=bar&foo1=bar1的字符串。非常公平,感谢您的提醒。刚刚投票表决:
$(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
 session_start();
 $_SESSION['input'] = $_POST['input'];

 echo $_SESSION['input'];
 ?>
<!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>
$('.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.
                }
           });
      });