如何解决从php中的方法html文本字段获取值不起作用
我试图在 php 中单击 html 中的按钮后获取文本字段的值。我用 get 和 post 方法尝试过,但没有任何效果。我的代码是:
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<div class="btn-group" role="group" aria-label="...">
<form method="post">
<input type="submit" name="test" id="test" class="btn btn-info" value="RUN" /><br/>
</form>
</div>
<div class="formoutline">
<form action="" method="get">
<input name="subject2"type="text" id="subject2" class="form-control" />
</form>
</div>
<?php
function testfun()
{
echo $_GET["subject2"];
}
if(array_key_exists('test',$_POST)){
testfun();
}
?>
有人可以帮我吗?
解决方法
您有两个单独的表单,但假设您尝试测试两种不同的方法,这应该会为您指明方向。
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<div class="btn-group" role="group" aria-label="...">
<form method="post">
<input type="text" name="test" id="test" class="btn btn-info" /><br/>
<input type="submit" name="post_submit" value="Submit via Post" />
</form>
</div>
<div class="formoutline">
<form method="get">
<input name="test" type="text" id="subject2" class="form-control" />
<input type="submit" name="get_submit" value="Submit via Get" />
</form>
</div>
<?php
echo 'Get Form: ' . (filter_input(INPUT_GET,'test') ?: 'Not Submitted') . '<br />';
echo 'Post Form: ' . (filter_input(INPUT_POST,'test') ?: 'Not Submitted') . '<br />';
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。