PHP验证表格
我很难显示我在PHP教程中拨打printf中的输出。我刚刚遵循了本教程,但仍然无法弄清楚在Localhost中显示printf内部的变量有什么问题。有人可以帮助我吗?谢谢。
<?php
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
};
if (isset($_POST['password'])) {
$password = $_POST['password'];
};
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
};
if (isset($_POST['color'])) {
$color = $_POST['color'];
};
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
};
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
};
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
};
//here's the problem i cant resolve printing out the output//
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T&C: %s',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode('', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action=""
method="post">
User name: <input type="text" name="name"><br>
Password: <input type="password" value="password"><br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other<br/>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select><br>
Languages spoken:
<select name="languages[]"multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select><br>
Comments: <textarea name="comments"></textarea><br>
<input type="checkbox" name="tc" value="ok"> I accept the T&C<br>
<input type="submit" value="Register">
</form>`
I am having trouble showing the output that i called in printf in php tutorial. I just followed the tutorial but still can't figure out what is wrong in displaying the variables inside the printf in localhost. Is anyone can help me. Thank you.
<?php
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
};
if (isset($_POST['password'])) {
$password = $_POST['password'];
};
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
};
if (isset($_POST['color'])) {
$color = $_POST['color'];
};
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
};
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
};
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
};
//here's the problem i cant resolve printing out the output//
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T&C: %s',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode('', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action=""
method="post">
User name: <input type="text" name="name"><br>
Password: <input type="password" value="password"><br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other<br/>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select><br>
Languages spoken:
<select name="languages[]"multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select><br>
Comments: <textarea name="comments"></textarea><br>
<input type="checkbox" name="tc" value="ok"> I accept the T&C<br>
<input type="submit" value="Register">
</form>`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此处的问题是
如果(ISSET($ _ post ['submit']))
没有任何表格字段,即namesubmit
,并且执行永远不会成立。 删除提交条件的if条件,否则将Sumbit按钮名称称为提交The problem here is
if (isset($_POST['submit']))
there is no any form field with the namesubmit
and it will never become true to execute. Remove the if Condition with submit or Else give the sumbit button name as Submit您以前的代码中的错误是:
;
。名称
属性为密码以表单中的输入指定。如果(isset($ _ post ['submit'])){...}
而无需在表单中指定name =“ submit”
属性。爆破
函数的使用无效,它将生成输出就像enfrit
,反之亦然。其他提示
必需的
属性添加到每个输入
以防止发送空形式数据。method =“ post”
,因此,无需验证每个输入字段为if(isset($ _ post ['fieldName']))){... }
。如果(ISSET($ _ post ['submit'])){...}
将以post
方法发送所有表单数据。最后,这是您的更新代码。
Errors in your previous code are:
;
are needed after endingif
statements.name
attribute is specified for password input in the form.if (isset($_POST['submit'])) {...}
without specifying thename="submit"
attribute in the form.implode
function which will generate output be likeenfrit
or vice versa.Additional Tips
required
attribute to eachinput
to prevent from sending empty form data.method="post"
in the form tag so, no need for validating each input field asif (isset($_POST['fieldname'])) {...}
.if (isset($_POST['submit'])) {...}
will send all the form data asPOST
method.And lastly, here is your updated code.