我将在哪里运行这个 validateForm 方法?我是 PHP 开发新手,正在掌握 Web 开发的窍门

发布于 2024-08-22 23:46:26 字数 1412 浏览 3 评论 0原文

这是我所拥有的:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陪我终i 2024-08-29 23:46:26
function validateForm(){
 if ( ) // Your form conditions go here
 {
    return true;
 } else {
    return false;
 }
} 
if ( $_GET ) // Only validate the form if it was submitted
{
   $validForm = validateForm();
} else {
   $validForm = false;
}

某些条件的示例:

if ( !empty ( $_GET[ 'name' ] ) and !empty ( $_GET[ 'lastname' ] ) ) // Check if the user entered something in both fields

我只能建议您也更改 getValue 函数。

最好更改

return $_GET[$field];

为类似这样的内容,

return htmlspecialchars ( trim ( $_GET[$field] ) );

这将删除不必要的空格并转义一次输出。

您应该采取的下一步或可能的第一步是将表单从 GET 更改为 POST,因为在大多数情况下,通过 发送表单数据是一个坏主意。代码>获取。

function validateForm(){
 if ( ) // Your form conditions go here
 {
    return true;
 } else {
    return false;
 }
} 
if ( $_GET ) // Only validate the form if it was submitted
{
   $validForm = validateForm();
} else {
   $validForm = false;
}

Examples for some conditions:

if ( !empty ( $_GET[ 'name' ] ) and !empty ( $_GET[ 'lastname' ] ) ) // Check if the user entered something in both fields

I can only recommend you to change your getValue function too.

It would be a good idea to change

return $_GET[$field];

to something like

return htmlspecialchars ( trim ( $_GET[$field] ) );

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 to POST as it is, in most cases, a bad idea to send form data via GET.

十年九夏 2024-08-29 23:46:26

您必须在提交按钮中添加一个名称:

  <input type="submit" value="enviar" name="validate" />

validateForm 声明之后

在您放置的

if(getValue("validate") != "")
validateForm();

You have to add a name to your submit button :

  <input type="submit" value="enviar" name="validate" />

And after the declaration of validateForm

you put

if(getValue("validate") != "")
validateForm();
思念满溢 2024-08-29 23:46:26
  1. 当您尝试验证表单数据时,我会检查 getValue 函数中的 $_POST-variables (您检查 $_GET-variables 是通过 URL 提交的数据),您可能还想添加“trim”
  2. 给提交按钮一个 id 并检查表单是否已以这种方式提交

,例如

设置 $validForm 看起来像这样: $validForm = validateForm(); (而不是“$validForm = false;”)

您的 validateForm 函数应该检查“$_POST["submitButton"]”是否设置以及您的数据是否有效(设置、正确类型等),如果全部可以的话,返回“true”,否则返回“false”

  1. I'd check for the $_POST-variables in your getValue-function as you're trying to validate form-data (your checking $_GET-variables which is data submitted through the URL), you also may want to add "trim"
  2. give the submit-button an id and check if the form has beed submitted that way

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"

药祭#氼 2024-08-29 23:46:26
<html>
  <head>
  <?php
    $validForm = validateForm();

C.

<html>
  <head>
  <?php
    $validForm = validateForm();

C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文