I
I
Ivan2015-11-17 23:41:30
PHP
Ivan, 2015-11-17 23:41:30

Send data via ajax to $_SESSION variable?

Good evening. I can't set up the ajax request handler in any way so that it adds data to the $_SESSION variable.
Here is the code below:
The form with which the request occurs

<div id='product_add_result'></div>
<form method="get" action="//work.shucach.freedomain.thehost.com.ua/wp-content/themes/work-and-travel-child/add.php" class='product_add'>
<input type="text" name="unit" value="1">
<input type="submit" value="добавить" name="nevvalue">
</form>


Script that handles this request:
$(function(){
  // откуда берем данные сформы
  $('.product_add').submit(function(e){
    //отменяем стандартное действие при отправке формы
    e.preventDefault();
    //берем из формы метод передачи данных
    var m_method=$(this).attr('method');
    //получаем адрес скрипта на сервере, куда нужно отправить форму
    var m_action=$(this).attr('action');
    //получаем данные, введенные пользователем в формате input1=value1&input2=value2...,
    //то есть в стандартном формате передачи данных формы
    var m_data=$(this).serialize();
    $.ajax({
      type: m_method,
      url: m_action,
      data: m_data,
      success: function(result){
        // где показываем результат
        $dd = result;
        $_SESSION['id'] = $dd;
        $('#product_add_result').html($dd);

      }
    });
  });
});


The file that handles all of this.
<?
echo $_GET['unit']
?>

As a result, everything works, but I can't assign $_GET['unit'] to a variable and display it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-11-18
@Shucach

I'm wildly surprised that you wrote "everything works", but if specifically on the problem, then look in the developer panel what data you have going to the server, you can remove ['unit'] and put var_dump($_GET)
and I'm surprised specifically by this line

$_SESSION['id'] = $dd;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question