php / Wordpress / Ajax Button调用php脚本

时间:2022-06-05 01:23:28

I'm just beginning to learn PHP and I am developing a WordPress plugin. But I have a problem. When I press a button, I want it to call a PHP script where I have a different SQL request to execute and others things without changing page. I know I need to use Ajax but I don't understand how use it and where I write it. Could you help me please?

我刚刚开始学习PHP,我正在开发一个WordPress插件。但我有一个问题。当我按下一个按钮时,我希望它调用一个PHP脚本,在那里我可以执行不同的SQL请求,而不需要更改页面。我知道我需要使用Ajax,但我不明白如何使用它以及我在哪里使用它。请问你能帮帮我吗?

index.php is where I have a function that prints a list with checkbox and the button to execute the script createInsert.php

index.php是我有一个函数,它打印带复选框的列表和执行脚本createInsert.php的按钮

PS: Sorry for my bad English.

PS:抱歉我的英语不好。

index.php

的index.php

 <?php
   public function pref_liste_checkbox(){
        global $wpdb; 

        print '<h3>Composition de la liste synthetique</h3>';
        print '<form method="POST" action=" "><table border="1"><tr><th>Attributs</th><th>Choix</th></tr>';

        $resultat= $wpdb->get_results("SELECT * from wp_frm_fields ",ARRAY_N); 

        foreach ($resultat as $post) {      
           print'<tr>';
           echo '<td>'.$post[2].'</td>';
           echo"<td><center><input type='checkbox' name='choixP[]'value='".$post[0]."'></center></td>";
        }

        print '</tr><input type="submit" name ="execute"value="execute"></table></form>';
    }
?>

createInsert.php

createInsert.php

<?php
     //if the table exist =1 
    if(FrmStatisticsController::table_ok('testWP','wp_users')==1){

    ?>
            <script type="text/javascript">

                    var r = confirm("Voulez-vous vraiment mettre à jour les attributs de la liste synthétique?");
                    if (r == true) {

                        <?php
                                ///////////CLEAN LA TABLE//////////
                                ////////////INSERT INTO/////////

                        ?>
                        var f = alert("MàJ réussie !");

                    }else {
                                ///////////Aucune Action\No Action////////
                    }

            </script>
    <?php

    } else (FrmStatisticsController::table_ok('testWP','wp_users')==0){

    ?>
            <script type="text/javascript">

                    var r = confirm("Voulez-vous sauvegarder les attributs de la liste synthétique?");
                    if (r == true) {

                        <?php
                                ///////////CREATE TABLE//////////

                                global $wpdb;
                                $nom_table = $wpdb->prefix .'choix_attributs_liste';
                                $sql = "CREATE TABLE $nom_table ( mytable_id bigint(20) unsigned NOT NULL auto_increment, att_name varchar(255), att_id bigint(20),PRIMARY KEY(mytable_id));";
                                dbDelta($sql);

                                ////////////INSERT INTO/////////
                        ?>

                        var f = alert("Création et insertion des n-uplets réussie!");

                    }else {
                                ///////////Aucune Action\No Action////////
                    }

            </script>
    <?php
    }

1 个解决方案

#1


1  

I know your Question is for the button but there is these point you should look at before .

我知道你的问题是针对按钮的,但有一点你应该先看一下。

  1. to create plugin there is standards ... see this tool will help you get the files ... and set the basic plugin ready http://wppb.me/

    创建插件有标准...看到这个工具将帮助您获取文件...并设置基本插件准备http://wppb.me/

  2. check some tutorials

    检查一些教程

http://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ https://codex.wordpress.org/Writing_a_Plugin

http://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ https://codex.wordpress.org/Writing_a_Plugin

  1. search for other plugins before create one ( so you don't reinvent the wheel ) https://wordpress.org/plugins/
  2. 在创建之前搜索其他插件(所以你不要重新发明*)https://wordpress.org/plugins/

#1


1  

I know your Question is for the button but there is these point you should look at before .

我知道你的问题是针对按钮的,但有一点你应该先看一下。

  1. to create plugin there is standards ... see this tool will help you get the files ... and set the basic plugin ready http://wppb.me/

    创建插件有标准...看到这个工具将帮助您获取文件...并设置基本插件准备http://wppb.me/

  2. check some tutorials

    检查一些教程

http://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ https://codex.wordpress.org/Writing_a_Plugin

http://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ https://codex.wordpress.org/Writing_a_Plugin

  1. search for other plugins before create one ( so you don't reinvent the wheel ) https://wordpress.org/plugins/
  2. 在创建之前搜索其他插件(所以你不要重新发明*)https://wordpress.org/plugins/