Codeigniter : unable to post values by form set by jquery

时间:2018-03-23 02:11:08

标签: php jquery codeigniter

i am setting values in form using jquery and posting it using it form . this is how i am doing it

this is the data to be edited

<div class="col-xs-12">
    <?php $image =base_url()."assets/front/img/".$slider[0]->image; ?>
    <h3>Slider image </h3>
    <img src="<?=$image?>" alt="" width="300" height="200">
    <br/><br/>
    <p class="caption bold" ><?php echo $caption =$slider[0]->caption; ?></p>
    <br/>
    <button id="" type="button" id="btn" class="btn btn-default" data-toggle="modal" data-target="#myModal" data-id="<?=$slider[0]->id;?>">Update image</button>

</div>


 <form  method="post" enctype="multipart/form-data" action ="<?=base_url()?>admin/home/index" >
                     <input type="text" name="caption"  class="caption form-control" />

                     <br/>
                     <label>Upload Picture </label>

                     <input type='file'  name='userfile' />
                     <input type="hidden" name="updateimage">
                     <input type="hidden" class="slider" name="id"  value=""  />
                     <input type="submit" class="btn btn-primary pull-right" />
                 </form>

this is how i am setting values in jquery

$(document).ready(function () {
        $('.btn').on('click',function () {

           var id = $(this).attr('data-id');
           $("input.slider").val(id);
           var caption =   $(this).siblings('p').text();
           $('.caption').val(caption);
        })
    })

problem is that when i post values posted array is empty.

if i set text field value using value="" it works .

Plese help me to sort it out

1 个答案:

答案 0 :(得分:0)

尝试更改:

$('.btn').on('click',function () {

$('#btn').on('click',function () {

所以点击监听器将获得正确的按钮

修改

还可以编辑按钮id(多个id):

<button id="" type="button" id="btn" class="btn btn-default" data-toggle="modal" data-target="#myModal" data-id="<?=$slider[0]->id;?>">Update image</button>  

<button type="button" id="btn" class="btn btn-default" data-toggle="modal" data-target="#myModal" data-id="<?=$slider[0]->id;?>">Update image</button>  

因为这可能是输入数据未被发送的原因。