Wordpress - is it a good idea to insert data into the database with ajax?












0















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);
}
});
}









share|improve this question























  • Your code is bit confusing. Can you explain what are you trying to do?

    – zipkundan
    Nov 22 '18 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 '18 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 '18 at 1:07













  • use the wordpress ajax system and work with nonce

    – Tobias
    Nov 23 '18 at 8:50











  • I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

    – muka.gergely
    Nov 23 '18 at 16:38
















0















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);
}
});
}









share|improve this question























  • Your code is bit confusing. Can you explain what are you trying to do?

    – zipkundan
    Nov 22 '18 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 '18 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 '18 at 1:07













  • use the wordpress ajax system and work with nonce

    – Tobias
    Nov 23 '18 at 8:50











  • I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

    – muka.gergely
    Nov 23 '18 at 16:38














0












0








0








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);
}
});
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 18:26









n_atn_at

326




326













  • Your code is bit confusing. Can you explain what are you trying to do?

    – zipkundan
    Nov 22 '18 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 '18 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 '18 at 1:07













  • use the wordpress ajax system and work with nonce

    – Tobias
    Nov 23 '18 at 8:50











  • I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

    – muka.gergely
    Nov 23 '18 at 16:38



















  • Your code is bit confusing. Can you explain what are you trying to do?

    – zipkundan
    Nov 22 '18 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 '18 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 '18 at 1:07













  • use the wordpress ajax system and work with nonce

    – Tobias
    Nov 23 '18 at 8:50











  • I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

    – muka.gergely
    Nov 23 '18 at 16:38

















Your code is bit confusing. Can you explain what are you trying to do?

– zipkundan
Nov 22 '18 at 18:44





Your code is bit confusing. Can you explain what are you trying to do?

– zipkundan
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 1:07















use the wordpress ajax system and work with nonce

– Tobias
Nov 23 '18 at 8:50





use the wordpress ajax system and work with nonce

– Tobias
Nov 23 '18 at 8:50













I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

– muka.gergely
Nov 23 '18 at 16:38





I suggest you start here: codex.wordpress.org/AJAX_in_Plugins

– muka.gergely
Nov 23 '18 at 16:38












0






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
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


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

But avoid



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

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


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




draft saved


draft discarded














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





















































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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy