dongqixian8474
2017-03-03 05:27
浏览 87

如何将参数传递给jQuery函数

In there i used this code to create HTML table using JSON , in my web program i generate JSON using PHP , now i need to pass the JSON as a parameter to above function, how can i do this.

<script type="text/javascript">
    var data = [
  {
    "UserID": 1,
    "UserName": "rooter",
    "Password": "12345",
    "Country": "UK",
    "Email": "sac@gmail.com"

  },
  {
    "UserID": 2,
    "UserName": "binu",
    "Password": "123",
    "Country": "uk",
    "Email": "Binu@gmail.com"
  },
  {
    "UserID": 3,
    "UserName": "cal",
    "Password": "123",
    "Country": "uk",
    "Email": "cal@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  }
];
$(document).ready(function () {
    var html = '<table class="table table-striped">';
    html += '<tr>';
    var flag = 0;
    $.each(data[0], function(index, value){
        html += '<th>'+index+'</th>';
    });
    html += '</tr>';
     $.each(data, function(index, value){
         html += '<tr>';
        $.each(value, function(index2, value2){
            html += '<td>'+value2+'</td>';
        });
        html += '<tr>';
     });
     html += '</table>';
     $('body').html(html);
});
</script>

Can i pass the JSON value as parameter to this function using PHP

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanhuren5581 2017-03-03 05:38
    已采纳

    You can pass as parameter with php tags

       <?php
    $data = '[
      {
        "UserID": 1,
        "UserName": "rooter",
        "Password": "12345",
        "Country": "UK",
        "Email": "sac@gmail.com"
    
      },
      {
        "UserID": 2,
        "UserName": "binu",
        "Password": "123",
        "Country": "uk",
        "Email": "Binu@gmail.com"
      },
      {
        "UserID": 3,
        "UserName": "cal",
        "Password": "123",
        "Country": "uk",
        "Email": "cal@gmail.com"
      },
      {
        "UserID": 4,
        "UserName": "nera",
        "Password": "1234",
        "Country": "uk",
        "Email": "nera@gmail.com"
      },
      {
        "UserID": 4,
        "UserName": "nera",
        "Password": "1234",
        "Country": "uk",
        "Email": "nera@gmail.com"
      }
    ]';
    
    
    ?>
    
    <script>
        $(document).ready(function () {
        var html = '<table class="table table-striped">';
        html += '<tr>';
        var flag = 0;
    
        var data2   =   <?php echo $data; ?>;
        $.each(data2[0], function(index, value){
            html += '<th>'+index+'</th>';
        });
        html += '</tr>';
         $.each(data2, function(index, value){
             html += '<tr>';
            $.each(value, function(index2, value2){
                html += '<td>'+value2+'</td>';
            });
            html += '<tr>';
         });
         html += '</table>';
         $('body').html(html);
         console.log(html);
    });
        </script>
    
    点赞 评论
  • dongqing4774 2017-03-03 06:05

    I would suggest just creating your table in php so it's on the page when it loads, it will be much better for seo and user experience.

    But this is how you can print javascript data when the page loads from php without having to make an ajax request.

    The key is <?= json_encode(array); ?> json encode will parse a php array/associative array into a json object.

    I did this in it's individual script tag in case there is an error with parsing or your editors syntax.

    <?php                                                                                                             
    $data = [                                                                                                         
      [ "UserID" => 1, "UserName" => "rooter", "Password" => "12345", "Country" => "UK", "Email" => "sac@gmail.com" ],
      [ "UserID" => 2, "UserName" => "binu", "Password" => "123", "Country" => "uk", "Email" => "Binu@gmail.com" ],   
    ];                                                                                                                
    ?>                                                                                                                
    <script>data = <?= json_encode($data, JSON_NUMERIC_CHECK); ?></script>                                                                
    $(document).ready(function () {                                                                                   
        var html = '<table class="table table-striped">';                                                             
        html += '<tr>';                                                                                               
        var flag = 0;                                                                                                 
        $.each(data[0], function(index, value){                                                                       
            html += '<th>'+index+'</th>';                                                                             
        });                                                                                                           
        html += '</tr>';                                                                                              
         $.each(data, function(index, value){                                                                         
             html += '<tr>';                                                                                          
            $.each(value, function(index2, value2){                                                                   
                html += '<td>'+value2+'</td>';                                                                        
            });                                                                                                       
            html += '<tr>';                                                                                           
         });                                                                                                          
         html += '</table>';                                                                                          
         $('body').html(html);                                                                                        
    });                                                                                                               
    </script>                                                                                                         
    
    点赞 评论

相关推荐 更多相似问题