从localstorage到POST到PHP变量

I am executing a somewhat complex code from my point of view. It goes like this: First I am getting a list of numbers from localstorage from another page.

<script> 
var data = JSON.parse(localStorage.getItem('key'));
var localData = data.join(", ");  

Then I am posting data to PHP code on the same page like this:

$.ajax({
type: 'post',
       data: {localData: localData}, 
       dataType: "json",
       success: function(result){
       console.log(result)
       }  
       });
</script>

And then I want to put the data into a PHP variable and use it like this:

<?php
$user_id = isset($_POST['localData'])?$_POST['localData']:"";
$values = $user_id;
echo $values; ?>

The network inspector reveals the data is posted like this: localData 3, 5, 6, 8, 9

But I can't seem to get the data into the PHP variable and echo it. Please help.

Your initial php tag is wrong, change <php? to <?php, also, in order to convert json to an array, you should use json_decode(), i.e.:

<php?
$user_id = isset($_POST['localData'])?$_POST['localData']:"";
$values = json_decode($user_id);
var_dump($values);

Tip:

Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code.
Remember to comment or remove it in production mode.