简体   繁体   中英

Javascript is not working when page reloads

this is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title> Daftar Berita </title>
    <meta http-equiv="content-type"
          content="text/html; charset=utf-8"/>
</head>
<body>

    <form method="post" action="">
        <?php include 'formpencarian.html.php'; ?>

    <div id="newssection" style="margin-top: 50px; clear: both; display: block;">
        <?php include 'newssection.php'; ?>
    </div>

    <?php
            if(isset($_POST["cari"])){
                echo "<div id='searchsection' style='margin-top: 50px; clear: both; display: block;'>";
                include 'search.php';
                echo "</div>";
                echo "<script>document.getElementById('newssection').style.display = 'none';</script>";
            }
    ?>

    <script>
        function showMore(id){
            document.getElementById('isi'+id).style.display = "block";
            document.getElementById('more'+id).style.display = "none";
            document.getElementById('less'+id).style.display = "block";
        }

        function showLess(id){
            document.getElementById('isi'+id).style.display = "none";
            document.getElementById('more'+id).style.display = "block";
            document.getElementById('less'+id).style.display = "none";
        }

        function advancedSearch(){
            document.getElementById('advanced').style.display = "none";
            document.getElementById('searching').style.display = "block";
            document.getElementById('no').style.display = "block";
        }

        function noSearch(){
            document.getElementById('advanced').style.display = "block";
            document.getElementById('searching').style.display = "none";
            document.getElementById('no').style.display = "none";
        }

    </script>
</body>
</html>

basically, the search.php file contains mysql query for displaying results.

the problem is, when this page reloaded (because i sent the POST variable to this page) the javascript doesn't work. as a result, i can't show or hide certain divs on my results page (because the results page is formatted just the same as this page).

Am i doing something wrong?? Please help. Thanks.

in the bigger JS part there are only function definitions , but now calls - to make them run in any case, you need to call them, like:

noSearch();
advancedSearch();

and this part doesn't seem to be related to $_POST checking .. to avoid not calling them in search.php, you should remove if(isset($_POST["cari"])){ check from your main file and do it inside of the search.php

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