简体   繁体   中英

Getting a post variable inside a function

I have a case where I want to pass a variable inside a function. The code gives more clarity:

<?php

$id=$_POST['id'];            

echo "
<script type=\"text/javascript\">
  $(document).ready(function(){
    function loadData(page){
      $.ajax({  
        type: \"POST\",
        url: \"loadSubscritor.php\",
        dataType: \"html\",
        data: ({  page:page }),
        success: function(msg) {
          $(\"#subscritor #container\").ajaxComplete(function(event, request, settings) {
            $(\"#subscritor #container\").html(msg);
          });
        },
        error: function(){
            alert('alertErr');
        }
      });
    }

    loadData(1);  // For first time page load default results
    $('#subscritor #container .pagination li.active').live('click',function() {
      var page = $(this).attr('p');
      loadData(page);
    });           

    $('#subscritor #go_bt').live('click',function() {
      var page = parseInt($('.goto').val());
      var no_of_pages = parseInt($('.total').attr('a'));
      if(page != 0 && page <= no_of_pages){
        loadData(page);
      }else{
        alert('Enter a PAGE between 1 and '+no_of_pages);
        $('.goto').val(\"\").focus();
        return false;
      }
    });
  }); 
</script>                 
<h3>Subscritor</h3>     

<div id=\"subscritor\">
  <div id=\"container\">
        <div class=\"pagination\"></div>
  </div> 
</div>"; 

?>

As shown in the code, I would like to know how could I pass $id inside the loadData(page) function . I get this variable from a post request made with ajax, and need to use it inside the function to pass it has variable to loadSubscritor.php

Any idea on how to do it?

EDIT 1: I guees I wasnt very clear on what I wanted, i want to do this : function loadData(page){ $.ajax({
data: ({ page:page id:$id }),

Thanks in advance.

Well, if I understood what you are trying to achieve you could replace this :

$id=$_POST['id'];  

By this :

$id = isset($_POST['id']) ? $_POST['id'] : 1;

And this :

loadData(1);  // For first time page load default results

By this :

loadData($id);

For explanation, if it's first time page load, $id will be set to "1".
Else, this will come from $_POST['id'].

在您的jquery代码内,您可以按以下方式调用php变量

var current_page = "<?php echo $id; ?>" ;

You can make a local variable and easily use as below:

<?php

 $id=$_POST['id'];            


  echo " 


        <script type=\"text/javascript\">
        var id = \"".$id."\";
         $(document).ready(function(){
            function loadData(page){
                $.ajax
                ({  
                     type: \"POST\",
                    url: \"loadSubscritor.php\",
                                         dataType: \"html\",

                   data: ({  page:page
                             }),
                    success: function(msg)
                    {
                        $(\"#subscritor #container\").ajaxComplete(function(event, request, settings)
                        {
                            $(\"#subscritor #container\").html(msg);
                        });
                    },
                        error:
         function() 
         {               alert('alertErr');




        }



                });
            }
            loadData(1);  // For first time page load default results
            $('#subscritor #container .pagination li.active').live('click',function(){
                var page = $(this).attr('p');
                loadData(id);

            });           
            $('#subscritor #go_bt').live('click',function(){
                var page = parseInt($('.goto').val());
                var no_of_pages = parseInt($('.total').attr('a'));
                if(page != 0 && page <= no_of_pages){
                    loadData(id);
                }else{
                    alert('Enter a PAGE between 1 and '+no_of_pages);
                    $('.goto').val(\"\").focus();
                    return false;
                }

            });
        });    
   </script>                 
<h3>Subscritor</h3>     

<div id=\"subscritor\">
<div id=\"container\">
        <div class=\"pagination\"></div>
  </div> 
  </div>



                ";                                                                                                                                                                          
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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