PHP 中的验证

发布于 2024-08-28 06:48:21 字数 160 浏览 11 评论 0原文

我有一个提交页面html。当我提交表单数据时,它会转到一个 php 页面,该页面将数据插入数据库中。

我的问题是,在不使用 javascript 的情况下,如何确保 html 页面上的数据不为 null 或为空?无论如何,这可以在 php 中完成吗?谢谢!

i have a submit page in html. when i submit the form data, it goes to a php page, which inserts the data in the database.

my question is how would i ensure that the data on the html page is not null or empty without using javascript? is there anyway this could be done in php? thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

薄荷港 2024-09-04 06:48:21

像往常一样简单地使用控制结构:

$non_empties = array('name', 'email', 'address');

foreach ($non_empties as $field) {
    if (! isset($_POST[$field]) || $_POST[$field] == '') {
        show_error("Please fill your $field, thank you.");
    }
}

$db->insert();

更高级的示例:

// Defined in your library
function validate_as_non_empty(Array $non_empties) {
    foreach ($non_empties as $field) {
        if (! isset($_POST[$field]) || $_POST[$field] == '') {
            throw new Exception("Please fill your $field, thank you.");
        }
    }
}

// Defined in your library
function validate_as_foo(Array $arr) { ... }

// Your request handler
try {
    validate_as_non_empty(array('name', 'address'));
    validate_as_foo(array('email'));
    $db->insert();
} catch (Exception $e) {
    show_error($e->getMessage());
}

至少有百万种方法可以进行验证。通常你有某种框架可以依赖。以上例子仅供参考。

Simply use control structures as usual:

$non_empties = array('name', 'email', 'address');

foreach ($non_empties as $field) {
    if (! isset($_POST[$field]) || $_POST[$field] == '') {
        show_error("Please fill your $field, thank you.");
    }
}

$db->insert();

More advanced example:

// Defined in your library
function validate_as_non_empty(Array $non_empties) {
    foreach ($non_empties as $field) {
        if (! isset($_POST[$field]) || $_POST[$field] == '') {
            throw new Exception("Please fill your $field, thank you.");
        }
    }
}

// Defined in your library
function validate_as_foo(Array $arr) { ... }

// Your request handler
try {
    validate_as_non_empty(array('name', 'address'));
    validate_as_foo(array('email'));
    $db->insert();
} catch (Exception $e) {
    show_error($e->getMessage());
}

There are at least million ways to do validation. Usually you have some kind of framework to rely on. The above examples are merely given for inspiration.

妖妓 2024-09-04 06:48:21

在您的 php 页面的最顶部(您在表单的 action 属性中链接到的页面)放置:

if (isset($_POST['submit_check']) && strlen($_POST['whatever'])) { // submit_check is a hidden form field
    insert_to_db();
    header('Location: success.php'); // redirect to another page to prevent double-submits
    exit;
}

因此您的 html 可能如下所示:

<form action="" method="post"> 
    <input type="text" name="whatever"/>
    <input type="hidden" name="submit_check" value="1"/> 
</form>

空白操作值意味着发布到同一页面,但您可以发布到另一个页面如果你愿意的话。

At the very top of your php page, (the page you linked to in the action attribute of the form) put:

if (isset($_POST['submit_check']) && strlen($_POST['whatever'])) { // submit_check is a hidden form field
    insert_to_db();
    header('Location: success.php'); // redirect to another page to prevent double-submits
    exit;
}

So your html might look like:

<form action="" method="post"> 
    <input type="text" name="whatever"/>
    <input type="hidden" name="submit_check" value="1"/> 
</form>

the blank action value means to post to the same page, but you can post to another page if you want.

幼儿园老大 2024-09-04 06:48:21

在 PHP 文件中使用 $_POST 超全局变量检索提交的数据,如下所示:

$_POST['firstname'] 对于每个正在 POST 的 HTML 元素。例如,在 HTML 中:

<input type="text" name="firstname" />

然后使用 PHP、正则表达式、isset 等验证数据,并相应地重定向到成功页面或返回到 HTML 表单。

您还可以使用 Ajax 执行相同的操作(将应用相同的 PHP 验证,只是数据传输模型略有不同)

Retrieve the submitted data using the $_POST super-global in your PHP file like this:

$_POST['firstname'] for each HTML element that is being POST-ed. For eaxmple, in your HTML:

<input type="text" name="firstname" />

Then validate the data using PHP, with regex, isset, etc and redirect accordingly either to a success page, or back to the HTML form.

You can also do the same using Ajax (the same PHP validation will apply, just the data transfer model is slightly different)

半暖夏伤 2024-09-04 06:48:21

如果您问的是,“我能否确保数据在客户端有效而无需 JavaScript”,那么答案是否定的。

但是,如果您只想确保在将数据插入数据库之前验证数据,那么当然可以。

foreach ($_POST as $key=> $value) {

  switch ($key) {
   case 'my_value_name':
      //test $value is not null and other validation;
      //insert into database function
      break;

   ...

  }

}

If what you are asking is, 'can I ensure that the data is valid on the client side without javascript' then the answer is no.

But if all you want to do is make sure to validate the data before you insert it into your database, then certainly.

foreach ($_POST as $key=> $value) {

  switch ($key) {
   case 'my_value_name':
      //test $value is not null and other validation;
      //insert into database function
      break;

   ...

  }

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