skip to Main Content

I want to execute the javascript code through php when the submit button is clicked……this code does it but the page refreshes…..i dont want the page to refresh……can I achieve it through ajax or jquery and how?

<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>

2

Answers


  1. try to use javascript to execute on second plane the code

    Login or Signup to reply.
  2. You cannot do this with PHP. As PaulProgrammer stated PHP can only execute when the page is generated as it is a server side language. You need a client side language such as JavaScript to do this. Here is an example of code you can try out. Don’t just take the example, but learn what each of the components do:

    <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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search