Bengau
Bengau

Reputation: 175

Radio Buttons Switch Checked php

My site is like a search engine with 2 types of search. Type 1 Type 2. I just dont know how to do it when someone come to my site the default checked type to be Type 1 and if it choose Type2 then remain Type2 for the visitor even if the page is refreshed. What i need exactly is like sample from here.

js

$(function() {
        $( "#radio" ).buttonset();
    });​

html

<form>
    <div id="radio">
        <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
        <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
    </div>
</form>

I want to be able to switch the checked="checked" from a radio to another or something like this i think. Really don't know. But i think it has to do something with php or javascript.

Upvotes: 2

Views: 483

Answers (4)

chokrijobs
chokrijobs

Reputation: 761

and my_function will send an ajax request to open session to keep the value of checked radio:

my_function()
{
   var value_checked = $("input[name='radio']:checked").val();
   $.ajax({
   type: 'POST',
   url: 'page.php',
   data: {'value_checked':value_checked},
   });
}

you get in your page.php $_POST['value_checked '] and you set it in a session variable

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55740

You have to store that information in a cookie or a session as web is stateless and it will not remember the status of the page if posted back unless it is cached somewhere..

Try using the jQuery cookie plugin

Upvotes: 0

chokrijobs
chokrijobs

Reputation: 761

you can define function in the onClick event of the radio button:

$(document).ready(function() {
  $('radio[name=radio]').each(function() {
   $(this).click(function() {
    my_function();
    });
   });
});

Upvotes: 1

Daniel Li
Daniel Li

Reputation: 15379

You would need to cache/store the set option somewhere (cookie, database, memcache(d), redis, etc.) and load using the user's ID. This will also have to be handled in Javascript as it involves the client-side.

Cookie Reference: https://developer.mozilla.org/en-US/docs/DOM/document.cookie

For memcache(d), redis, SQL, file storage:

AJAX Reference: https://developer.mozilla.org/en-US/docs/AJAX

Upvotes: 3

Related Questions