javascript - 表单提交 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>
解决方案
您不需要使用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 -->
推荐阅读
- python - 在服务器上生成 Microsoft Excel
- cobol - 使用最新的编译器重新编译旧的 COBOL 源代码
- java - JAVA 8 要从列表分配给对象的内存/集合/列表值
- php - 嵌入式网站如何分辨谁在嵌入它?
- javascript - 动态绘制吉他和弦
- html - 背景模糊但保留表格 CSS 中的文本颜色
- android - 超级账本客户端
- kubernetes - 如何使用 python API 以编程方式从 GCP 服务帐户生成 kubernetes 配置
- java - JavaFX11 的 javafx.stage.Stage 版本无效
- django - 如何更新字段实例的选择选项,而不是整个字段?