Updating values in a database
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom: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>
javascript php jquery event-handling ajax
New contributor
$endgroup$
add a comment |
$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>
javascript php jquery event-handling ajax
New contributor
$endgroup$
add a comment |
$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>
javascript php jquery event-handling ajax
New contributor
$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
javascript php jquery event-handling ajax
New contributor
New contributor
edited 14 mins ago
Jamal♦
30.6k11121227
30.6k11121227
New contributor
asked Apr 2 at 21:31
prosportalprosportal
41
41
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$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)));
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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)));
$endgroup$
add a comment |
$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)));
$endgroup$
add a comment |
$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)));
$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)));
edited 3 hours ago
answered Apr 2 at 22:57
Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ
10.2k62168
10.2k62168
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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