Updating values in a database





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0












$begingroup$


I'm using standard jQuery Ajax to update values in the database and reflect the result in an output. This repetition seems like bad practice and I don't know enough about jQuery to consolidate this correctly.



jQuery



        $("#vendorticket").change(function() { //Input/Select tag
var output = ("#vendorticket-result");
$.ajax({
url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
type: 'post',
data: {
vendorticket: $(this).val(),
column: 'vendorticket',
id: 15
},
success: function(result) {
$(output).html(result); //Output span either "Saved" or "Save Failed"
}
});
});

//Rinse & Repeat...

$("#priority").change(function() {
var output = ("#priority-result");
$.ajax({
url: 'includes/view.func.php',
type: 'post',
data: {
priority: $(this).val(),
column: 'priority',
id: 15
},
success: function(result) {
$(output).html(result);
}
});
});

$("#assignedto").change(function() {
var output = ("#assignedto-result");
$.ajax({
url: 'includes/view.func.php',
type: 'post',
data: {
assignedto: $(this).val(),
column: 'assignedto',
id: 15 //
},
success: function(result) {
$(output).html(result);
}
});
});

});


HTML



<input type="text" id="assignedto" /><span id="assignedto-result"></span>

<input type="text" id="priority" /><span id="priority-result"></span>

<input type="text" id="vendorticket" /><span id="vendordicket-result"></span>









share|improve this question









New contributor




prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



















    0












    $begingroup$


    I'm using standard jQuery Ajax to update values in the database and reflect the result in an output. This repetition seems like bad practice and I don't know enough about jQuery to consolidate this correctly.



    jQuery



            $("#vendorticket").change(function() { //Input/Select tag
    var output = ("#vendorticket-result");
    $.ajax({
    url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
    type: 'post',
    data: {
    vendorticket: $(this).val(),
    column: 'vendorticket',
    id: 15
    },
    success: function(result) {
    $(output).html(result); //Output span either "Saved" or "Save Failed"
    }
    });
    });

    //Rinse & Repeat...

    $("#priority").change(function() {
    var output = ("#priority-result");
    $.ajax({
    url: 'includes/view.func.php',
    type: 'post',
    data: {
    priority: $(this).val(),
    column: 'priority',
    id: 15
    },
    success: function(result) {
    $(output).html(result);
    }
    });
    });

    $("#assignedto").change(function() {
    var output = ("#assignedto-result");
    $.ajax({
    url: 'includes/view.func.php',
    type: 'post',
    data: {
    assignedto: $(this).val(),
    column: 'assignedto',
    id: 15 //
    },
    success: function(result) {
    $(output).html(result);
    }
    });
    });

    });


    HTML



    <input type="text" id="assignedto" /><span id="assignedto-result"></span>

    <input type="text" id="priority" /><span id="priority-result"></span>

    <input type="text" id="vendorticket" /><span id="vendordicket-result"></span>









    share|improve this question









    New contributor




    prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I'm using standard jQuery Ajax to update values in the database and reflect the result in an output. This repetition seems like bad practice and I don't know enough about jQuery to consolidate this correctly.



      jQuery



              $("#vendorticket").change(function() { //Input/Select tag
      var output = ("#vendorticket-result");
      $.ajax({
      url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
      type: 'post',
      data: {
      vendorticket: $(this).val(),
      column: 'vendorticket',
      id: 15
      },
      success: function(result) {
      $(output).html(result); //Output span either "Saved" or "Save Failed"
      }
      });
      });

      //Rinse & Repeat...

      $("#priority").change(function() {
      var output = ("#priority-result");
      $.ajax({
      url: 'includes/view.func.php',
      type: 'post',
      data: {
      priority: $(this).val(),
      column: 'priority',
      id: 15
      },
      success: function(result) {
      $(output).html(result);
      }
      });
      });

      $("#assignedto").change(function() {
      var output = ("#assignedto-result");
      $.ajax({
      url: 'includes/view.func.php',
      type: 'post',
      data: {
      assignedto: $(this).val(),
      column: 'assignedto',
      id: 15 //
      },
      success: function(result) {
      $(output).html(result);
      }
      });
      });

      });


      HTML



      <input type="text" id="assignedto" /><span id="assignedto-result"></span>

      <input type="text" id="priority" /><span id="priority-result"></span>

      <input type="text" id="vendorticket" /><span id="vendordicket-result"></span>









      share|improve this question









      New contributor




      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm using standard jQuery Ajax to update values in the database and reflect the result in an output. This repetition seems like bad practice and I don't know enough about jQuery to consolidate this correctly.



      jQuery



              $("#vendorticket").change(function() { //Input/Select tag
      var output = ("#vendorticket-result");
      $.ajax({
      url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
      type: 'post',
      data: {
      vendorticket: $(this).val(),
      column: 'vendorticket',
      id: 15
      },
      success: function(result) {
      $(output).html(result); //Output span either "Saved" or "Save Failed"
      }
      });
      });

      //Rinse & Repeat...

      $("#priority").change(function() {
      var output = ("#priority-result");
      $.ajax({
      url: 'includes/view.func.php',
      type: 'post',
      data: {
      priority: $(this).val(),
      column: 'priority',
      id: 15
      },
      success: function(result) {
      $(output).html(result);
      }
      });
      });

      $("#assignedto").change(function() {
      var output = ("#assignedto-result");
      $.ajax({
      url: 'includes/view.func.php',
      type: 'post',
      data: {
      assignedto: $(this).val(),
      column: 'assignedto',
      id: 15 //
      },
      success: function(result) {
      $(output).html(result);
      }
      });
      });

      });


      HTML



      <input type="text" id="assignedto" /><span id="assignedto-result"></span>

      <input type="text" id="priority" /><span id="priority-result"></span>

      <input type="text" id="vendorticket" /><span id="vendordicket-result"></span>






      javascript php jquery event-handling ajax






      share|improve this question









      New contributor




      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 14 mins ago









      Jamal

      30.6k11121227




      30.6k11121227






      New contributor




      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Apr 2 at 21:31









      prosportalprosportal

      41




      41




      New contributor




      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      prosportal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          Simplifying redundancy



          You could do something like iterating over the id of each input element, calling a function to add the handler.



          ['priority', 'assignedTo', 'vendorticket'].forEach(addChangeHandler);

          function addChangeHandler(inputId) {
          $("#" + inputId).change(function() { //Input/Select tag
          var output = ("#" + inputId + "-result");
          var data = {
          column: inputId,
          id: 15
          };
          data[inputId] = $(this).val()
          $.post({
          url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
          data: data,
          success: function(result) {
          $(output).html(result); //Output span either "Saved" or "Save Failed"
          }
          });
          });
          }


          Note the code uses the shortcut method $.post() to allow skipping the request type.



          But a simpler technique would be to add a generic change handler to any of those inputs. This can be done by combining the selectors passed to the jQuery function:



          $('#priority, #assignedTo, #vendorticket')


          This can then be used to add a generic function handler that can get the id of the input using .attr('id'):



          $('#priority, #assignedTo, #vendorticket').change(changeHandler);
          function changeHandler() {
          var inputId = $(this).attr('id');
          var output = ("#" + inputId + "-result");
          var data = {
          column: inputId,
          id: 15
          };
          data[inputId] = $(this).val()
          $.post({
          url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
          data: data,
          success: function(result) {
          $(output).html(result); //Output span either "Saved" or "Save Failed"
          }
          });
          }


          If you had a lot more input elements to add to that list, a class could be applied and they could be selected by class name (e.g. $('.inputToWatch')) or else if they were all child elements of a container then a child selector might simplify things (e.g. $('#containerElement input').



          Simplifying the success function



          The success function could be replaced with a bound function - i.e. a function bound to $(output).html:



          success: $().html.bind($(output))


          These changes are visible in this phpfiddle.



          You could also use the other syntax of $.ajax() i.e. Query.post( url [, data ] [, success ] [, dataType ] )



          $.post('includes/view.func.php', data, $().html.bind($(output))); 





          share|improve this answer











          $endgroup$














            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            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: "196"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            });


            }
            });






            prosportal is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216751%2fupdating-values-in-a-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1












            $begingroup$

            Simplifying redundancy



            You could do something like iterating over the id of each input element, calling a function to add the handler.



            ['priority', 'assignedTo', 'vendorticket'].forEach(addChangeHandler);

            function addChangeHandler(inputId) {
            $("#" + inputId).change(function() { //Input/Select tag
            var output = ("#" + inputId + "-result");
            var data = {
            column: inputId,
            id: 15
            };
            data[inputId] = $(this).val()
            $.post({
            url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
            data: data,
            success: function(result) {
            $(output).html(result); //Output span either "Saved" or "Save Failed"
            }
            });
            });
            }


            Note the code uses the shortcut method $.post() to allow skipping the request type.



            But a simpler technique would be to add a generic change handler to any of those inputs. This can be done by combining the selectors passed to the jQuery function:



            $('#priority, #assignedTo, #vendorticket')


            This can then be used to add a generic function handler that can get the id of the input using .attr('id'):



            $('#priority, #assignedTo, #vendorticket').change(changeHandler);
            function changeHandler() {
            var inputId = $(this).attr('id');
            var output = ("#" + inputId + "-result");
            var data = {
            column: inputId,
            id: 15
            };
            data[inputId] = $(this).val()
            $.post({
            url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
            data: data,
            success: function(result) {
            $(output).html(result); //Output span either "Saved" or "Save Failed"
            }
            });
            }


            If you had a lot more input elements to add to that list, a class could be applied and they could be selected by class name (e.g. $('.inputToWatch')) or else if they were all child elements of a container then a child selector might simplify things (e.g. $('#containerElement input').



            Simplifying the success function



            The success function could be replaced with a bound function - i.e. a function bound to $(output).html:



            success: $().html.bind($(output))


            These changes are visible in this phpfiddle.



            You could also use the other syntax of $.ajax() i.e. Query.post( url [, data ] [, success ] [, dataType ] )



            $.post('includes/view.func.php', data, $().html.bind($(output))); 





            share|improve this answer











            $endgroup$


















              1












              $begingroup$

              Simplifying redundancy



              You could do something like iterating over the id of each input element, calling a function to add the handler.



              ['priority', 'assignedTo', 'vendorticket'].forEach(addChangeHandler);

              function addChangeHandler(inputId) {
              $("#" + inputId).change(function() { //Input/Select tag
              var output = ("#" + inputId + "-result");
              var data = {
              column: inputId,
              id: 15
              };
              data[inputId] = $(this).val()
              $.post({
              url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
              data: data,
              success: function(result) {
              $(output).html(result); //Output span either "Saved" or "Save Failed"
              }
              });
              });
              }


              Note the code uses the shortcut method $.post() to allow skipping the request type.



              But a simpler technique would be to add a generic change handler to any of those inputs. This can be done by combining the selectors passed to the jQuery function:



              $('#priority, #assignedTo, #vendorticket')


              This can then be used to add a generic function handler that can get the id of the input using .attr('id'):



              $('#priority, #assignedTo, #vendorticket').change(changeHandler);
              function changeHandler() {
              var inputId = $(this).attr('id');
              var output = ("#" + inputId + "-result");
              var data = {
              column: inputId,
              id: 15
              };
              data[inputId] = $(this).val()
              $.post({
              url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
              data: data,
              success: function(result) {
              $(output).html(result); //Output span either "Saved" or "Save Failed"
              }
              });
              }


              If you had a lot more input elements to add to that list, a class could be applied and they could be selected by class name (e.g. $('.inputToWatch')) or else if they were all child elements of a container then a child selector might simplify things (e.g. $('#containerElement input').



              Simplifying the success function



              The success function could be replaced with a bound function - i.e. a function bound to $(output).html:



              success: $().html.bind($(output))


              These changes are visible in this phpfiddle.



              You could also use the other syntax of $.ajax() i.e. Query.post( url [, data ] [, success ] [, dataType ] )



              $.post('includes/view.func.php', data, $().html.bind($(output))); 





              share|improve this answer











              $endgroup$
















                1












                1








                1





                $begingroup$

                Simplifying redundancy



                You could do something like iterating over the id of each input element, calling a function to add the handler.



                ['priority', 'assignedTo', 'vendorticket'].forEach(addChangeHandler);

                function addChangeHandler(inputId) {
                $("#" + inputId).change(function() { //Input/Select tag
                var output = ("#" + inputId + "-result");
                var data = {
                column: inputId,
                id: 15
                };
                data[inputId] = $(this).val()
                $.post({
                url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
                data: data,
                success: function(result) {
                $(output).html(result); //Output span either "Saved" or "Save Failed"
                }
                });
                });
                }


                Note the code uses the shortcut method $.post() to allow skipping the request type.



                But a simpler technique would be to add a generic change handler to any of those inputs. This can be done by combining the selectors passed to the jQuery function:



                $('#priority, #assignedTo, #vendorticket')


                This can then be used to add a generic function handler that can get the id of the input using .attr('id'):



                $('#priority, #assignedTo, #vendorticket').change(changeHandler);
                function changeHandler() {
                var inputId = $(this).attr('id');
                var output = ("#" + inputId + "-result");
                var data = {
                column: inputId,
                id: 15
                };
                data[inputId] = $(this).val()
                $.post({
                url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
                data: data,
                success: function(result) {
                $(output).html(result); //Output span either "Saved" or "Save Failed"
                }
                });
                }


                If you had a lot more input elements to add to that list, a class could be applied and they could be selected by class name (e.g. $('.inputToWatch')) or else if they were all child elements of a container then a child selector might simplify things (e.g. $('#containerElement input').



                Simplifying the success function



                The success function could be replaced with a bound function - i.e. a function bound to $(output).html:



                success: $().html.bind($(output))


                These changes are visible in this phpfiddle.



                You could also use the other syntax of $.ajax() i.e. Query.post( url [, data ] [, success ] [, dataType ] )



                $.post('includes/view.func.php', data, $().html.bind($(output))); 





                share|improve this answer











                $endgroup$



                Simplifying redundancy



                You could do something like iterating over the id of each input element, calling a function to add the handler.



                ['priority', 'assignedTo', 'vendorticket'].forEach(addChangeHandler);

                function addChangeHandler(inputId) {
                $("#" + inputId).change(function() { //Input/Select tag
                var output = ("#" + inputId + "-result");
                var data = {
                column: inputId,
                id: 15
                };
                data[inputId] = $(this).val()
                $.post({
                url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
                data: data,
                success: function(result) {
                $(output).html(result); //Output span either "Saved" or "Save Failed"
                }
                });
                });
                }


                Note the code uses the shortcut method $.post() to allow skipping the request type.



                But a simpler technique would be to add a generic change handler to any of those inputs. This can be done by combining the selectors passed to the jQuery function:



                $('#priority, #assignedTo, #vendorticket')


                This can then be used to add a generic function handler that can get the id of the input using .attr('id'):



                $('#priority, #assignedTo, #vendorticket').change(changeHandler);
                function changeHandler() {
                var inputId = $(this).attr('id');
                var output = ("#" + inputId + "-result");
                var data = {
                column: inputId,
                id: 15
                };
                data[inputId] = $(this).val()
                $.post({
                url: 'includes/view.func.php', // Updating database based on specified row id, column, and value
                data: data,
                success: function(result) {
                $(output).html(result); //Output span either "Saved" or "Save Failed"
                }
                });
                }


                If you had a lot more input elements to add to that list, a class could be applied and they could be selected by class name (e.g. $('.inputToWatch')) or else if they were all child elements of a container then a child selector might simplify things (e.g. $('#containerElement input').



                Simplifying the success function



                The success function could be replaced with a bound function - i.e. a function bound to $(output).html:



                success: $().html.bind($(output))


                These changes are visible in this phpfiddle.



                You could also use the other syntax of $.ajax() i.e. Query.post( url [, data ] [, success ] [, dataType ] )



                $.post('includes/view.func.php', data, $().html.bind($(output))); 






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 3 hours ago

























                answered Apr 2 at 22:57









                Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ

                10.2k62168




                10.2k62168






















                    prosportal is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    prosportal is a new contributor. Be nice, and check out our Code of Conduct.













                    prosportal is a new contributor. Be nice, and check out our Code of Conduct.












                    prosportal is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • 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.


                    Use MathJax to format equations. MathJax reference.


                    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%2fcodereview.stackexchange.com%2fquestions%2f216751%2fupdating-values-in-a-database%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

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар