2017-06-21 71 views
0

問題: becaue文件由ajax上傳,發佈信息以不同形式完成。我如何從#editpost表單中獲取隱藏的帖子值並將其添加到ajax中?從其他表單獲取隱藏表單數據並將其添加到ajax

我上傳使用這個腳本

<script type="text/javascript"> 
$('#upload-attachment').on('click', function() { 
    //$('#form-upload').remove(); 

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>'); 

    $('#form-upload input[name=\'file\']').trigger('click'); 

    $('#form-upload input[name=\'file\']').on('change', function() { 
     $.ajax({ 
      url: "<?php echo base_url('extensions/attachments/upload');?>", 
      type: 'post',  
      dataType: 'json', 
      data: new FormData($(this).parent()[0]), 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(json) { 
       if (json['success'] == false) 
       { 
        alert(json['error']) 
       } 
      } 
     }); 
    }); 
}); 
</script> 

這是對這一觀點

<div class="container"> 
<div class="row"> 
<div class="col-sm-12 col-xs-12"> 
<?php echo form_open_multipart('editthread/' . $post_id, array('role' => 'form', 'id' => 'editpost'));?> 
<div class="panel panel-default editpost-panel"> 
<div class="panel-heading"></div> 
<div class="panel-body"> 

<input type="hidden" name="post_id" value="<?php echo $post_id;?>" id="post_id"> 

<div class="form-group"> 
<label>Question title</label> 
<?php echo form_input('title', $title, array('class' => 'form-control'));?> 
<?php echo form_error('title', '<div class="error text-danger">', '</div>');?> 
</div> 

<div class="form-group"> 
<label>Message/Question</label> 
<?php echo form_textarea('message', $message, array('class' => 'form-control'));?> 
<?php echo form_error('message', '<div class="error text-danger">', '</div>');?> 
</div> 


<div class="form-group"> 
<label>Edit Reason</label> 
<?php 

$options = array(
    'name' => 'editreason', 
    'rows' => '3', 
    'cols' => '40', 
    'class' => 'form-control', 
    'value'=> $editreason 
); 

echo form_textarea($options);?> 
<?php echo form_error('editreason', '<div class="error text-danger">', '</div>');?> 
</div> 

<div class="form-group editpost-buttons"> 
<button type="button" class="btn btn-default" id="upload-attachment">Add Attachment</button> 
<a href="" role="button" class="btn btn-default">Cancel Update</a> 
<button type="submit" class="btn btn-primary">Update Post</button> 
</div> 

</div> 
</div> 
<?php echo form_close();?> 
</div> 
</div> 
</div> 

<script type="text/javascript"> 
$('#upload-attachment').on('click', function() { 
    //$('#form-upload').remove(); 

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>'); 

    $('#form-upload input[name=\'file\']').trigger('click'); 

    $('#form-upload input[name=\'file\']').on('change', function() { 
     $.ajax({ 
      url: "<?php echo base_url('extensions/attachments/upload');?>", 
      type: 'post',  
      dataType: 'json', 
      data: new FormData($(this).parent()[0]), 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(json) { 
       if (json['success'] == false) 
       { 
        alert(json['error']) 
       } 
      } 
     }); 
    }); 
}); 
</script> 

問題的文件: Becaue該文件由AJAX和上傳發布以不同形式完成的信息。我如何從#editpost表單中獲取隱藏的帖子值並將其添加到ajax中?

回答

2

追加數據爲形式的數據,試試這個

然後在你阿賈克斯:

$.ajax({ 
    . 
    . 
    . 
    data: formData 
}) 

來源:FormData.append

相關問題