jQuery find all input type with select for ajax request












2














I am trying to perform an ajax request by setting FormData using jquery each loop after finding input and select element for update as follows:



$(document).on("click", ".update", function (e) {
e.preventDefault();
e.stopPropagation();

let thisBtn = $(this);
//Form Data
let formData = new FormData();
let thisRow = thisBtn.closest("tr");
thisRow.find("input,select").each(function() {
//console.log(this.value)
formData.append($(this).attr('name'), $(this).val());
});

$.ajax({
type: "POST",
url: '<?php echo base_url()?>exam/update',
data: formData,
processData: false,
contentType: false,
success:function(data){
if($.trim(data)=='yes')
{
alert('Success! Record updated successfully');
}
else
{
alert('Error! Record not updated successfully')
}
}
});

});


But getting some undefined params as follows:



enter image description here



But I want pure parameters except undefined










share|improve this question





























    2














    I am trying to perform an ajax request by setting FormData using jquery each loop after finding input and select element for update as follows:



    $(document).on("click", ".update", function (e) {
    e.preventDefault();
    e.stopPropagation();

    let thisBtn = $(this);
    //Form Data
    let formData = new FormData();
    let thisRow = thisBtn.closest("tr");
    thisRow.find("input,select").each(function() {
    //console.log(this.value)
    formData.append($(this).attr('name'), $(this).val());
    });

    $.ajax({
    type: "POST",
    url: '<?php echo base_url()?>exam/update',
    data: formData,
    processData: false,
    contentType: false,
    success:function(data){
    if($.trim(data)=='yes')
    {
    alert('Success! Record updated successfully');
    }
    else
    {
    alert('Error! Record not updated successfully')
    }
    }
    });

    });


    But getting some undefined params as follows:



    enter image description here



    But I want pure parameters except undefined










    share|improve this question



























      2












      2








      2







      I am trying to perform an ajax request by setting FormData using jquery each loop after finding input and select element for update as follows:



      $(document).on("click", ".update", function (e) {
      e.preventDefault();
      e.stopPropagation();

      let thisBtn = $(this);
      //Form Data
      let formData = new FormData();
      let thisRow = thisBtn.closest("tr");
      thisRow.find("input,select").each(function() {
      //console.log(this.value)
      formData.append($(this).attr('name'), $(this).val());
      });

      $.ajax({
      type: "POST",
      url: '<?php echo base_url()?>exam/update',
      data: formData,
      processData: false,
      contentType: false,
      success:function(data){
      if($.trim(data)=='yes')
      {
      alert('Success! Record updated successfully');
      }
      else
      {
      alert('Error! Record not updated successfully')
      }
      }
      });

      });


      But getting some undefined params as follows:



      enter image description here



      But I want pure parameters except undefined










      share|improve this question















      I am trying to perform an ajax request by setting FormData using jquery each loop after finding input and select element for update as follows:



      $(document).on("click", ".update", function (e) {
      e.preventDefault();
      e.stopPropagation();

      let thisBtn = $(this);
      //Form Data
      let formData = new FormData();
      let thisRow = thisBtn.closest("tr");
      thisRow.find("input,select").each(function() {
      //console.log(this.value)
      formData.append($(this).attr('name'), $(this).val());
      });

      $.ajax({
      type: "POST",
      url: '<?php echo base_url()?>exam/update',
      data: formData,
      processData: false,
      contentType: false,
      success:function(data){
      if($.trim(data)=='yes')
      {
      alert('Success! Record updated successfully');
      }
      else
      {
      alert('Error! Record not updated successfully')
      }
      }
      });

      });


      But getting some undefined params as follows:



      enter image description here



      But I want pure parameters except undefined







      javascript jquery ajax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 8:08

























      asked Nov 12 at 9:00









      Bablu Ahmed

      972720




      972720
























          2 Answers
          2






          active

          oldest

          votes


















          4














          Without seeing your HTML we can't tell you exactly why, but from the output it's clear that you have some input and/or select elements in your form which have no name or value. They're probably hidden, so check the DOM inspector to find and remove them.



          If you don't want to amend the HTML, then you can use an attribute selector to only find the input and select elements which have a name, like this:



          let $thisBtn = $(this);
          let formData = new FormData();
          let $thisRow = $thisBtn.closest("tr");
          $thisRow.find("input[name], select[name]").each(function() {
          formData.append(this.name, $(this).val());
          });





          share|improve this answer































            2














            It seems that this piece of code is finding inputs/select which name attribute is undefined, what would explain the fact you are getting undefined values in the formData.



             thisRow.find("input,select").each(function() {
            //alert(this.value)
            formData.append($(this).attr('name'), $(this).val());
            });


            I would recommend you to check whether the name is undefined before adding to the formData.



            thisRow.find("input,select").each(function() {
            //alert(this.value)
            if($(this).attr('name'))
            {
            formData.append($(this).attr('name'), $(this).val());
            }
            });





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258761%2fjquery-find-all-input-type-with-select-for-ajax-request%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Without seeing your HTML we can't tell you exactly why, but from the output it's clear that you have some input and/or select elements in your form which have no name or value. They're probably hidden, so check the DOM inspector to find and remove them.



              If you don't want to amend the HTML, then you can use an attribute selector to only find the input and select elements which have a name, like this:



              let $thisBtn = $(this);
              let formData = new FormData();
              let $thisRow = $thisBtn.closest("tr");
              $thisRow.find("input[name], select[name]").each(function() {
              formData.append(this.name, $(this).val());
              });





              share|improve this answer




























                4














                Without seeing your HTML we can't tell you exactly why, but from the output it's clear that you have some input and/or select elements in your form which have no name or value. They're probably hidden, so check the DOM inspector to find and remove them.



                If you don't want to amend the HTML, then you can use an attribute selector to only find the input and select elements which have a name, like this:



                let $thisBtn = $(this);
                let formData = new FormData();
                let $thisRow = $thisBtn.closest("tr");
                $thisRow.find("input[name], select[name]").each(function() {
                formData.append(this.name, $(this).val());
                });





                share|improve this answer


























                  4












                  4








                  4






                  Without seeing your HTML we can't tell you exactly why, but from the output it's clear that you have some input and/or select elements in your form which have no name or value. They're probably hidden, so check the DOM inspector to find and remove them.



                  If you don't want to amend the HTML, then you can use an attribute selector to only find the input and select elements which have a name, like this:



                  let $thisBtn = $(this);
                  let formData = new FormData();
                  let $thisRow = $thisBtn.closest("tr");
                  $thisRow.find("input[name], select[name]").each(function() {
                  formData.append(this.name, $(this).val());
                  });





                  share|improve this answer














                  Without seeing your HTML we can't tell you exactly why, but from the output it's clear that you have some input and/or select elements in your form which have no name or value. They're probably hidden, so check the DOM inspector to find and remove them.



                  If you don't want to amend the HTML, then you can use an attribute selector to only find the input and select elements which have a name, like this:



                  let $thisBtn = $(this);
                  let formData = new FormData();
                  let $thisRow = $thisBtn.closest("tr");
                  $thisRow.find("input[name], select[name]").each(function() {
                  formData.append(this.name, $(this).val());
                  });






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 11:28

























                  answered Nov 12 at 9:09









                  Rory McCrossan

                  241k29206245




                  241k29206245

























                      2














                      It seems that this piece of code is finding inputs/select which name attribute is undefined, what would explain the fact you are getting undefined values in the formData.



                       thisRow.find("input,select").each(function() {
                      //alert(this.value)
                      formData.append($(this).attr('name'), $(this).val());
                      });


                      I would recommend you to check whether the name is undefined before adding to the formData.



                      thisRow.find("input,select").each(function() {
                      //alert(this.value)
                      if($(this).attr('name'))
                      {
                      formData.append($(this).attr('name'), $(this).val());
                      }
                      });





                      share|improve this answer


























                        2














                        It seems that this piece of code is finding inputs/select which name attribute is undefined, what would explain the fact you are getting undefined values in the formData.



                         thisRow.find("input,select").each(function() {
                        //alert(this.value)
                        formData.append($(this).attr('name'), $(this).val());
                        });


                        I would recommend you to check whether the name is undefined before adding to the formData.



                        thisRow.find("input,select").each(function() {
                        //alert(this.value)
                        if($(this).attr('name'))
                        {
                        formData.append($(this).attr('name'), $(this).val());
                        }
                        });





                        share|improve this answer
























                          2












                          2








                          2






                          It seems that this piece of code is finding inputs/select which name attribute is undefined, what would explain the fact you are getting undefined values in the formData.



                           thisRow.find("input,select").each(function() {
                          //alert(this.value)
                          formData.append($(this).attr('name'), $(this).val());
                          });


                          I would recommend you to check whether the name is undefined before adding to the formData.



                          thisRow.find("input,select").each(function() {
                          //alert(this.value)
                          if($(this).attr('name'))
                          {
                          formData.append($(this).attr('name'), $(this).val());
                          }
                          });





                          share|improve this answer












                          It seems that this piece of code is finding inputs/select which name attribute is undefined, what would explain the fact you are getting undefined values in the formData.



                           thisRow.find("input,select").each(function() {
                          //alert(this.value)
                          formData.append($(this).attr('name'), $(this).val());
                          });


                          I would recommend you to check whether the name is undefined before adding to the formData.



                          thisRow.find("input,select").each(function() {
                          //alert(this.value)
                          if($(this).attr('name'))
                          {
                          formData.append($(this).attr('name'), $(this).val());
                          }
                          });






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 9:05









                          Gabitu

                          1414




                          1414






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258761%2fjquery-find-all-input-type-with-select-for-ajax-request%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Lugert, Oklahoma