简体   繁体   中英

Set Session on checked box php Joomla

I need to store some sort of value like session does, $_Session['ss']=1 and then check if session has been set or not... but the session has to be set with javascript or jquery, I know how to set a session var in PHP but I don't in java, now the reason for me to use Java or jQuery is because I need to refresh the page soon as the user checks the box..

I was trying to use this example https://stackoverflow.com/a/20948139/2293454 but I'm to dumb to follow...
so here is what I want to do:

User checks-box -> the script sets a variable session which will be use in a another file
after that the page refresh and the user now has the session var and the checkbox has to stay checked, now is the user unchecked the box the session var gets unset and the page refreshes again...
now how do I do that?

Thank you for taking the time.

You can use jquery/ajax to create a session...

A simple html form with checkbox:

<form>
    <input type="checkbox" id="checkBox" name="my_check_box" value="1" />Check me!
</form>

The JS:

$("#checkBox").change(function () {
    if ($(this).is(':checked')) { //we want the rest to run only if checkbox is checked
        var checkValue = $(this).val();
        //alert(checkValue); //uncomment to check the behaviour of the condition ...or delete it!
        var setSess = $.ajax({
            type: "POST",
            url: "create_session.ajax.php",
            data: "valueR=" + checkValue
        });
        setSess.done(function(getResp) {
            var clrResp = $.trim(getResp);
            alert(clrResp); //here we alert the session as it has been "echoed" in the php file
            return true;
        });
    }

});

Finally, the new file "create_session.ajax.php" that is called by ajax, could look something like that:

<?php
session_start();
$value = $_POST["valueR"];

$_SESSION['ss'] = 1; //use this line or the next one
//$_SESSION['ss'] = $value; //commented out because we use the line above that provides a static "1" value... if you want to use the actual "value" of the checkbox use this line instead the one above.
$response = $_SESSION['ss'];
echo $response;

?>

Quite simple solution using jquery... If you have any questions or need further information, I'll be glad to help.

A demo of the jquery functionality without the php file can be found here: http://jsfiddle.net/S5dnP/3/

T.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM