ds3422222 2018-01-17 14:36
浏览 45
已采纳

php代码后滚动到div [关闭]

Hi I would like to scroll to a div after running a php code that runs when i press the it searches the database and posts to a table thats located in that div

<?php
session_start();
include_once 'includes/dbh.php';
if(isset($_POST['search']))
{
    $searchvalue = $_POST['searchvalue'];
    $query = "SELECT * FROM `users` WHERE CONCAT(`user_id`, `user_username`, `user_password`, `user_name`, `user_gender`, `user_age`, `user_contact`, `user_email`, `user_address`, `user_industry`, `user_findout`, `user_admin`) LIKE '%".$searchvalue."%'";
    $result = mysqli_query($conn,$query);
    echo"
    <script type='text/javascript'>
    $(document).ready(function () {
            $('html,body').animate({
                scrollTop: $('.graph').offset().top
            },
                'slow');
        })
    </script>";
else
{
    $query = "SELECT * FROM `users`";
    $result = mysqli_query($conn,$query);
}

?>
  • 写回答

1条回答 默认 最新

  • dongtie0929 2018-01-17 15:13
    关注

    The session_start() needs to be before any HTML output so that means somewhere above the <!DOCTYPE html> tag. The rest of the code should be in the body of the document so you can run the script.

    Also, use the $( window ).load(); instead of $(document).ready(); to be sure that the whole HTML page is loaded including CSS and javascript files.

    Here is the corrected code:

    <?php
        session_start();
        include_once 'includes/dbh.php';
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <?php
    if(isset($_POST['search']))
    {
      // at least use some kind of escaping.. (Use prepared statements!)
      $searchvalue = mysqli_real_eascape_string($conn, $_POST['searchvalue']);
      $query = "SELECT * FROM `users` WHERE CONCAT(`user_id`, `user_username`, `user_password`, `user_name`, `user_gender`, `user_age`, `user_contact`, `user_email`, `user_address`, `user_industry`, `user_findout`, `user_admin`) LIKE '%".$searchvalue."%'";
      $result = mysqli_query($conn,$query);
      echo '<script type="text/javascript">
         $( window ).load(function()
         {
              $("html,body").animate({
                  scrollTop: $(".graph").offset().top
              },
                 "slow");
         });
      </script>';
    else
    {
        $query = "SELECT * FROM `users`";
        $result = mysqli_query($conn,$query);
    }
    ?>
    </body>
    
    </html>
    

    Note: To help make your application more secure, I would suggest looking into mysqli prepared statements. Read on prepared statements: http://php.net/prepared-statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?