【问题标题】:inserting multiple row of medicine using jquery and php codeigniter使用 jquery 和 php codeigniter 插入多行药物
【发布时间】:2020-12-12 22:41:30
【问题描述】:

如果我想添加单击按钮而不是打开下一个药物输入字段而不是填充,我想添加一次插入一行的药物,如果一次 5 次药物输入比 5 次插入数据库,则插入下一行。但这里只有一个将数据插入数据库的时间复选框不起作用只接受第一个值

查看页面

我需要数据库

name      quantity     days     take medician
----------------------------------------------
Amit       1            2        morning,evening
-----------------------------------------------
Amitabh    2            3        evening,night
-----------------------------------------------
Amitabh    3            4        afternoon,night
kumar 
gupta

插入当前数据库

name      quantity     days     take medician
----------------------------------------------
Amit       1            2        morning
-----------------------------------------------

html查看页面

 <tbody>
        <?php foreach ($app_booking as $row){ ?>
        <input type="hidden" name="doctorname" id="doctorname" value="<?php echo $row["doctor_name"];?>">
        <input type="hidden" id="doctorid" name="doctorid" value="<?php echo $row["doctor_id"];?>">
        <input type="hidden" id="appid"  name="appid" value="<?php echo $row["appointment_id"];?>">
        <input type="hidden" id="uid" name="uid" value="<?php echo $row["user_id"];?>">
    <?php };?>
        <tr>
        <td><input class="form-control" type="text" name="name" id="name" required=""></td>
        <td><input class="form-control" type="text" id="quantity" name="quantity" required=""></td>
        <td><input class="form-control" type="text" name="days" id="days"  required=""></td>
        <td>
            <div class="form-check form-check-inline">
             <label class="form-check-label">                                               
            <input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning
            </label>
        </div>
        <div class="form-check form-check-inline">
            <label class="form-check-label">
            <input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon
            </label>
            </div>
        <div class="form-check form-check-inline">
        <label class="form-check-label">
            <input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening
            </label>
        </div>
        <div class="form-check form-check-inline">
        <label class="form-check-label">
        <input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" value="Night" id="night"> Night
        </label>
        </div>
        </td>
        <td>
        <a href="#"  name="add" id="btn1" value="Add" class="btn bg-success-light"><i class="fas fa-plus-circle"></i> Add Item</a>
        </td>
        <button type="button" onclick="save_medical_records();" class="btn btn-primary submit-btn">Save</button>
            </tr>
        </tbody>

ajax 代码

 function save_medical_records()
           {
               
               var doctorname           =   $("input[name='doctorname']").val();
               var doctorid             =   $("input[name='doctorid']").val();  
               var appid                =   $("input[name='appid']").val();
               var uid                  =   $("input[name='uid']").val();
               var name                 =   $("input[name='name']").val();             
               var quantity             =   $("input[name='quantity']").val();
               var days                 =   $("input[name='days']").val();
               
                
                const take_meds = $("[name='take_medicine[]']:checked").map(function() { return this.value}).get()
                 console.log(take_meds);
                
               
                   $.ajax({
                       url:"<?php echo base_url() ?>add-prescription",
                       data:"doctorname="+doctorname+"&doctorid="+doctorid+"&appid="+appid+"&uid="+uid+"&name="+name+"&quantity="+quantity+"&days="+days+"&take_meds="+take_meds,
                       //data: $("[name=doctorname]").serialize(),$("[name=doctorid]").serialize(),$("[name=appid]").serialize(),$("[name=uid]").serialize(),$("[name=name]").serialize(),$("[name=quantity]").serialize(),$("[name=days]").serialize(),$("[name=take_meds]").serialize(),
                       
                       
                       
                       type:"post",
                       success:function(response){ 
                           if(response==1)
                              
                              $("#msg").css("display","block"); 
                           
                       }
                       });
               }

            
    

控制器

public function add_prescription()
    {
        $result = $this->doctor_health_model->add_prescription();
        echo $result;

    }

模型

public function add_prescription()
    {
        $db2 = $this->load->database('dpr',TRUE);
        $medicine       = $this->input->post('name');
        $uid            = $this->input->post('uid');
        $appid          = $this->input->post('appid');
        $doctor_id      = $this->input->post('doctorid');
        $doctor_name    = $this->input->post('doctorname');
        $quantity       = $this->input->post('quantity');
        $days           = $this->input->post('days');
        $take_medicine  = $this->input->post('take_medicine');  
        $today_date     = date("Y-m-d");
        
     $insert =$db2->query('INSERT INTO dpr_save_farmacytest (medicine,user_id,appointment_id,doctor_id,doctor_name,quantity,days,take_medicine_time,created_date) 
            VALUES ("'.$medicine.'","'.$uid.'","'.$appid.'","'.$doctor_id.'","'.$doctor_name.'","'.$quantity.'","'.$days.'","'.$take_medicine.'","'.$today_date.'")');
            //echo $db2->last_query();
            return $insert;

        }
    }

【问题讨论】:

  • 这就是 jQuery 的工作方式 - 这将返回一个值,从第一个具有相同名称的元素 var take_medicine = $("input[name='take_medicine[]']").val();
  • 根据该方法的 jQuery 文档,.val() 仅从任何集合中的第一个匹配元素中获取值。因此,如果$("input[name='take_medicine[]']") 匹配许多复选框,.val() 只会从第一个复选框中获取值。老实说,与其尝试从表单中一个一个地提取所有数据,不如直接使用 jQuery 的serialize() 方法来自动处理它? api.jquery.com/serialize
  • P.S. 警告:您的代码容易受到 SQL 注入攻击。您应该使用参数化查询和准备好的语句来帮助防止攻击者通过使用恶意输入值来破坏您的数据库。 bobby-tables.com 给出了风险解释,以及如何使用 PHP / mysqli 安全地编写查询的一些示例。 切勿将未经处理的数据直接插入您的 SQL 中。按照您现在编写代码的方式,有人可以轻松窃取、错误更改甚至删除您的数据。
  • P.P.S.你的数据库也是非规范化的——你不应该在这样的单个字段中存储多个值(例如morning, night)。请在继续之前阅读关系数据库设计。
  • 请先生分享编辑代码

标签: php jquery ajax codeigniter


【解决方案1】:

除了 SQL 注入问题、在一个表字段中存储多个值等问题之外,您的直接问题由以下人员回答:

这就是 jQuery 的工作原理 - 这将返回一个值,从第一个具有相同名称的元素开始

var take_medicine =   $("input[name='take_medicine[]']").val();

您需要获取每个选中的复选框 - 例如

const take_meds = $("[name='take_medicine[]']:checked")
       .map(function() { return this.value}).get();
console.log(take_meds);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-check form-check-inline">
  <label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning</label>
</div>
<div class="form-check form-check-inline">
  <label class="form-check-label"><input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon</label>
</div>
<div class="form-check form-check-inline">
  <label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening</label>
</div>

如果您使用serialize,您可能会过上更简单的生活

如果您有 &lt;form id="myForm" - 您将整个 AJAX 代码更改为

$("#myForm").on("submit", function(e) {
  e.preventDefault();
  $.ajax({
    url: "<?php echo base_url() ?>add-prescription",
    data: $(this).serialize(),
    type: "post",
    success: function(response) {
      if (response == 1) $("#msg").css("display", "block");
    },
    error: function(jxhr) {
      console.log("Error", jxhr)
    }
  })
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 2014-02-25
  • 2012-01-04
  • 2021-04-16
  • 2012-06-02
  • 2019-07-21
相关资源
最近更新 更多