php - 为什么点击 input[type='button'] 时 PHP 无法识别?
问题描述
我只想简单地点击echo 'OK!'
输入时type="button" name="calculateBtn"
,但PHP无法识别该条件if (isset($_POST['calculateBtn']))
,所以不会做任何事情,有人可以告诉我这是为什么吗?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="" method="post">
<div>
<input id="input" type="text">
<?php
if (isset($_POST['calculateBtn'])) {
echo 'ok';
}
?>
<input name="calculateBtn" value="change the value" type="button">
</div>
</form>
</div>
</div>
</body>
</html>
解决方案
首先,您没有在表单中定义动作,其次按钮类型设置为按钮而不是提交,它不会提交表单。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="page.php" method="post">
<div>
<input id="input" type="text">
<?php
if (isset($_POST['calculateBtn'])) {
echo 'ok';
}
?>
<input name="calculateBtn" value="change the value" type="submit">
</div>
</form>
</div>
</div>
</body>
</html>
请将 page.php 设置为您的页面(文件)的名称。
推荐阅读
- django-forms - 我在安装 django cripsy forms 时遇到错误
- javascript - TypeError: navigator.getUserMedia 不是函数
- python - 使用 Selenium 引用页面上的某个元素时遇到问题
- dynamic - Camel Dynamic To(又名 ToD)是否仍然需要明确?
- javascript - 我在处理外部文件时遇到问题 | 不和谐.js
- javascript - 只有在 Safari 中点击其他元素 2-3 次后,来自 iframe 的焦点才会模糊
- operating-system - 如何使用 GDB 调试带有自定义 GDT 的内核?(断点失败)
- powerbi - 根据其他列中的键值转置列中的值
- c# - 使用 EF CORE 检索可为空的对象
- javascript - 即使在允许脚本访问 Google 表格后,调用 getDataSourceFormula() 时仍然存在授权错误