栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

复选框数据在单击时动态保存到数据库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

复选框数据在单击时动态保存到数据库

使用jQuery来做,一个简单的例子可能是:

HTML:

<input type="checkbox" name="option1" value="Milk"><input type="checkbox" name="option2" value="Sugar"><input type="checkbox" name="option3" value="Chocolate">

JS:

$("input[type='checkbox']").on('click', function(){   var checked = $(this).attr('checked');   if(checked){      var value = $(this).val();      $.post('file.php', { value:value }, function(data){          // data = 0 - means that there was an error          // data = 1 - means that everything is ok          if(data == 1){  // Do something or do nothing :-)  alert('Data was saved in db!');          }      });   }});

PHP:file.php

<?phpif ($_POST && isset($_POST['value'])) {    // db connection    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');    if (!$link) {       // error happened       print(0);    }    mysql_select_db('mydb');    // sanitize the value    $value = mysql_real_escape_string($_POST['value']);    // start the query    $sql = "INSERT INTO table (value) VALUES ('$value')";    // check if the query was executed    if(mysql_query($sql, $link)){       // everything is Ok, the data was inserted       print(1);        } else {       // error happened       print(0);    }}?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号