简体   繁体   中英

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.

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

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();
    });
   });
});

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

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

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