Inserting the selection of radio buttons into MySQL

寵の児 提交于 2019-12-06 12:34:12

问题


Unfortunately I haven't been able to find exactly what I'm looking for online to resolve this.

At any rate, I have three radio buttons and I need to push the selection of a single button selected from the list into MySQL.

Currently using the below as my radio buttons & submit:

   <input class="radiobutton" type="radio" name="1" id="1">1</div>
   <input class="radiobutton" type="radio" name="2" id="2">2</div>
   <input class="radiobutton" type="radio" name="3" id="3">3</div>
   <input type="submit" name="submit" value="Update">`

Can someone help me with the syntax to grab the selected radio button and insert it into MySQL?

An example of the syntax would be UPDATE tblwhatever.setting SET value='$x' where setting='Y';


回答1:


Consider the following:

<input class="radiobutton" type="radio" name="radio" id="1" value="1" onclick="document.getElementById('2').checked = false;"/>
<input class="radiobutton" type="radio" name="radio" id="2" value="2" onclick="document.getElementById('1').checked = false;"/>

Note that i gave them the same name attribute. When the form is submitted we will retrieve $_POST['radio'] and the value will determine which radio button was selected. The onclick attribute will uncheck the other radio button if selected.. eg: if radio id="1" is being checked uncheck id="2"




回答2:


    <form method='POST' >
<input class="radiobutton" type="radio" 

name="radio" id="1" value='1' checked>
       <input class="radiobutton" type="radio" 

name="radio" id="2" value='2'>
       <input class="radiobutton" type="radio" 

name="radio" id="3" value='3'>
       <input type="submit" name="submit" 

value="Update">`

</form>

in php code should be like this :

<?
    $radio = $_POST['radio'];
    $query = mysql_query('update "your table" set column1=$radio WHERE some_column=some_value);
?>


来源:https://stackoverflow.com/questions/13042001/inserting-the-selection-of-radio-buttons-into-mysql

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