PHP - 单击单选按钮时如何将数据更新到 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1626730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - How to update data to MySQL when click a radio button
提问by wow
Example to save gender
保存性别的示例
<form action="save.php?id=<?=$id?>" method="post">
<p><label><input name="gender" type="radio" value="male" <?php if($gender=='male'){?>checked="checked"<? }?> /> Male</label></p>
<p><label><input name="gender" type="radio" value="female" <?php if($gender=='female'){?>checked="checked"<? }?> /> Female</label></p>
</form>
Here an example to update the value
这是一个更新值的示例
if ($_REQUEST['gender']) {
mysql_query("UPDATE users SET gender='$gender' WHERE id='" . $id . "'") or die(mysql_error());
}
How to make when we click on the gender the value will auto save to the db. Let me know.
当我们点击性别时,如何使该值自动保存到数据库中。让我知道。
回答by karim79
Something to set you off on a prettier path:
一些让你走上更漂亮道路的东西:
// $_POST is way cooler than $_REQUEST
if (isset($_POST['gender']) && !empty($_POST['gender'])) {
// sql injection sucks
$gender = my_real_escape_string($_POST['gender']);
// cast it as an integer, sql inject impossible
$id = intval($_GET['id']);
if($id) {
// spit out the boolean INSERT result for use by client side JS
if(mysql_query("UPDATE users SET gender=$gender WHERE id=$id")) {
echo '1';
exit;
} else {
echo '0';
exit;
}
}
}
Assuming the same markup, an ajaxy solution (using jQuery):
假设相同的标记,ajaxy 解决方案(使用jQuery):
<script>
var id = <?=$id?>;
// when the DOM is ready
$(document).ready(function() {
// 'click' because IE likes to choke on 'change'
$('input[name=gender]').click(function(e) {
// prevent normal, boring, tedious form submission
e.preventDefault();
// send it to the server out-of-band with XHR
$.post('save.php?id=' + id, function() {
data: $(this).val(),
success: function(resp) {
if(resp == '1') {
alert('Saved successfully');
} else {
alert('Oops, something went wrong!');
}
}
});
});
});
</script>
回答by ty812
You can't do this with PHP alone ... you'll need some JavaScript on that page which executes onchangedof the radiobutton(s) and executes a PHP script. This is called Asynchronous JavaScript and XML or "AJAX", and a quick introduction would be http://www.w3schools.com/ajax/default.asp
你不能单独用 PHP 做到这一点......你需要在该页面上使用一些 JavaScript,该页面执行onchanged单选按钮并执行 PHP 脚本。这称为异步 JavaScript 和 XML 或“AJAX”,快速介绍将是http://www.w3schools.com/ajax/default.asp
回答by Mir Nazim
+1 to karim79 for pointing out jQuery/AJAX and $_POST thingy. Very important.
+1 到 karim79 指出 jQuery/AJAX 和 $_POST 东西。很重要。
Here is a solution without jQuery(if you are not interested in learning jQuery right now)
这是一个没有 jQuery 的解决方案(如果你现在对学习 jQuery 不感兴趣)
Step 1:Add an onchangeeven on your checkbox tags like this:
第 1 步:即使在您的复选框标签上也添加一个onchange,如下所示:
<p><label><input name="gender" type="radio" value="male" onchange="do_submit()" <?php if($_POST['gender']=='male'){?>checked="checked"<? }?> /> Male</label></p>
<p><label><input name="gender" type="radio" value="female" onchange="do_submit()" <?php if($_POST['gender']=='female'){?>checked="checked"<? }?> /> Female</label></p>
Step 3:Add a nameattribute to form tag like this:
第 3 步:将name属性添加到表单标签中,如下所示:
<form name="myform" action="check.php" method="post">
Step 3:Write the onchange event handler function in javascript:
第 3 步:在 javascript 中编写 onchange 事件处理函数:
<script type="text/javascript">
function do_submit() {
document.forms['myform'].submit();
}
</script>
Couple of important things to note.
需要注意的几个重要事项。
- $_POST is a better option than $_REQUEST.
- Use
<?phpinstead of short form of php tag<?. It will be deprecated in future versions of php. - Investing time in learning jQuery/AJAX is 100% worth the time and effort
- $_POST 是比 $_REQUEST 更好的选择。
- 使用
<?phpphp 标签的缩写代替<?。它将在未来版本的 php 中弃用。 - 花时间学习 jQuery/AJAX 是 100% 值得的时间和精力

