dprxj1995 2015-10-21 05:43
浏览 56
已采纳

如何使用jquery和php将表单数据保存到sql

I have tried number solutions and cannot figure out what I am doing wrong.

I have the following input element:

<input type="checkbox" data-layout="fixed" class="pull-right" value="1">

just before my closing body tag I have the following jquery

<script>
  $("input[type='checkbox']").on('click', function(){
  var checked = $(this).attr('checked');
     if(checked){
       var value = $(this).val();
         $.post('mysqlfunc.php', { value:value }, function(data){
              if(data == 1){
              }
         });
     }
  });
</script>

In mysqlfunc.php file I have

<?php
    include "../../inc/config.php";
    include "../../inc/funcs.php";
    @mysql_connect($dbhost,$dbuser,$dbpass);
    @mysql_select_db($dbname) or die( "Unable to select database");
    include "../../inc/userauth.php";

    if ($_POST && isset($_POST['value'])) {
    $value = mysql_real_escape_string($_POST['value']);
    $sql = "UPDATE members SET fixed = '".$value."' WHERE username='".$_SESSION['uid']."'";
    } else {
    }
?>

My expectation is when I click the input checkbox, a value "1" is saved into the "members" table in the "fixed" column where the username is equal to the session uid.

I can echo the uid, so that is working. Its not giving any errors, so its connecting to the database it seems. Yet checking the input textbox, does not change the value in the database.

I get no js errors at all, I get no php errors, just not saving.

What am I doing wrong? Example working code if at all possible would be very helpful.

  • 写回答

4条回答 默认 最新

  • duanhu7400 2015-10-21 05:55
    关注

    This line in your script is wrong.

    var checked = $(this).attr('checked');
    

    change it to

    var checked = $(this).is(':checked')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题