doudou7361 2012-12-24 03:56
浏览 43

使用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?

  • 写回答

2条回答 默认 最新

  • doufangxian4985 2012-12-24 04:08
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集