我想在脚本中运行SQL查询,该如何操作

时间:2020-01-29 作者:Hakimuddin Saifee

这是我尝试的,但它给控制台错误。我不知道如何运行这个,我已经将jquery变量传递给sql查询,但它不起作用

<小时>
 <script type="text/javascript">
            var scriptTag = document.scripts[document.scripts.length - 1];
            var parentTag = scriptTag.parentNode;
            var main = parentTag.parentNode;
            var id = jQuery(main).attr(\'id\')
            var i;
            for (i = id; i<=id; i++) {
                <?php
                $results = $wpdb->get_var("select COUNT(*) from wpq7_pmpro_memberships_users where user_id = $current_user->ID AND membership_id = ?>id <?php");

        if( $results > 0){
            ?>
           if(id == i){
           btn = \'<a href="https://thepearledu.com/membership-account/membership-checkout/?level=\'+id+\'" class="elementor-button-link orange-btn elementor-button elementor-size-md" role="button"><span class="elementor-button-content-wrapper"><span class="elementor-button-text">GET IT NOW <font color="#00ff" class="animated infinite flash delay-2s">Only ₹  99.00  </font></span></span></a>\'
             jQuery(\'#\'+id).append(btn)
           }
            }
           <?php } ?>
                </script>

            <?php   
##

我尝试了Ajax,但不幸的是,这也不起作用

 <script type="text/javascript">
            var scriptTag = document.scripts[document.scripts.length - 1];
            var parentTag = scriptTag.parentNode;
            var main = parentTag.parentNode;
            var id = jQuery(main).attr(\'id\')

            var i;
            for (i = id; i<=id; i++) {
            jQuery.ajax({
  type: "POST",
  url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
  dataType: "html",
  data: {
            action: \'my_action_name\',
            "myData":id
        }}).done(function(data) {
     console.log(data);

           if(data == true){
           btn = \'<a href="https://thepearledu.com/membership-account/membership-checkout/?level=\'+id+\'" class="elementor-button-link orange-btn elementor-button elementor-size-md" role="button"><span class="elementor-button-content-wrapper"><span class="elementor-button-text">GET IT NOW <font color="#00ff" class="animated infinite flash delay-2s">Only ₹  99.00  </font></span></span></a>\'
             jQuery(\'#\'+id).append(btn)
           }
           else{
               btn = \'<a href="https://thepearledu.com/membership-account/membership-checkout/?level=\'+id+\'" class="elementor-button-link orange-btn elementor-button elementor-size-md" role="button"><span class="elementor-button-content-wrapper"><span class="elementor-button-text">GET IT NOW <font color="#00ff" class="animated infinite flash delay-2s">Only ₹  99.00  </font></span></span></a>\'
             jQuery(\'#\'+id).append(btn)
           }
        });
            }

                </script>

##

在相同的文件函数中。php

function my_action_name(){
     $data = $_POST[\'myData\'];
   $results = $wpdb->get_var("select COUNT(*) from wpq7_pmpro_memberships_users where user_id = $current_user->ID AND membership_id = $data");

        if( $results > 0){
            return true;
        }
}

1 个回复
SO网友:Sanaullah Ahmad

您可以在每个函数中使用ES6 Promise inside。这样循环停止,等待ajax调用的响应,然后使用响应数据再次继续循环。Promises是在JavaScript中实现异步编程的干净方法(ES6新特性)。在承诺之前,回调用于实现异步编程。让我们首先了解什么是异步编程及其实现,使用回调。