使用jquery创建php会话

I am looking to use jquery to add to the DOM for an <ul>, I want to add more <li>'s into the list, but just to last for a session with php.

e.g.

$lists = $_SESSION['names']
//which contains an array of all the 'names', and then I would run a loop:

echo "<ul>";
foreach($lists as $list) {
  echo "<li>$list</li>";
}
echo "</ul>";

So the user can add these variables into the list, during this session. How would this be done, and how is the jquery able to interact with php like this? And what method would be easiest?

It seems like you misunderstand how PHP works.

PHP is a server side script. This means that it runs on a server and the user can't see it running or have the code appearing on his side.

jQuery is a javascript library. It is a client side helper for user interactions.

So basically there's no way to directly run PHP from an HTML document

But indirectly there is AJAX.

AJAX allows you to visit a page (and basically running a PHP script) without reloading your page.

http://api.jquery.com/jQuery.ajax/

Example

$.ajax({
 type: "POST",
 url: "some.php",
 data: { list: jQueryVariableList
  }).done(function( msg ) {
      alert( "Data Saved: " + msg );
});

Now on the PHP side you can pick up the list using $_POST['jQueryVariableList']

Have a look at this:

In javascript:

jQuery('#div_session_write').load('session_write.php?session_name=new_value');

In session_write.php file:

<?
session_start();

if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];}
?>

In html:

<div id='div_session_write'> </div>