问题描述
对这个有点陌生,所以如果我的问题经常被问到或者没有任何意义,我很抱歉。我试图让 PHP 将用户的输入回显到屏幕上的表单中。每当我测试表单时,没有任何变化。您对这个问题的处理方法是什么?
<body>
<script src="index.js"></script>
</body>
</html>
<center><form name="name" method="get" class="form" id="nameInput">
<center><h4>Name</h4></class></center>
<center><input>
<center><div><form name="post" method="get" class="form" id="postInput">
<center><h4>Post</h4>
<center><input></div>
<center><div><button id="postButton">Post</button></div>
<?PHP
if(empty($_GET['name']) && empty($_GET['post'])){
echo "<h3>test</h3>";
$name = $_GET['name'];
$post = $_GET['post'];
}else{
$name = $_GET['name'];
$post = $_GET['post'];
echo "<h3> User: $name </h3>";
echo "<h3> Post: $post </h3>";
}
?>
解决方法
这样的事情可能会帮助您入门:
<form method="get" class="form">
<center>
<strong>Name</strong>
<input type="text" name="name" value="" />
</center>
<center>
<strong>Post</strong>
<input type="text" name="post" value="" />
</center>
<center>
<input type="submit" value="Go!" />
</center>
</form>
,
试试这个:-
<body>
<center>
<form method="get" class="form" id="nameInput">
<h4>Name</h4>
<input name="Name" type="name">
<h4>Post</h4>
<input name="Post type="textarea">
<br><input type="submit" name="sub">
</form>
</center>
<?php
if(isset($_GET['sub'])) {
if(empty($_GET['Name']) && empty($_GET['Post'])) {
echo '<h2>Name: '.$_GET['Name'].'</h2>';
echo '<h2>Post: '.$_GET['Post'].'</h2>';
}
}
?>
<script src="Index.js"></script>
</body>
</html>
错误在于您为表单元素提供了名称属性。每个输入都应该有自己的名称,而不是表单。并尝试将帖子和名称输入保留在一个表单中。
希望这会有所帮助。
,首先,主要问题是由您的 HTML
代码引起的。
Mozilla 说;
<form>
HTML 元素表示包含
用于提交信息的交互式控件。
因此,您需要将所有 input
/ textarea
等元素都放在一个表单中。为了做到这一点并使它更简单,我写了下面的代码片段。这在这个例子中会更有意义;
<form method='GET'>
<!-- INPUT Start -->
<input type="text" name='name'>
<input type="text" name='post'>
<!-- INPUT End -->
<!-- SEND BUTTON Start -->
<input type="submit" value='send'>
<!-- SEND BUTTON End -->
</form>
<form>
HTML 元素的另一个重要属性是 action='where_are_you_going_path'
,但现在这是不必要的,因为您想重定向与您形成的页面相同的页面。
其次,当您打开页面时出现一些 PHP 错误;
遇到 PHP 错误 严重性:注意
消息:未定义索引:名称
...
发生这种情况是因为您错误地检查了 $_GET
数组。因为对于一个还没有赋值的索引,你无法检查它是否为空。
要解决这个问题,您可以尝试使用 isset()
方法。
<?php
if(isset($_GET['name']) && isset($_GET['post'])){
echo "<h3> User:".$_GET['name']." </h3>";
echo "<h3> Post: ".$_GET['post']." </h3>";
}
?>
我想提出这个建议;在跳转到 PHP
之后使用 Mozilla Documentation. 学习 HTML/CSS 基础知识。这对你来说会更容易。