CodeIgniter Forums
Combine input val ajax from two forms. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Combine input val ajax from two forms. (/showthread.php?tid=67671)



Combine input val ajax from two forms. - wolfgang1983 - 03-23-2017

When user asks a question on my form and needs to upload a image they can by clicking on button and opens a bootstrap modal. When the user ask a question that question has its own id.

I would like to know how to combine these to on my ajax code below

Code:
var form_upload = new FormData($('#form-upload')[0]);
var question_code = $('#form-ask #question_code').val()

Question: How to combine question code val with the form upload?


Code:
$('input[id=\'file\']').on('change', function(e) {
    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {
        if ($('input[id=\'file\']').val() != '') {
            clearInterval(timer);

            // Form-upload is in bootstrap modal
            var form_upload = new FormData($('#form-upload')[0]);
            var question_code = $('#form-ask #question_code').val()

            $.ajax({
                url: "<?php echo base_url('question/ask/upload');?>",
                type: 'post',
                dataType: 'json',
                data: form_upload,
                cache: false,
                contentType: false,
                processData: false,
                success: function(json) {
                    if (json['error']) {
                        alert(json['error']);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
    }, 500);
});



RE: Combine input val ajax from two forms. - spjonez - 03-23-2017

Code:
form_upload.append( 'fieldname', question_code );