在不刷新页面的情况下将javascript变量传递给php [英] Passing javascript variable to php without refreshing the page

查看:79
本文介绍了在不刷新页面的情况下将javascript变量传递给php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个5x5的div框网格(其中有25个),当我在其中放置项目时,我正在使用jQuery UI进行注册.它将收到放入的盒子的标题和该部分的名称.

I have a 5x5 grid of div boxes (25 of them) and I am using jQuery UI to register when I drop a item in it. It will receive the title of the box it was dropped in and the name of the item, that part works.

我想在不刷新页面的情况下将标题和名称传递给PHP(因为这将重置项目).我得到成功!"但似乎没有传递数据.

I want to pass the title and name to PHP without refreshing the page (because then the items will reset). I get a "success!" but it seems like it doesn't pass the data.

index.php

<script>
    $(function() {
        $( "li img" ).draggable({ 
            snap: ".grid", 
            start: function (event, ui ) {
                item = $(this).attr('title');
            }
        });
        $( "li .grid" ).droppable({
            drop: function( event, ui ) {
                box = $(this).attr('title');
                $.ajax({
                    type: "POST",
                    url: 'index.php',
                    data: { box : 'box' },
                    success: function(data){
                        alert("success!");
                    }
                });

            }
        });
    });        
</script>

sessions.php

<?php
   session_start();
if(isset($_POST['box']))
{
   // store session data
   $_SESSION['box'] = $_POST['box'];
}
   //retrieve session data
   echo $_SESSION[box];
?>

如何在不刷新页面的情况下将标题和名称传递给PHP?

How do I pass the title and name to PHP without refreshing the page?

推荐答案

尝试从中更改urldata:

url: 'index.php',
data: { box : 'box' }

对此:

url: 'sessions.php',
data: { box : box }

这篇关于在不刷新页面的情况下将javascript变量传递给php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆