Jump to content

Recommended Posts

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.

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.

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 by smoseley
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.