admin管理员组文章数量:1130349
I am creating a Wordpress plugin and using it to learn jQuery and AJAX. I have read many posts here on WPD and SE and on other sites but cannot get to grips with what I need to do (I am a real novice at this).
I currently have this as my JS
jQuery(".my_button").click(function(){
var sn = jQuery(this).val();
alert(sn);
jQuery.get(ajaxurl,{'action': 'getname'},
function (msg) { jQuery(".result_area").html(msg);});
});
This works fine and it alerts with the value from the button click, it then calls the PHP fuction fine and displays the result correctly in the div.
What I need to do is send the value of the variable sn to the php function and use that result in a custom database query. I think that I need to add something to this line of code jQuery.get(ajaxurl,{'action': 'getname'},
But after a couple of days trying various different bits of code I cannot get the variable to appear in the PHP function.
Should I use $atts in the PHP function to retrieve this or do it in some other way.
I am creating a Wordpress plugin and using it to learn jQuery and AJAX. I have read many posts here on WPD and SE and on other sites but cannot get to grips with what I need to do (I am a real novice at this).
I currently have this as my JS
jQuery(".my_button").click(function(){
var sn = jQuery(this).val();
alert(sn);
jQuery.get(ajaxurl,{'action': 'getname'},
function (msg) { jQuery(".result_area").html(msg);});
});
This works fine and it alerts with the value from the button click, it then calls the PHP fuction fine and displays the result correctly in the div.
What I need to do is send the value of the variable sn to the php function and use that result in a custom database query. I think that I need to add something to this line of code jQuery.get(ajaxurl,{'action': 'getname'},
But after a couple of days trying various different bits of code I cannot get the variable to appear in the PHP function.
Should I use $atts in the PHP function to retrieve this or do it in some other way.
Share Improve this question asked Dec 29, 2018 at 15:28 ColinColin 1356 bronze badges1 Answer
Reset to default 1You can pass the var the same way the action is passed- add a comma and add it on:
jQuery.get( ajaxurl,{ 'action': 'getname', 'my_var': sn },
Then in your ajax php function, $_GET['my_var'] will contain that value.
I am creating a Wordpress plugin and using it to learn jQuery and AJAX. I have read many posts here on WPD and SE and on other sites but cannot get to grips with what I need to do (I am a real novice at this).
I currently have this as my JS
jQuery(".my_button").click(function(){
var sn = jQuery(this).val();
alert(sn);
jQuery.get(ajaxurl,{'action': 'getname'},
function (msg) { jQuery(".result_area").html(msg);});
});
This works fine and it alerts with the value from the button click, it then calls the PHP fuction fine and displays the result correctly in the div.
What I need to do is send the value of the variable sn to the php function and use that result in a custom database query. I think that I need to add something to this line of code jQuery.get(ajaxurl,{'action': 'getname'},
But after a couple of days trying various different bits of code I cannot get the variable to appear in the PHP function.
Should I use $atts in the PHP function to retrieve this or do it in some other way.
I am creating a Wordpress plugin and using it to learn jQuery and AJAX. I have read many posts here on WPD and SE and on other sites but cannot get to grips with what I need to do (I am a real novice at this).
I currently have this as my JS
jQuery(".my_button").click(function(){
var sn = jQuery(this).val();
alert(sn);
jQuery.get(ajaxurl,{'action': 'getname'},
function (msg) { jQuery(".result_area").html(msg);});
});
This works fine and it alerts with the value from the button click, it then calls the PHP fuction fine and displays the result correctly in the div.
What I need to do is send the value of the variable sn to the php function and use that result in a custom database query. I think that I need to add something to this line of code jQuery.get(ajaxurl,{'action': 'getname'},
But after a couple of days trying various different bits of code I cannot get the variable to appear in the PHP function.
Should I use $atts in the PHP function to retrieve this or do it in some other way.
Share Improve this question asked Dec 29, 2018 at 15:28 ColinColin 1356 bronze badges1 Answer
Reset to default 1You can pass the var the same way the action is passed- add a comma and add it on:
jQuery.get( ajaxurl,{ 'action': 'getname', 'my_var': sn },
Then in your ajax php function, $_GET['my_var'] will contain that value.
本文标签: Pass AJAX variable to PHP functions Wordpress plugin
版权声明:本文标题:Pass AJAX variable to PHP functions Wordpress plugin 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749058972a2309802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论