douzhiling3166 2018-03-02 09:40 采纳率: 100%
浏览 53
已采纳

每次添加新数据时,select选项中的所有值都将保存到所有数据库。 我想只保存所选的选项。 Php,Javascript [关闭]

i have this javascript CODE in index.php:

$(document).on('blur', '.first_name', function(){  
           var id = $(this).data("id1");  
           var first_name = $(this).text();  
           edit_data(id, first_name, "first_name");  
      });  
      $(document).on('blur', '.last_name', function(){  
           var id = $(this).data("id2");  
           var last_name = $(this).text();  
           edit_data(id,last_name, "last_name");  
      });
      $(document).on('blur', '.gender', function(){  
           var id = $(this).data("id3");  
           var gender= $(this).text();  
           edit_data(id,gender, "gender");  
      });

AND in select.php:

while($row = mysqli_fetch_array($result))  
      {  
           $output .= '  
                <tr>  
                     <td class="first_name" data-id1="'.$row["id"].'" contenteditable>'.$row["first_name"].'</td>  
                     <td class="last_name" data-id2="'.$row["id"].'" contenteditable>'.$row["last_name"].'</td>  
                     <td><select class="type" data-id3="'.$row["id"].'">  
                               <option>'.$row["gender"].'</option>  
                               <option>male</option>
                               <option>female</option>                                 
                          </select></td> 
                     <td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>  
                </tr>  
           ';  
      }  

ALSO in edit.php here:

<?php  
 $connect = mysqli_connect("localhost", "root", "", "test");  
 $id = $_POST["id"];  
 $text = $_POST["text"];  
 $column_name = $_POST["column_name"];  
 $sql = "UPDATE tbl_sample SET ".$column_name."='".$text."' WHERE id='".$id."'";  
 if(mysqli_query($connect, $sql))  
 {  
      echo 'Data Updated';  
 }  
 ?>

My question is, everytime i add new data, all the text in gender option will save to database. i want to save only the selected option. Please help me.

  • 写回答

2条回答 默认 最新

  • duanba5777 2018-03-02 09:54
    关注

    all the text in gender option will save to database

    Well yes, this is what happens when you use $(this).text();

    Gender is a select input, not a text field (where .text() works fine). Therefore you should use

    var gender= $(this).val();
    

    Edit: it's .val(), not .value()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算