Wordpress - is it a good idea to insert data into the database with ajax?
I'm making a wordpress plugin, and I need to save some data to the database. The data I need to save is the result of an ajax post request. Which would be the better option, to try to insert it to the database directly with ajax (Which I don't really know how to do and if it is a good option), or to send the data to a php file with another ajax post request, then save it in a php variable and put it into the database? Also is it possible to have both the ajax requests and a php tag on the same file, so when I'm making the new post request I should send it to the file in which it is? Esentially the two post requests would look something like this, and please, feel free to correct me if I'm not doing something properly, I haven't used ajax that much.
function getcode(){
var token = localStorage.getItem('token');
console.log(token);
$.ajax({
type: 'POST',
url: '*LINK THAT I DON'T WANT TO SHARE ONLINE*',
data: { token: token },
success: function(result) {
console.log(result);
localStorage.setItem('script', result);
}
});
var script = localStorage.getItem('script')
$.ajax({
type: 'POST',
url: 'index.php',
data: { script: script },
success: function(result) {
console.log(script);
}
});
}
php ajax wordpress post
add a comment |
I'm making a wordpress plugin, and I need to save some data to the database. The data I need to save is the result of an ajax post request. Which would be the better option, to try to insert it to the database directly with ajax (Which I don't really know how to do and if it is a good option), or to send the data to a php file with another ajax post request, then save it in a php variable and put it into the database? Also is it possible to have both the ajax requests and a php tag on the same file, so when I'm making the new post request I should send it to the file in which it is? Esentially the two post requests would look something like this, and please, feel free to correct me if I'm not doing something properly, I haven't used ajax that much.
function getcode(){
var token = localStorage.getItem('token');
console.log(token);
$.ajax({
type: 'POST',
url: '*LINK THAT I DON'T WANT TO SHARE ONLINE*',
data: { token: token },
success: function(result) {
console.log(result);
localStorage.setItem('script', result);
}
});
var script = localStorage.getItem('script')
$.ajax({
type: 'POST',
url: 'index.php',
data: { script: script },
success: function(result) {
console.log(script);
}
});
}
php ajax wordpress post
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38
add a comment |
I'm making a wordpress plugin, and I need to save some data to the database. The data I need to save is the result of an ajax post request. Which would be the better option, to try to insert it to the database directly with ajax (Which I don't really know how to do and if it is a good option), or to send the data to a php file with another ajax post request, then save it in a php variable and put it into the database? Also is it possible to have both the ajax requests and a php tag on the same file, so when I'm making the new post request I should send it to the file in which it is? Esentially the two post requests would look something like this, and please, feel free to correct me if I'm not doing something properly, I haven't used ajax that much.
function getcode(){
var token = localStorage.getItem('token');
console.log(token);
$.ajax({
type: 'POST',
url: '*LINK THAT I DON'T WANT TO SHARE ONLINE*',
data: { token: token },
success: function(result) {
console.log(result);
localStorage.setItem('script', result);
}
});
var script = localStorage.getItem('script')
$.ajax({
type: 'POST',
url: 'index.php',
data: { script: script },
success: function(result) {
console.log(script);
}
});
}
php ajax wordpress post
I'm making a wordpress plugin, and I need to save some data to the database. The data I need to save is the result of an ajax post request. Which would be the better option, to try to insert it to the database directly with ajax (Which I don't really know how to do and if it is a good option), or to send the data to a php file with another ajax post request, then save it in a php variable and put it into the database? Also is it possible to have both the ajax requests and a php tag on the same file, so when I'm making the new post request I should send it to the file in which it is? Esentially the two post requests would look something like this, and please, feel free to correct me if I'm not doing something properly, I haven't used ajax that much.
function getcode(){
var token = localStorage.getItem('token');
console.log(token);
$.ajax({
type: 'POST',
url: '*LINK THAT I DON'T WANT TO SHARE ONLINE*',
data: { token: token },
success: function(result) {
console.log(result);
localStorage.setItem('script', result);
}
});
var script = localStorage.getItem('script')
$.ajax({
type: 'POST',
url: 'index.php',
data: { script: script },
success: function(result) {
console.log(script);
}
});
}
php ajax wordpress post
php ajax wordpress post
asked Nov 22 at 18:26
n_at
326
326
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38
add a comment |
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38
add a comment |
active
oldest
votes
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53436453%2fwordpress-is-it-a-good-idea-to-insert-data-into-the-database-with-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2fstackoverflow.com%2fquestions%2f53436453%2fwordpress-is-it-a-good-idea-to-insert-data-into-the-database-with-ajax%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
Your code is bit confusing. Can you explain what are you trying to do?
– zipkundan
Nov 22 at 18:44
@zipkundan I'm verifying a token and after that getting a script which I want to save to a database.
– n_at
Nov 23 at 0:50
WordPress has a framework for handling AJAX requests. It is probably easier and safer (it can provide some protection against CSRF) to use that framework then to roll your own.
– magenta
Nov 23 at 1:07
use the wordpress ajax system and work with nonce
– Tobias
Nov 23 at 8:50
I suggest you start here: codex.wordpress.org/AJAX_in_Plugins
– muka.gergely
Nov 23 at 16:38