AJAX发布不从表单数据发送值

时间:2017-03-28 14:14:39

标签: javascript php jquery ajax

我有奇怪的情况,我已经创建了具有静态值的AJAX帖子,并且它工作正常。现在我尝试使用HTML输入的动态值的AJAX帖子但不起作用。 如果我尝试插入value="1"它将POST 1发送到php。 任何人都可以查看我的代码吗?

<script>

$(document).ready(function () {   
$('#unggah<?php echo $mhs?>').submit(function (event) {

 var formData = {
            'jmlmhs': $('input[name=jmlmhs]').val(),
            'mulaikul': $('input[name=mulaikul]').val(),
            'akhirkul': $('input[name=akhirkul]').val()
        };

   $.ajax({
            type: 'POST',
            url: '<?=base_url()?>operator_pt/unggah/<?php echo $proses.'_'.$CL?>',
            data: formData,
            dataType: 'json',
            encode: true
        })          
 event.preventDefault();

     });

});
</script>

HTML

<form action="<?=base_url()?>operator_pt/unggah/<?php echo $proses.'_'.$CL?>" method="POST" role="form" id="unggah<?php echo $mhs?>">
<label for="jmlmhs">Jumlah Mahasiswa Diterima</label>
<div id="jmlmhs" class="form-group">
<input type="text" class="form-control" id="jmlmhs" name="jmlmhs" value="">       
</div>

<label for="datePicker">Tanggal Mulai Kuliah</label>
<div class="input-group date" data-provide="datepicker" id="datePicker">
    <input type="text" class="form-control" name="mulaikul" value="">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

<label for="datePicker1">Tanggal Akhir Kuliah</label>
<div class="input-group date" data-provide="datepicker" id="datePicker1">
    <input type="text" class="form-control" name="akhirkul" value="">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

 <button type="submit" id="button1<?php echo $mhs?>" class="btn btn-primary" ><?php echo $tombol ?></button>

</form>

2 个答案:

答案 0 :(得分:0)

您需要在css选择器中的名称周围加上引号:

 var formData = {
    'jmlmhs': $('input[name="jmlmhs"]').val(),
    'mulaikul': $('input[name="mulaikul"]').val(),
    'akhirkul': $('input[name="akhirkul"]').val()
 };

答案 1 :(得分:0)

我刚修好它,只需删除this.reset();,一切顺利。我甚至不知道为什么我首先添加this.reset();。我忘了在问题上插入this.reset();代码。所以人们也无法帮助我。