how to show the radio button is selected is still ticked?

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

my app has 2 radio buttons, when user ticks any of it, the value of the ticked radio button gets saved in the table via ajax, now the problem is, when user gets back to the page, the radio button is not ticked, how to make the selected radio button appear as ticked, so that the user knows which he selected before ?

here's my code

<ul>     <li style="list-style-type: none;">     <div align="center" class="radio_group">     <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" /> Gallery Link - In the navigation of my website, display one "gallery" link<br />     <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" /> Category Links - In the navigation of my website, display a separate link to each category. </div>     </li> </ul> 

回答1:

When you are rendering the page for the user in PHP you would need to do a query to where you have saved the selection (presumably a database). If the user has selected that particular radio button then you would display it as selected. Code would be something like:

// Do a database query or something to get the value that the user has stored before (if any) <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if ($gallerymenustyleFromDatabaseValue == 1){ echo 'selected'; }/> Gallery Link....<br /> <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="2" <?php if ($gallerymenustyleFromDatabaseValue == 2){ echo 'selected'; }/> Category Link.... 


回答2:

If the user is logged in you should query the database to get the value whether the button was ticked.

If the user isn't logged in you could use the session id. Although this only works until the user closes his browser / the session expires.

<?php $checked = 0; // this should come from db $checked = 1; ?>  <ul>   <li style="list-style-type: none;">         <div align="center" class="radio_group">       <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if ($checked == 1) print('checked="checked"') ?>/> Gallery Link - In the navigation of my website, display one "gallery" link<br />       <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" <?php if ($checked == 1) print('checked="checked"') ?>/> Category Links - In the navigation of my website, display a separate link to each category.     </div>   </li> </ul> 

Will check the first radio button



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!