简体   繁体   中英

AJAX POST not working, php doesn't create session

I want to send id of element to php and create session for this.

This is piece from php file:

<?php
$sql = "SELECT id FROM products";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
?>
    <tr class="table-manufacture-tr">
    <td class="table-manufacture-td-statys">
        <div class="warehouse-window-content-dropdown-plus2 plus">
           <a class="open_item" data-id=<?php echo "\"".$row['p_id']."\"";?> 
            style="text-decoration: none; color: #D3D3D3;">Click</a>
        </div>
    </td>
    </tr>
<?php
}
?>

And in this file javascript code:

$(document).on('click', '.open_item', function(event){

        var data_item = this.getAttribute("data-id");

        $.ajax({
            url: 'get_id.php',
            type: 'POST',
            data-type: 'json',
            data: { id: data_item },
            contentType: 'application/x-www-form-urlencoded',
            success: function(data){
                console.log(data);
            },
            error: function(){
                console.log("not working");
            }
        });
    });

This is get_id.php:

<?php
    session_start();
    $_SESSION['item_id'] = json_encode($_POST);
    header("Content-Type: application/json", true);
?>

I have tried also without content types and without json. "var data_item" prints id correct, but php doesn't create session and in console also clear(nothing).

The reason that you are not getting data in session is, you are not assigning proper value to session. Also it should be json_decode not json_encode .

replace

$_SESSION['item_id'] = json_encode($_POST);

with

if (!empty($_POST['id'])) {
    $_SESSION['item_id'] = json_decode($_POST['id']); // use json_decode
}

It seems to me that you are making some small mistake in your code like you are echoing $row['p_id'] while your query should return id instead p_id also you are making mistake in ajax you are sending data-type JavaScript assuming your code is subtracting so try to use this code i code below.

// modify your php code
<?php
$sql = "SELECT id FROM products";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_assoc($result)) { ?>
  <tr class="table-manufacture-tr">
      <td class="table-manufacture-td-statys">
        <div class="warehouse-window-content-dropdown-plus2 plus">
          <a class="open_item" data-id=<?php echo "\"".$row['id']."\"";?> 
          style="text-decoration: none; color: #D3D3D3;">Click</a>
        </div>
      </td>
  </tr>
<?php } ?>

// modify your jQuery
$(document).on('click', '.open_item', function(event){

    var data_item = $(this).data("id");

    $.ajax({
        url: 'get_id.php',
        type: 'POST',
        dataType: 'json',
        data: { id: data_item },
        success: function(data){
            console.log(data);
        },
        error: function(){
            console.log("not working");
        }
    });
});


<?php
  session_start();
  header("Content-Type: application/json", true);
  $_SESSION['item_id'] = json_encode($_POST["id"]);
  echo json_encode(['data_id' => $_SESSION['item_id']]);
?>

You can use

$_SESSION['item_id'] = json_encode($_POST['id']);

instead of

$_SESSION['item_id'] = json_encode($_POST);

this will work fine.

I don't know what you are trying to do, but from your JS, it looks like that you are expecting that the PHP script --which you post some data to it-- to return a json with the data you have just posted in it. In that case, try this, change your get_id.php to be like:

<?php
    session_start();
    $_SESSION['item_id'] = json_encode($_POST);
    header("Content-Type: application/json", true);
    echo $_SESSION['item_id'];
?>

I'd troubleshoot this by making sure the click handler is actually going off. Put alert("clicked"); as the first thing in the in the click handler to make sure.

For the meantime, remove the contentType in the json call. Also remove the dataType (data-type) entirely. On the php side, replace the header() line so (as mentioned) the php is just:

session_start();
$_SESSION['item_id'] = $_POST["id"];
echo $_SESSION['item_id'];

Do not use json_encode/decode right now. From your code, it is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM