从另一个表单中获取隐藏的表单数据并将其添加到ajax

时间:2017-06-21 00:25:44

标签: ajax codeigniter

  

问题:这个文件是由ajax上传的,而且帖子信息是以不同的形式完成的。如何从#editpost表单中获取隐藏的帖子值并将其添加到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>
  

问题:这个文件是由ajax上传的,而且帖子信息是以不同的形式完成的。如何从#editpost表单中获取隐藏的帖子值并将其添加到ajax?

1 个答案:

答案 0 :(得分:2)

将数据附加到表单数据中,试试这个

var formData = new FormData($(this).parent()[0]);
formData.append('input_name',$("#editform #hidden_name_field").val());

然后在你的ajax:

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

来源:FormData.append