简体   繁体   English

如何在函数内部触发 $.post?

[英]How to fire $.post when inside function?

I want to fire Jquery to execute php code inside different file.我想触发 Jquery 在不同的文件中执行 php 代码。 However I cannot find a way to get the Jquery command to execute.但是我找不到让 Jquery 命令执行的方法。

I am using wordpress.我正在使用wordpress。

My code inside functions.php我在functions.php中的代码

 add_action( 'wp_footer', 'mycustom_wp_footer' ); //adding funtion for each form buttom to fire when pressed function mycustom_wp_footer() { //function form each form button to fire when button pressed ?> <script type="text/javascript"> var wpcf7Elm = document.querySelectorAll( '.wpcf7' ); wpcf7Elm.forEach(function(formr){ formr.addEventListener( 'wpcf7submit', function( event ) { //if ('wpcf7-f101-p97-o1' == event.detail.unitTag) { //when button inside form with this unitTag is pressed do code bellow var gen3_name = "25"; //tis code is not working $(document).ready(function(){ //adding ajax, Jquery to send data to .php file for php function to execute $.post("gen3.php", name: gen3_name); }); //tis code is not working var idform = event.detail.unitTag; alert (idform); }, false ); }) </script>

My code inside gen3.php我在 gen3.php 中的代码

 global $wpdb; if (isset($_POST['name'])) { $name = $_POST['name']; if ($name == "25") { $wpdb->update('Gen3', array('tvalue' => 1,), array('id' => 25), array('%d'), array('%d')); } }

Can you tell me what output do you get in your console?你能告诉我你在控制台中得到了什么输出吗? Any error or warning?任何错误或警告? I think your code will be placed before attaching the JQuery file.我认为您的代码将在附加 JQuery 文件之前放置。 Put the priority of the hook to a high number which means low priority and it will be placed after attaching the jquery.将钩子的优先级设置为高数,这意味着低优先级,它会在附加 jquery 后放置。

add_action( 'wp_footer', 'mycustom_wp_footer', 0, 999999 );

Also because of loading JQuery NoConflict or some other plugins, You might need to use JQuery.method() instead of $.method.同样因为加载 JQuery NoConflict 或其他一些插件,您可能需要使用 JQuery.method() 而不是 $.method。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM