【问题标题】:send data attribute input select option发送数据属性输入选择选项
【发布时间】:2016-09-27 13:14:50
【问题描述】:

我有一个包含 3 个input type="text 和 1 个select option 的编辑表单。当我尝试更新到数据库表时,input text 的值工作正常。但是select 选项有问题。

我在 html button 中使用data-* 属性:

<?php  foreach ($driver as $d) : ?>
<button class="btn btn-warning edit_button"
 data-toggle="modal" 
data-target="#edit_driver_modal" 
data-id="<?php echo $d['id'];?>" 
data-email="<?php echo $d['email'];?>" 
data-name="<?php echo $d['name'];?>" 
data-phone="<?php echo $d['phone'];?>" 
data-locationid="<?php echo $d['location_id'];?>" 
data-location="<?php echo $d['location_name'];?>">
<span class="glyphicon glyphicon-pencil"></span></button>
<?php endforeach ; ?>

Modal 中的Edit 表单:

<form method="post" action="<?php echo base_url(); ?>admin/edit_driver">
          <div class="modal-body" id='add'>
            <div class="row" id="form_pesan">
                <input type="hidden" name="id" class="form-control id" />

                <div class="col-sm-6">
                <input type="email" class="form-control input-lg email" name="email">
                </div>

                <div class="col-sm-6">
                <input type="text" class="form-control input-lg name" name="name" >
                </div>

                <div class="col-sm-6">
                <input type="text" class="form-control input-lg phone" name="phone">
                </div>

                <div class="col-sm-6">
                    <select class="form-control" id="location_name" name="location_name">
                    <?php foreach ($vendor as $v) { 
                        ?>
                        <option id="<?php echo $v['location_id']; ?>" value="<?php echo $v['location_id']?>" ><?php echo $v['name'] ;?></option>
                    <?php }; ?> 
                    </select>
                </div>

            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button id="add" type="submit" class="btn btn-primary btn-md">Save</button>
          </div>

我的 jquery:

$(document).on( "click", '.edit_button',function(e) {

    var email = $(this).data('email');
    var id = $(this).data('id');
    var name = $(this).data('name');
    var phone = $(this).data('phone');
    var location = $(this).data('location');
    var locationid = $(this).data('locationid')

    $(".id").val(id);
    $(".email").val(email);
    $(".name").val(name);
    $(".phone").val(phone);
    $(".location").val(location);
    $(".locationid option:selected").val(locationid);

});

控制器:

public function edit_driver()
{
    if(!$this->user_permission->check_permission())return;

        $user_email                 =   $this->input->post('email');
        $id                         =   $this->input->post('id');
        $phone                      =   $this->input->post('phone');
        $location_id                =   $this->input->post('location_id');

        $data_user = array(
            'user_email'            => $user_email,
            'user_id'               => $id,
            'user_phone'            => $phone
        );

        $this->db->where('user_id',$id);
        $this->db->update('user', $data_user);

    if($this->db->affected_rows() > 0){

        $data_driver = array(
            'user_id'               => $id,
            'location_id'           => $location_id
            );

        $this->db->where('user_id',$id);
        $this->db->update('user_driver',$data_driver);

        redirect('admin/user/user_driver');
    }
}

它抛出 database error: location_id cannot be null 是因为我的代码不知何故不会将 location_id 发送到控制器,但像 email namephone 这样的其他值可以正常工作。我不知道如何解决这个问题。

另外,我添加了存储 select 查询的控制器:

$this->db->select("d.user_id as id, d.plate_number as plate_number, d.current_lat as lat, d.current_lon as lon, d.created_on as created_on, d.updated_on as updated_on, d.available as available, d.location_id as location_id, u.user_name as name, u.user_email as email, u.user_phone as phone, v.name as location_name");
        $this->db->from('user_driver as d');
        $this->db->join('user as u', 'd.user_id = u.user_id','left');
        $this->db->join('vendor_location as v', 'd.location_id = v.location_id','left');
        $query = $this->db->get();
        $data['driver'] = $query->result_array();

        $this->db->select("location_id, name");
        $this->db->from('vendor_location');
        $query2 = $this->db->get();
        $data['vendor'] = $query2->result_array();

【问题讨论】:

  • 您忘记添加$data_user = array('location_id' =&gt; $location_id);,它丢失了,只需在更新前添加该密钥对
  • 当我 update('user') 它没有更新 location_id 因为没有名为 location_id 的列。 location_id 应在 update('user_driver') 表中更新。对不起,如果我的问题对你来说不够清楚@Ghost
  • 只需在user_driver更新之前添加location_id$data['location_id'] = $location_id,只需添加该声明,如果user未包含在user中,则无需在更新之前添加该声明跨度>
  • 我做了你的建议,但它在第 1 行给了我Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 = NULL WHERE user_id` = '293'' UPDATE user_driver SET user_id = '293', 0 = NULL WHERE user_id = '293'`

标签: php jquery html codeigniter custom-data-attribute


【解决方案1】:

[已解决] 我将 $(this).find(':selected').attr('data-id') 添加到我的 jquery 中,并将 &lt;select&gt; 中的 idname 属性也更改为 locationid

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 2013-01-12
    • 2020-11-03
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多