最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery inside PHP if statement - Stack Overflow

programmeradmin0浏览0评论

Need help to put some jquery function inside if statement . I want to hide my div when data from database is empty . I've done like this , and nothing happened.

                    <?php if(empty($all_data)){ ?>

                            <script>
                                $(document).ready(function () {
                                    $( '.table-wrapper').css("display","none");
                                });
                            </script>                       <===update

                    <?php }else{ ?>
                        <?php foreach($all_data->result() as $data){ ?>
                            <tr> 
                                <td><?php echo $data->id_history;?></td>
                                <td><?=$data->id_admin;?></td>
                                <td><?=$data->ipc;?></td>
                                <td><?=$data->task_date;?></td>
                                <td><?=$data->task_time;?></td>
                                <?php if ($data->id_task == 1){ ?>
                                    <td>Login Site</td>  
                                <?php }else{ ?>
                                    <td>Logout Site</td>
                                <?php } ?>
                                <td>-</td>
                            </tr>
                        <?php } ?>  
                    <?php } ?>

Is it possible to put some jquery inside php ??

Need help to put some jquery function inside if statement . I want to hide my div when data from database is empty . I've done like this , and nothing happened.

                    <?php if(empty($all_data)){ ?>

                            <script>
                                $(document).ready(function () {
                                    $( '.table-wrapper').css("display","none");
                                });
                            </script>                       <===update

                    <?php }else{ ?>
                        <?php foreach($all_data->result() as $data){ ?>
                            <tr> 
                                <td><?php echo $data->id_history;?></td>
                                <td><?=$data->id_admin;?></td>
                                <td><?=$data->ipc;?></td>
                                <td><?=$data->task_date;?></td>
                                <td><?=$data->task_time;?></td>
                                <?php if ($data->id_task == 1){ ?>
                                    <td>Login Site</td>  
                                <?php }else{ ?>
                                    <td>Logout Site</td>
                                <?php } ?>
                                <td>-</td>
                            </tr>
                        <?php } ?>  
                    <?php } ?>

Is it possible to put some jquery inside php ??

Share Improve this question edited Mar 4, 2015 at 9:04 Ibnu Habibie asked Mar 4, 2015 at 8:07 Ibnu HabibieIbnu Habibie 7211 gold badge10 silver badges20 bronze badges 4
  • jQuery is JavaScript which is executed on the client side, PHP is executed on the server side. By definition you cannot put some jquery inside PHP... – vim Commented Mar 4, 2015 at 8:09
  • 1 Where do you call your function hiding() by the way? – Hanky Panky Commented Mar 4, 2015 at 8:11
  • 1 Define "nothing happened". Was there an error? What was the resulting output client-side? It doesn't look like anything ever invokes that hiding() function in the PHP code, so that echo statement within it would never be executed. In order to execute the code within a function, something has to call that function. – David Commented Mar 4, 2015 at 8:12
  • i've deleted function hiding() and just a jquery inside script. div with class table-wrapper is not hiding. how do i check a value of $all_data ? i tried with echo $all_data; inside if statement , nothing appear a value. – Ibnu Habibie Commented Mar 4, 2015 at 8:56
Add a ment  | 

6 Answers 6

Reset to default 2

Try this one

<!--Always generate table-->
<div class='table-wrapper'>
    <table>
       <?php foreach($all_data->result() as $data){ ?>
          <tr> 
         <td><?php echo $data->id_history;?></td>
         <td><?=$data->id_admin;?></td>
         <td><?=$data->ipc;?></td>
         <td><?=$data->task_date;?></td>
         <td><?=$data->task_time;?></td>
         <?php if ($data->id_task == 1){ ?>
            <td>Login Site</td>  
         <?php }else{ ?>
            <td>Logout Site</td>
         <?php } ?>
         <td>-</td>
     </tr>
<?php } ?>  
</table>
</div>

<!-- Hide Wrapper if no data -->
<script>
var div = $(".table-wrapper");
if(div.html() == "<table></table>") {
   div.css("display","none");
}
</script>

Nothing ever calls hiding(). So you conditionally define that function in PHP, but you never actually invoke it. If you want the contents of that function to execute in the if block then don't declare the hiding() function, just execute the code:

<?php if(empty($all_data)){
          echo "<script>
                    $(document).ready(function () {
                        $( '.table-wrapper').hide();
                    });
                </script>";
      }else{ ?>
...

Or maybe remove the echo and just emit the output directly, might look a little cleaner:

<?php if(empty($all_data)){ ?>
<script>
    $(document).ready(function () {
        $( '.table-wrapper').hide();
    });
</script>
<?php }else{ ?>
...

Though, if I'm being honest, hiding on document ready probably isn't the best approach. If .table-wrapper elements should be hidden when the page renders, conditionally style them as hidden (or don't emit them to the page at all if they're not supposed to be visible, depending on the dynamic functionality of the page). Emitting visible output and then hiding it could easily cause a poor user experience. Better to emit it as hidden in the first place or not emit it at all.

try it. dont want call any function.

<?php if(empty($all_data)){ ?>
                            <?php 

                                    echo "<script>
                                            $(document).ready(function () {
                                                $( '.table-wrapper').hide();
                                            });
                                          </script>";

                            ?>
                        <?php }?>

just need to call the function if query result is empty:

?php if(empty($all_data)){ ?>
    <?php 
        echo "<script>hiding();</script>";
    ?>
<?php }else{ ?>
    <?php foreach($all_data->result() as $data){ ?>
        <tr> 
            <td><?php echo $data->id_history;?></td>
            <td><?=$data->id_admin;?></td>
            <td><?=$data->ipc;?></td>
            <td><?=$data->task_date;?></td>
            <td><?=$data->task_time;?></td>
            <?php if ($data->id_task == 1){ ?>
                <td>Login Site</td>  
            <?php }else{ ?>
                <td>Logout Site</td>
            <?php } ?>
            <td>-</td>
        </tr>
    <?php } ?>  
<?php } ?>

and then

<script>
       $(document).ready(function () {
        function hiding(){
           $( '.table-wrapper').hide();
        }
       });
</script>

Well,delete the function hiding(){}.It's better to do this with php,like

<?php if(!empty($all_data)){ ?>
            <div class="table-wrapper">
              <?php foreach($all_data->result() as $data){ ?>
                            <tr> 
                                <td><?php echo $data->id_history;?></td>
                                <td><?=$data->id_admin;?></td>
                                <td><?=$data->ipc;?></td>
                                <td><?=$data->task_date;?></td>
                                <td><?=$data->task_time;?></td>
                                <?php if ($data->id_task == 1){ ?>
                                    <td>Login Site</td>  
                                <?php }else{ ?>
                                    <td>Logout Site</td>
                                <?php } ?>
                                <td>-</td>
                            </tr>
            <?php } ?>  
          </div>
<?php } ?>

Try this :

<?php if(empty($all_data)) : ?>

        <script>
            $(document).ready(function () {
                $( '.table-wrapper').css("display","none");
            });
        </script>

<?php else : ?>
    <?php foreach($all_data->result() as $data) : ?>

        <tr> 
            <td><?=$data->id_history;?></td>
            <td><?=$data->id_admin;?></td>
            <td><?=$data->ipc;?></td>
            <td><?=$data->task_date;?></td>
            <td><?=$data->task_time;?></td>

            <?php if ($data->id_task == 1) : ?>
                <td>Login Site</td>  
            <?php else : ?>
                <td>Logout Site</td>
            <?php endif; ?>

            <td>-</td>
        </tr>

    <?php endforeach; ?>  
<?php endif; ?>
发布评论

评论列表(0)

  1. 暂无评论