如何在jQuery中存储多个Click事件的PHP会话值?

如何在jQuery中存储多个Click事件的PHP会话值?

问题描述:

I am trying to use jQuery to pass some values to session variables when buttons are clicked on. Each button has unique values and I need to display values from both buttons when both are clicked.

The problem I am having is that only one set of values (from the most recent button clicked) is translated as a session variable - if I click the first button, the first values are passed to the session backend fine, but clicking the second button afterwards results in only the second values being passed.

My jquery looks like this:

$button_one.click(function(e) {
 e.preventDefault();
 var var_one = "value1";
 var var_two = "value2";
 $.post("sessions.php", { 'session_var_one': var_one, 'session_var_two': var_two });
});

$button_two.click(function(e) {
 e.preventDefault();
 var var_three = "value3";
 var var_four = "value4";
 $.post("sessions.php", { 'session_var_three': var_three, 'session_var_four': var_four });
});

The sessions.php is very simple:

<?php

 session_start();

 $_SESSION['value_one'] = $_POST['session_var_one'];
 $_SESSION['value_two'] = $_POST['session_var_two'];

 $_SESSION['value_three'] = $_POST['session_var_three'];
 $_SESSION['value_four'] = $_POST['session_var_four'];

?>

And I have a simple page set up to display the session values:

<?php
 session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php

 echo '<h5>' . $_SESSION['session_var_one'] . '</h5>';
 echo '<h5>' . $_SESSION['session_var_two'] . '</h5>';

 echo '<h5>' . $_SESSION['session_var_three'] . '</h5>';
 echo '<h5>' . $_SESSION['session_var_four'] . '</h5>';

?>

</body>
</html>

This page displays only the two values of the button that was last clicked on - instead of displaying both sets of values if both buttons are clicked on.

I am guessing that having two separate AJAX requests, one within each click function, may be the problem here - when button two is clicked after button one, it "forgets" the first request and thus the values are not recorded.

I have worked around this problem by sending each of the button click values to a different session php page (i.e. button_one goes to sessions_one.php and button_two goes to sessions_two.php) but I would prefer not to have to create a new place to store the session values for each button. I plan on adding more buttons and it seems like bad practice to have each button have a separate home for its stored values.

How can I rewrite my AJAX requests and/or sessions.php so that I can store all of the values from each button click? Thank you for any guidance, I'm very new to PHP and AJAX in general!

If a $_POST variable is not provided, your code will just put NULL in the corresponding $_SESSION variable. To prevent that, check that it is provided before doing so:

<?php

session_start();

if( isset($_POST['session_var_one']) ){
    $_SESSION['value_one'] = $_POST['session_var_one'];
}
if( isset($_POST['session_var_two']) ){
    $_SESSION['value_two'] = $_POST['session_var_two'];
}

if( isset($_POST['session_var_three']) ){
    $_SESSION['value_three'] = $_POST['session_var_three'];
}
if( isset($_POST['session_var_four']) ){
    $_SESSION['value_four'] = $_POST['session_var_four'];
}

?>

I think you really want something like this so the session data stays if you aren't passing it a new value.

$_SESSION['value_one'] = (!empty($_POST['session_var_one'])) ? $_POST['session_var_one'] : $_SESSION['value_one'];
$_SESSION['value_two'] = (!empty($_POST['session_var_two'])) ? $_POST['session_var_two'] : $_SESSION['value_two'];
$_SESSION['value_three'] = (!empty($_POST['session_var_three'])) ? $_POST['session_var_three'] : $_SESSION['value_three'];
$_SESSION['value_four'] = (!empty($_POST['session_var_four'])) ? $_POST['session_var_four'] : $_SESSION['value_four'];