Updating values in a databasemixed AJAX/Javascript form validation checkCreating input when checking/clicking radio buttonjQuery code repetition and MVCWebsite for updating a divJavaScript parameters - best practices for settings object/callbackUpdate database, refresh the page to show changes, show a success messageUpload multiple files in different formats and sizes from one HTML formPHP passing messages back to AjaxPHP function which filters content from Wordpress database (wp_posts table)jQuery handler to update map UI based on AJAX response

meaning of に in 本当に?

Why doesn't H₄O²⁺ exist?

Perform and show arithmetic with LuaLaTeX

What typically incentivizes a professor to change jobs to a lower ranking university?

How to determine what difficulty is right for the game?

Why is consensus so controversial in Britain?

How can bays and straits be determined in a procedurally generated map?

What's the output of a record needle playing an out-of-speed record

What does the "remote control" for a QF-4 look like?

Cross compiling for RPi - error while loading shared libraries

Rock identification in KY

NMaximize is not converging to a solution

tikz convert color string to hex value

Character reincarnated...as a snail

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

Convert two switches to a dual stack, and add outlet - possible here?

Could an aircraft fly or hover using only jets of compressed air?

Modeling an IP Address

Why are electrically insulating heatsinks so rare? Is it just cost?

What is a clear way to write a bar that has an extra beat?

How do I deal with an unproductive colleague in a small company?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

What's that red-plus icon near a text?

dbcc cleantable batch size explanation



Updating values in a database


mixed AJAX/Javascript form validation checkCreating input when checking/clicking radio buttonjQuery code repetition and MVCWebsite for updating a divJavaScript parameters - best practices for settings object/callbackUpdate database, refresh the page to show changes, show a success messageUpload multiple files in different formats and sizes from one HTML formPHP passing messages back to AjaxPHP function which filters content from Wordpress database (wp_posts table)jQuery handler to update map UI based on AJAX response






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

                    कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

                    Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

                    चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि