我将在哪里运行这个 validateForm 方法?我是 PHP 开发新手,正在掌握 Web 开发的窍门
这是我所拥有的:
<html>
<head>
<?php
$validForm = false;
function getValue($field){
if(isset($_GET[$field])){
return $_GET[$field];
}
else{
return "";
}
}
function validateForm(){
//magic goes here.
}
?>
</head>
<body>
<?php if($validForm == false){ ?>
<form action="class2.php" method="get">
<dl>
<dt>First Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('name')) ?>" name="name" />
</dd>
<dt>Last Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('lastname')) ?>" name="lastname" />
</dd>
<br />
<dt>
<input type="submit" value="enviar" />
</dt>
</dl>
</form>
<?php
} else {
?>
<h1>Congratulations, you succesfully filled out the form!</h1>
<?php }
?>
</body>
我应该把 validateForm() 调用放在哪里?我不知道。 我想要的是继续显示表单,直到 $validForm 变量为 true。 :)
谢谢你,所以你总是帮助我。
Here's what I have:
<html>
<head>
<?php
$validForm = false;
function getValue($field){
if(isset($_GET[$field])){
return $_GET[$field];
}
else{
return "";
}
}
function validateForm(){
//magic goes here.
}
?>
</head>
<body>
<?php if($validForm == false){ ?>
<form action="class2.php" method="get">
<dl>
<dt>First Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('name')) ?>" name="name" />
</dd>
<dt>Last Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('lastname')) ?>" name="lastname" />
</dd>
<br />
<dt>
<input type="submit" value="enviar" />
</dt>
</dl>
</form>
<?php
} else {
?>
<h1>Congratulations, you succesfully filled out the form!</h1>
<?php }
?>
</body>
Where would I put the validateForm() call? I'm not sure.
What I want is to continue showing the form until the $validForm variable is true. :)
Thank you SO you always help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
某些条件的示例:
我只能建议您也更改
getValue
函数。最好更改
为类似这样的内容,
这将删除不必要的空格并转义一次输出。
您应该采取的下一步或可能的第一步是将表单从
GET
更改为POST
,因为在大多数情况下,通过发送表单数据是一个坏主意。代码>获取。
Examples for some conditions:
I can only recommend you to change your
getValue
function too.It would be a good idea to change
to something like
This will remove unnecessary whitespace and escape the output at once.
The next or probably first step you should take would be to change your form from
GET
toPOST
as it is, in most cases, a bad idea to send form data viaGET
.您必须在提交按钮中添加一个名称:
validateForm 声明之后
在您放置的
You have to add a name to your submit button :
And after the declaration of validateForm
you put
,例如
设置 $validForm 看起来像这样:
$validForm = validateForm();
(而不是“$validForm = false;”)您的 validateForm 函数应该检查“$_POST["submitButton"]”是否设置以及您的数据是否有效(设置、正确类型等),如果全部可以的话,返回“true”,否则返回“false”
e.g.
<input type="submit" value="enviar" id="submitButton" />
setting $validForm would look this this:
$validForm = validateForm();
(instead of "$validForm = false;")your validateForm-function should then check whether "$_POST["submitButton"]" is set and if your data is valid (set, right type, etc. etc.), if all of that is ok, return "true", otherwise "false"
C.
C.