1

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?

2
  • 3
    I would suggest AJAX call to a PHP script which adds it in the session. Mixing PHP in your front end is not possible as its SERVER SIDE SCRIPTING Commented Dec 24, 2012 at 4:01
  • If LI's exist in browser they can stay there as long as browser is open unless you set refresh with script or meta tag, or use ajax to talk to server and again use script depending on response...not exactly clear what your objective is Commented Dec 24, 2012 at 4:30

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.