1

# I just want to know how to send this array the controller by requete ajax

var dataPanier=[];

function addarray(objser)
{
 dataPanier.push(objser);

}


$('.target').click(function() {

  var btn_name=$(this).attr("name");

     switch(btn_name) {

       case 'FormPVC':

       var dataformPVC = new FormData(),
           form_data = $('#'+btn_name).serializeArray();

          $.each(form_data, function (key, input) {
              dataformPVC.append(input.name, input.value);
           });
          dataformPVC.append('Fichier', $('#File_PVC')[0].files[0]);
/* function addarray push dataform in array*/
           addarray(dataformPVC);
break;
.
.
.
more . . .

I am attempting to send multiple forms data as an array by ajax to a Larave controller.

$.ajax({
    type: 'POST',
    url: 'lsitedevis',
    data: array ,
    success: function(data) {
        toastr.success('Successfully added Post!', 'Success Alert', {timeOut: 5000});

    }
});
4
  • This might be helpful: stackoverflow.com/questions/19208699/… Commented May 23, 2019 at 1:39
  • yes I saw, but I don't know how many data to add to the table, more I used the formdata() Commented May 23, 2019 at 1:50
  • You have multiple forms on the same page, and you want to submit every forms data in a single request? Commented May 23, 2019 at 3:46
  • yes , I have more forms that contain the input type " file " ,and I want to push this data type formdata() to array in order to send a single request . Commented May 23, 2019 at 15:35

2 Answers 2

1

$("#btnTest").click(function(){
var formData = $('#frm1, #frm2').serialize();
console.log(formData);
$.ajax({
    method: 'POST',
    url: 'lsitedevis',
    data: formData ,
    success: function(data) {
     toastr.success('Successfully added Post!', 'Success Alert', {timeOut: 5000});
    }
    
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="frm1">
<input name="n1" type="text"/>
<input name="n2" type="hidden" value="test2"/>
<input name="n3" type="hidden" value="test3"/>

</form>

<form id="frm2">
<input name="n1" type="text" />
<input name="n2" type="hidden" value="test2"/>
<input name="n3" type="hidden" value="test3"/>

</form>

<input type="button" id="btnTest" value="send"/>

Sign up to request clarification or add additional context in comments.

Comments

0

As your already using jQuery for the AJAX request, you could use the serialize() function. This supports multiple form elements, so it is possible to do:

var formData = $('#form1, #form2').serialize();

$.ajax({
    type: 'POST',
    url: 'lsitedevis',
    data: formData ,
    success: function(data) {
        toastr.success('Successfully added Post!', 'Success Alert', {timeOut: 5000});
    }
});

You might want to ask yourself why you have multiple forms but submitting them all as a single request. If it's for visual purposes, it might be easier to have a single form and separate the contents using other markup elements such as a <fieldset> or <div>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.