MockY Posted October 26, 2012 Share Posted October 26, 2012 In a regular form, how would I go about to populate a php session variable with the value of the checkbox the user checked, all without reloading the page? I don't need to error check or anything for this, as I do that once the user submits the form. I just want to add a value to an existing session variable as soon as the user check the box. Any help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 26, 2012 Share Posted October 26, 2012 AJAX. Quote Link to comment Share on other sites More sharing options...
MockY Posted October 26, 2012 Author Share Posted October 26, 2012 I know AJAX is the way to go, hence me posting here and not elsewhere. However, I need some guidance to how to proceed with this as I find it far from straight forward. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 26, 2012 Share Posted October 26, 2012 If you don't know anything about ajax, we can't teach you from the ground up. The basic idea is that the request object allows javascript to call a URL and retrieve some formatted data, then parse that data and perform relevant actions. You'll need to write a page that accepts a value (maybe a cookie) and returns the relevant data you're wanting as a bare string. Then look up how to do an AJAX GET request using jquery (or by hand). Have the ajax request hit the page you wrote, then move on from there. Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 26, 2012 Share Posted October 26, 2012 jQuery. You're not asking a very specific question, so you're not going to get a very specific answer. Quote Link to comment Share on other sites More sharing options...
codefossa Posted October 31, 2012 Share Posted October 31, 2012 Have a PHP page that will set it, or part of a page and just use a GET variable then call jQuery's $.load function to it and you should be set. Quote Link to comment Share on other sites More sharing options...
smoseley Posted October 31, 2012 Share Posted October 31, 2012 (edited) AJAX. Helpful... If you don't know anything about ajax, we can't teach you from the ground up. Why not? It's not a complicated thing he's asking for, and AJAX is a pretty simple concept... Your html page (requires jQuery): <input type="checkbox" id="checkbox_id" /> <script type="text/javascript"> $('#checkbox_id').change(function(box) { $.ajax('/update_session.php?checked=' + (box.attr('checked') ? 1 : 0) ); }); </script> update_session.php session_start(); $_SESSION['box_checked'] = $_GET['checked'] ? 1 : 0; Edited October 31, 2012 by smoseley Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.