【问题标题】:Set session variable without reloading the page设置会话变量而不重新加载页面
【发布时间】:2020-04-30 04:56:57
【问题描述】:

我用AJAXphp 文件发送一个值。

我在jQuery有这个功能:

// Service select change
$container.on('change', '.bookly-js-select-service', function() {

var gekozen_behandeling = this.value;

  $.ajax({
    type: 'POST',
    url: '/behandeling.php',
    data: {
      behandeling_nieuw: gekozen_behandeling
    },
    dataType: 'html',
    success: function(response) {
      console.log(response);
    }
  });

});

执行此操作后,它会发出警报。

在我的文件behandeling.php 中,代码是这样的:

<?php
session_start();

$_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];

echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';

?>

完成此操作并导航到 behandeling.php 文件后,session variable 仍然为空。

<pre>Array
(
    [bookly] => Array
        (
            [forms] => Array
                (
                    [5e1c6d8916b27] => Array
                        (
                            [defaults] => Array
                                (
                                    [service_id] => 0
                                    [staff_id] => 0
                                    [location_id] => 0
                                    [category_id] => 5
                                )

                            [last_touched] => 1578921357
                            [data] => Array
                                (
                                    [first_rendered_step] => 1
                                    [time_zone] => 
                                    [time_zone_offset] => 
                                    [date_from] => 2020-01-13
                                    [days] => 
                                    [time_from] => 09:00
                                    [time_to] => 21:00
                                    [slots] => Array
                                        (
                                        )

                                    [facebook_id] => 
                                    [full_name] => 
                                    [first_name] => 
                                    [last_name] => 
                                    [email] => 
                                    [email_confirm] => 
                                    [phone] => 
                                    [birthday] => 
                                    [additional_address] => 
                                    [country] => 
                                    [state] => 
                                    [postcode] => 
                                    [city] => 
                                    [street] => 
                                    [street_number] => 
                                    [address_iso] => Array
                                        (
                                        )

                                    [notes] => 
                                    [info_fields] => Array
                                        (
                                        )

                                    [coupon_code] => 
                                    [deposit_full] => 0
                                    [edit_cart_keys] => Array
                                        (
                                        )

                                    [repeated] => 0
                                    [repeat_data] => Array
                                        (
                                        )

                                )

                            [cart] => Array
                                (
                                )

                            [chain] => Array
                                (
                                    [0] => Array
                                        (
                                            [service_id] => 
                                            [staff_ids] => Array
                                                (
                                                )

                                            [number_of_persons] => 
                                            [quantity] => 
                                            [extras] => Array
                                                (
                                                )

                                            [custom_fields] => Array
                                                (
                                                )

                                            [location_id] => 
                                            [series_unique_id] => 0
                                            [first_in_series] => 
                                            [units] => 1
                                        )

                                )

                            [booking_numbers] => Array
                                (
                                )

                            [payment_id] => 
                            [payment_type] => local
                        )

                    [5e1c6dc0324c7] => Array
                        (
                            [defaults] => Array
                                (
                                    [service_id] => 0
                                    [staff_id] => 0
                                    [location_id] => 0
                                    [category_id] => 5
                                )

                            [last_touched] => 1578921411
                            [data] => Array
                                (
                                    [first_rendered_step] => 1
                                    [time_zone] => 
                                    [time_zone_offset] => 
                                    [date_from] => 2020-01-13
                                    [days] => 
                                    [time_from] => 09:00
                                    [time_to] => 21:00
                                    [slots] => Array
                                        (
                                        )

                                    [facebook_id] => 
                                    [full_name] => 
                                    [first_name] => 
                                    [last_name] => 
                                    [email] => 
                                    [email_confirm] => 
                                    [phone] => 
                                    [birthday] => 
                                    [additional_address] => 
                                    [country] => 
                                    [state] => 
                                    [postcode] => 
                                    [city] => 
                                    [street] => 
                                    [street_number] => 
                                    [address_iso] => Array
                                        (
                                        )

                                    [notes] => 
                                    [info_fields] => Array
                                        (
                                        )

                                    [coupon_code] => 
                                    [deposit_full] => 0
                                    [edit_cart_keys] => Array
                                        (
                                        )

                                    [repeated] => 0
                                    [repeat_data] => Array
                                        (
                                        )

                                )

                            [cart] => Array
                                (
                                )

                            [chain] => Array
                                (
                                    [0] => Array
                                        (
                                            [service_id] => 
                                            [staff_ids] => Array
                                                (
                                                )

                                            [number_of_persons] => 
                                            [quantity] => 
                                            [extras] => Array
                                                (
                                                )

                                            [custom_fields] => Array
                                                (
                                                )

                                            [location_id] => 
                                            [series_unique_id] => 0
                                            [first_in_series] => 
                                            [units] => 1
                                        )

                                )

                            [booking_numbers] => Array
                                (
                                )

                            [payment_id] => 
                            [payment_type] => local
                        )

                    [5e1c6df9da30f] => Array
                        (
                            [defaults] => Array
                                (
                                    [service_id] => 0
                                    [staff_id] => 0
                                    [location_id] => 0
                                    [category_id] => 5
                                )

                            [last_touched] => 1578921468
                            [data] => Array
                                (
                                    [first_rendered_step] => 1
                                    [time_zone] => 
                                    [time_zone_offset] => 
                                    [date_from] => 2020-01-13
                                    [days] => 
                                    [time_from] => 09:00
                                    [time_to] => 21:00
                                    [slots] => Array
                                        (
                                        )

                                    [facebook_id] => 
                                    [full_name] => 
                                    [first_name] => 
                                    [last_name] => 
                                    [email] => 
                                    [email_confirm] => 
                                    [phone] => 
                                    [birthday] => 
                                    [additional_address] => 
                                    [country] => 
                                    [state] => 
                                    [postcode] => 
                                    [city] => 
                                    [street] => 
                                    [street_number] => 
                                    [address_iso] => Array
                                        (
                                        )

                                    [notes] => 
                                    [info_fields] => Array
                                        (
                                        )

                                    [coupon_code] => 
                                    [deposit_full] => 0
                                    [edit_cart_keys] => Array
                                        (
                                        )

                                    [repeated] => 0
                                    [repeat_data] => Array
                                        (
                                        )

                                )

                            [cart] => Array
                                (
                                )

                            [chain] => Array
                                (
                                    [0] => Array
                                        (
                                            [service_id] => 
                                            [staff_ids] => Array
                                                (
                                                )

                                            [number_of_persons] => 
                                            [quantity] => 
                                            [extras] => Array
                                                (
                                                )

                                            [custom_fields] => Array
                                                (
                                                )

                                            [location_id] => 
                                            [series_unique_id] => 0
                                            [first_in_series] => 
                                            [units] => 1
                                        )

                                )

                            [booking_numbers] => Array
                                (
                                )

                            [payment_id] => 
                            [payment_type] => local
                        )

                    [5e1c6e0f76ddd] => Array
                        (
                            [defaults] => Array
                                (
                                    [service_id] => 0
                                    [staff_id] => 0
                                    [location_id] => 0
                                    [category_id] => 5
                                )

                            [last_touched] => 1578921490
                            [data] => Array
                                (
                                    [first_rendered_step] => 1
                                    [time_zone] => 
                                    [time_zone_offset] => 
                                    [date_from] => 2020-01-13
                                    [days] => 
                                    [time_from] => 09:00
                                    [time_to] => 21:00
                                    [slots] => Array
                                        (
                                        )

                                    [facebook_id] => 
                                    [full_name] => 
                                    [first_name] => 
                                    [last_name] => 
                                    [email] => 
                                    [email_confirm] => 
                                    [phone] => 
                                    [birthday] => 
                                    [additional_address] => 
                                    [country] => 
                                    [state] => 
                                    [postcode] => 
                                    [city] => 
                                    [street] => 
                                    [street_number] => 
                                    [address_iso] => Array
                                        (
                                        )

                                    [notes] => 
                                    [info_fields] => Array
                                        (
                                        )

                                    [coupon_code] => 
                                    [deposit_full] => 0
                                    [edit_cart_keys] => Array
                                        (
                                        )

                                    [repeated] => 0
                                    [repeat_data] => Array
                                        (
                                        )

                                )

                            [cart] => Array
                                (
                                )

                            [chain] => Array
                                (
                                    [0] => Array
                                        (
                                            [service_id] => 
                                            [staff_ids] => Array
                                                (
                                                )

                                            [number_of_persons] => 
                                            [quantity] => 
                                            [extras] => Array
                                                (
                                                )

                                            [custom_fields] => Array
                                                (
                                                )

                                            [location_id] => 
                                            [series_unique_id] => 0
                                            [first_in_series] => 
                                            [units] => 1
                                        )

                                )

                            [booking_numbers] => Array
                                (
                                )

                            [payment_id] => 
                            [payment_type] => local
                        )

                )

        )

    [type_behandeling] => 51
)
</pre>

【问题讨论】:

  • 您收到警报了吗?没有任何异常?
  • 如何将变量存储到会话中?
  • 尝试警报(响应);并在此处提供
  • 将其设为console.log 并将结果粘贴在其下方。它在type_behandeling 中显示了正确的值,但它是一个预订表格,所以在下一步中我需要再次获得该值。我怎样才能做到这一点?导航到第 2 页时页面未刷新
  • 要填写SessionCookie,您必须使用return。您可以使用return response();,然后在会话中填写您的捐赠数据。

标签: php jquery ajax session post


【解决方案1】:
  1. dataType: 'html' 更改为 dataType: 'JSON'
  2. 通过 Chrome/Firefox 开发工具检查 - 正在调用 behandeling.php 文件
  3. 通过 Chrome/Firefox 开发工具检查 - 表单数据正在提交到 PHP 文件
  4. 另外,如果你在外面访问文件,它会重新检查 $_POST 的值,因为该值没有设置,所以会为会话分配一个空白。

检查 $_POST 值

   if (isset($_POST["behandeling_nieuw"])) {
       $_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];

     echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';    
   } else {    
       echo "POST Value not set";

       if(isset($_SESSION['type_behandeling']) && !empty($_SESSION['type_behandeling'])) {
      echo 'Set and not empty, and no undefined index error!';
       }

       else {
           echo "Session is not set ";
       }


   }

【讨论】:

  • 我在哪里可以查看?
  • 检查更新的答案
  • 我没有设置 POST 值
  • print_r($_SESSION) 和检查会话现在可用
  • 我得到 POST Value not set 并且在所有其他会话变量之下
【解决方案2】:

当您导航到behandeling.php 时,可能会出现这种情况 您将发布的变量分配给会话作为

$_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];

在您访问时未找到发布的数据(http get 方法)。因此会话被 null 或空覆盖。

【讨论】:

  • 好的,我明白了。但是我想在单击其他内容时在同一页面上重新使用会话变量。
  • 您可以检查变量 $_POST["behandeling_nieuw"] 是否设置了使用 isset() 如果这样更改会话,否则保持原样。
猜你喜欢
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
  • 2019-08-03
  • 2014-02-12
  • 2016-05-12
  • 2015-11-02
  • 2015-10-10
  • 2014-11-09
相关资源
最近更新 更多