skip to Main Content

I send a value to a php file with AJAX.

I am having this function in 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);
    }
  });

});

After doing this it alerts done.

In my file behandeling.php the code is like this:

<?php
session_start();

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

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

?>

After doing this and navigate to the behandeling.php file the session variable is still empty.

<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>

2

Answers


    1. Change dataType: ‘html’ to dataType: ‘JSON’
    2. Through Chrome/Firefox Dev tools check – behandeling.php file is getting called
    3. Through Chrome/Firefox Dev tools check – form data is getting submitted to PHP File
    4. Also if you access the file outside, it Recheck the $_POST Value , since the value is not set, a blank will be assigned to the session.

    Check the $_POST Value

       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 ";
           }
    
    
       }
    
    Login or Signup to reply.
  1. It might be possible that when you navigate to behandeling.php
    that you are assigning the posted variable to session as

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

    the posted data not found as you are visiting (http get method). Hence session is overwritten with null or empty.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search