首页 > 解决方案 > 表单提交 isset 不起作用,当我单击按钮时,它总是说大声笑而不是说工作

问题描述

我已经为我的网站编写了这段代码,但它不起作用我有一个小的 php 代码,我想在其中检查提交按钮是否被点击,请帮忙

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>THUNDR</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <section id="thundr_back">
            <div id="thundr_nav">
                <div id="thundr_head">!url shortner</div>
            </div>
            <div id="thundr_body">
                <div id="thundr_msg">
                    <div id="msg1">!url shortner</div>
                </div>
                <div id="thundr_bx">
                    <form method="post">
                        <input type="text" id="thundr_link" name="thundr_link" value="" placeholder="enter your link here" required>
                        <input type="submit" id="submit" name="submit" value="shorten">
                        <?php
                            if(isset($_POST['submit'])) {
                              echo "<script> alert('working'); </script>";
                            } else {
                              echo "<script> alert('lol'); </script>";
                            }
                            ?>
                        <div id="thundr_custom">need custom names for your url?</div>
                    </form>
                </div>
            </div>
            <div id="thundr_ft"></div>
        </section>
        <script src="main.js"></script>
    </body>
</html>

标签: javascriptphphtmlcssajax

解决方案


您不需要使用else. 确保您编写代码的文件具有 .php扩展名。一个例子是,

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" name="submit" value="Submit">
</form> 
<p>If you click the "Submit" button, the form-data will be submitted on the same page.</p>

</body>
</html>

<!-- You can put this anywhere, start -->
<?php
 if(isset($_POST['submit'])){
  echo '<script>alert("Form Submission Success")</script>
 }
?>
<!-- You can put this anywhere, end -->

推荐阅读