发送会话变量从阿贾克斯到PHP变量、阿贾克斯到、PHP

2023-09-10 14:00:56 作者:禁戒 

我有一个网页,每个人都有自己的按钮,4个按钮。当按钮被点击的页面不刷新,也没有翻开新的一页。

当一个按钮为pressed我想通过Ajax发送按钮的ID的PHP文件中的一个会话的形式。每4个按键有不同的ID(1,2,3,4)。 如果用户点击一个按钮,然后第二个按钮等,我想每一个点击按钮的ID的加入到本次会议。

这是我到目前为止有:

jQuery的:

  $(文件)。就绪(函数(){
     $('一[名称=添加])。点击(函数(五){
     会议= $(本).attr('身份证')

     .post的$('sessions.php',{会话:会话}
    );
     });
     });
 
php修改mysql数据找不到 php 如何修改mysql数据

这样简单的检查,如果以增加名称的按钮被点击(所有4个按钮有一个名称),并抓住了按钮ID,并将其发送到sessions.php

PHP:

 < PHP
    在session_start();
    $ _SESSION ['IDS'] = $ _ POST ['会议'];
    ?>
 

简单地创建会话。

我遇到的问题是,PHP会越来越点击第一个按钮的ID,这是所有。如果点击另一个按钮阿贾克斯确实张贴了按钮的ID,但会议只保留第一个按钮的ID点击。

我如何添加的下一个按钮ID点击到会话中。

例子:如果按钮1和3被点击,我需要的会议上就ID 1和3

。 解决方案

 在session_start();

// prepare $ _SESSION ['IDS']
如果(!is_array($ _ SESSION ['IDS']))
{
    $ _SESSION ['IDS'] =阵列();
}

//填充$ _SESSION ['IDS']只有新的$ _ POST ['会议']发布
如果(!in_array($ _ POST ['会议'],$ _SESSION ['IDS']))
{
    $ _SESSION ['IDS'] [] = $ _ POST ['会议'];
}
 

I have 4 buttons on a page that each have their own button. When the button is clicked the page does not refresh nor does it open a new page.

When a button is pressed I want to send the ID of that button via Ajax to a php file in the form of a session. Each of the 4 buttons have a different ID (1,2,3,4). If a user clicks one button and then a second button etc, I want to add each of the clicked button Id's into this session.

This is what I have so far:

Jquery:

     $(document).ready(function() {  
     $('a[name=add]').click(function(e) {
     session = $(this).attr('id')

     $.post('sessions.php', {session:session}
    );              
     });
     });

So that simply checks if a button with the name of "add" has been clicked (all 4 of the buttons have that name), and grabs the buttons ID and sends it to sessions.php

PHP:

    <?php
    session_start();
    $_SESSION['ids'] = $_POST['session'];  
    ?>

Simply creates the session.

The problem I am having is that the PHP session is getting the ID of the first button clicked and that is all. If another button is clicked Ajax does post the button ID but the session only keeps the ID of the first button clicked.

How can I add the ID of the next buttons clicked to the session.

Example: If button 1 and 3 are clicked, I need the session to have ID 1 and 3.

解决方案

session_start();

// prepare $_SESSION['ids']
if ( ! is_array($_SESSION['ids']))
{
    $_SESSION['ids'] = array();
}

// fill $_SESSION['ids'] only with new $_POST['session'] posted
if ( ! in_array($_POST['session'], $_SESSION['ids']))
{
    $_SESSION['ids'][] = $_POST['session'];
}

 
精彩推荐
图片推荐