admin管理员组

文章数量:1122832

Thats What I tried But it give console error. i didnt understand how can run this i Have pass jquery variable to sql query but it isnt working


 <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="/?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   

##

i Tried through Ajax but unfortunatly this is also not working

 <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="/?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="/?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>

##

in same file function.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;
        }
}

Thats What I tried But it give console error. i didnt understand how can run this i Have pass jquery variable to sql query but it isnt working


 <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   

##

i Tried through Ajax but unfortunatly this is also not working

 <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>

##

in same file function.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;
        }
}
Share Improve this question edited Jan 29, 2020 at 7:05 Hakimuddin Saifee asked Jan 29, 2020 at 6:25 Hakimuddin SaifeeHakimuddin Saifee 392 silver badges10 bronze badges 3
  • 1 You can't mix PHP and JavaScript like this. It won't work. If you need to pass JavaScript variables to PHP you need to send them to the server with an AJAX request. There's some documentation on using AJAX in WordPress here: developer.wordpress.org/plugins/javascript/ajax – Jacob Peattie Commented Jan 29, 2020 at 6:28
  • yes i tried through ajax also please check above – Hakimuddin Saifee Commented Jan 29, 2020 at 7:05
  • you have not add action your function.php – Dharmishtha Patel Commented Jan 29, 2020 at 7:31
Add a comment  | 

1 Answer 1

Reset to default 0

You can use ES6 Promise inside for-each function. So that the loop stop, wait for response from ajax call, than continue loop again with response data. Promises are a clean way to implement async programming in JavaScript (ES6 new feature). Prior to promises, Callbacks were used to implement async programming. Let's begin by understanding what async programming is and its implementation, using Callbacks.

本文标签: jqueryI want to run Sql Query in script how can i do this