shreyaskudav Posted May 25, 2014 Share Posted May 25, 2014 This is my radio disabling function. <script>function disableElement(){var dis = '<?php echo $dis_value; ?>';document.getElementById(dis).disabled=true;}</script> And I will call it when the page is loaded! <body onLoad="disableElement()"> And the radio button is displayed through loop..! echo '<form method="post" action="trainingserver.php">';$i = 1;while($i <= $num_rows){echo '<input type="radio" id="dis"'.$i.'"" name="select" value="'.$i.'" />';echo $poke[$i]."<br>";echo 'Level:'.$level[$i]."<br>";echo 'Health:'.$health[$i]."<br>";$i++;}echo '<input type="submit" name="submit" value="Select Pokemon!" />';echo '</form>'; So the radio ids would be like: dis1,dis2 ... dis6 But its not disabling it...! I think its because of the while loop, I used..! what should I do..? Link to comment Share on other sites More sharing options...
davej Posted May 25, 2014 Share Posted May 25, 2014 Well, how does the page render? Are the id values correct? You only want to disable one of them? Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 25, 2014 Author Share Posted May 25, 2014 Yaa! I worked on it..! and fortunately got solved! Problem was in id in form: id="dis'.$i.'" I have a question now..can I maintain this disabled radio even after reload?? Coz now it gets disabled but after reload again gets enabled..! I googled it and got something like I have to use localstorage ??!! Link to comment Share on other sites More sharing options...
Ingolme Posted May 25, 2014 Share Posted May 25, 2014 In order for Javascript to remember anything you need to use either cookies (compatible with the widest range of browsers) or local storage. 1 Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 25, 2014 Author Share Posted May 25, 2014 (edited) Well, do you have any good link that will help me out with storing radio in cookie?? or you can give me some idea about I can I use cookie in this case..! Edited May 25, 2014 by shreyaskudav Link to comment Share on other sites More sharing options...
dsonesuk Posted May 25, 2014 Share Posted May 25, 2014 For setting cookie when radio button selected after page page fully rendered. http://www.w3schools.com/js/js_cookies.asp To read cookie you can use JavaScript again, or php to produce the disabled radio button if condition meets requirements. http://www.w3schools.com/php/php_cookies.asp Link to comment Share on other sites More sharing options...
davej Posted May 25, 2014 Share Posted May 25, 2014 You are creating what seems to be a Php-centric page. When and why do you decide to disable the radio button? If you have already decided to disable it when Php is rendering the page then I don't know why you need to pass the task to Javascript. Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 25, 2014 Author Share Posted May 25, 2014 There would be 6 radio...each representing a pokemon...which is to be used! so basically all 6 radios would be enabled! Once a pokemon is killed, i.e. health <=0 ..... the corresponding radio will be disabled..! Therefore user cannot use it again, as its killed! So I need to store it in the cookie...!And also there are 5 more pokemons... I will also require to store all those in the cookie. Link to comment Share on other sites More sharing options...
davej Posted May 25, 2014 Share Posted May 25, 2014 Ever since this discussion started you have not made a clear delineation between the role of the client and the role of the server. When a "pokemon" is "killed" doesn't the server need to record this? Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 25, 2014 Author Share Posted May 25, 2014 (edited) Yes! I have already handled the server... the health reduction and other calculations are set..! <script>function disableElement(){var dis = '<?php echo $dis_value; ?>';document.getElementById(dis).disabled=true;}</script> You see the variable $dis_value , passed to the javascript variable when the health of the pokemon is <= 0. This $dis_value will consisting the value, which is also the ID="" of the radio, which is to be disabled! I just want to know the proper format or syntax, that I should use to save the state of disabled radio in cookie so that it maintains it futher...! Edited May 25, 2014 by shreyaskudav Link to comment Share on other sites More sharing options...
davej Posted May 25, 2014 Share Posted May 25, 2014 Yeah but why not simply have Php do it? if ($disabled[$i]){echo '<input type="radio" id="dis"'.$i.'"" name="select" value="'.$i.'" disabled />';}else{echo '<input type="radio" id="dis"'.$i.'"" name="select" value="'.$i.'" />';} 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted May 25, 2014 Share Posted May 25, 2014 If the radio buttons are using the same name value, you should be only be able select one, the rest should clear themselves, with check boxes you can select multiple choices under the same name value. Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 25, 2014 Author Share Posted May 25, 2014 (edited) hey davej! That idea is quiet simple and cool! (y) Didn't thought of it!! (y) But I can store ID value in array? $disabled[$i] ?? Edited May 25, 2014 by shreyaskudav Link to comment Share on other sites More sharing options...
davej Posted May 25, 2014 Share Posted May 25, 2014 You would have to use whatever criteria you have for remembering the id's that you need to disable. 1 Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 26, 2014 Author Share Posted May 26, 2014 Hahahah!! Great Davej!! Finally working on it for 3 dayss!!! Got it worked (y) I used foreach() ! Cheers! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now