Javascript 执行php而不刷新页面

Javascript 执行php而不刷新页面,javascript,html,jquery,css,ajax,Javascript,Html,Jquery,Css,Ajax,我想在点击submit按钮时通过php执行javascript代码……这段代码会执行,但页面会刷新……我不想页面刷新……我可以通过ajax或jquery实现吗?如何实现 <html> <head> <style> .popup { visibility:hidden; } </style> <

我想在点击submit按钮时通过php执行javascript代码……这段代码会执行,但页面会刷新……我不想页面刷新……我可以通过ajax或jquery实现吗?如何实现

<html>
    <head>
        <style>
            .popup
            {
                visibility:hidden;
            }
        </style>
    </head>
    <body>
        <form method="post">
        <div id="Popup" class="popup">
            Sorry! The vehicle is already booked on the corresponding date.
            Select a different date or book another vehicle.
        </div>
        <input type="submit" name="submit" value="display">
        </form>
        <?php
            if(isset($_POST["submit"]))
            {
                echo '<script>document.getElementById("Popup").style.visibility="visible";</script>';
            }
        ?>
    </body>
</html>

.弹出窗口
{
可见性:隐藏;
}
很抱歉车辆已在相应日期预订。
选择其他日期或预订其他车辆。

尝试使用javascript在第二个平面上执行代码

使用PHP无法做到这一点。正如程序员所说,PHP只能在生成页面时执行,因为它是服务器端语言。要做到这一点,您需要一种客户端语言,如JavaScript。下面是一个您可以尝试的代码示例。不要只是举个例子,还要了解每个组件的功能:

<html>
    <head>
        <style>
            #popup
            {
                display:none;
            }
        </style>
    </head>
    <body>
        <button id="showbtn">Show Popup</button>
        <div id="popup">
            Sorry!The vehicle is already booked on the corresponding date.
            Select a different date or book another vehicle.
        </div>

        <script>
            const mypopup = document.getElementById("popup");
            const mybutton = document.getElementById("showbtn");

            mybutton.addEventListener('click', function(event) {
                mypopup.style.display = "block";
            })
        </script>

    </body>
</html>

#弹出窗口
{
显示:无;
}
显示弹出窗口
很抱歉车辆已在相应日期预订。
选择其他日期或预订其他车辆。
const mypopup=document.getElementById(“popup”);
const mybutton=document.getElementById(“showbtn”);
mybutton.addEventListener('click',函数(事件){
mypopup.style.display=“block”;
})

你说的“相同的东西”是什么意思学习javascript的基础知识,你可以轻松解决类似的问题。如果不刷新,你就无法“执行PHP”。PHP是一种服务器端语言,因此,它仅在生成页面时执行。此外,您不需要使用PHP来实现这一点——只需将弹出窗口直接放入HTML,或者更好的是,不要在CSS中隐藏弹出窗口。