PHP 和 HTML 集成

发布于 2024-08-15 12:25:12 字数 628 浏览 9 评论 0原文

我有一个 php 文件,其中包含 HTML 表单,然后是 PHP 逻辑,然后是 HTML 页脚......按这个顺序。

我正在尝试使用 . 将表单中的值获取到页面底部(但在页脚之前)的一些 php 验证逻辑中

。验证发现错误,php 逻辑将 die(),因此,我的 HTML 页脚将不会被执行。

尽管我的 php die(); 有没有办法让我的 HTML 页脚执行? ...或者是否有更好的整体方法来集成我的 HTML 和 PHP?谢谢!任何帮助将不胜感激。


编辑: 实际上,我的代码中几乎所有要连接到数据库的地方都有 die() 。如果用户凭据正确,它们就会连接。如果凭据错误,那么它将 die()。

这是 die() 的良好实践和使用吗?看来我的问题的解决方案是使用 return() 代替 die()...以便继续处理 HTML 页脚。

另外,我遇​​到了诸如 mysql_connect() 或 die() 之类的情况。在处理 HTML 之前执行 die() 时,我如何继续处理剩余的 HTML 页面? ..我不认为 mysql_connect() 或 return;好的做法对吗?

再次非常感谢!反馈非常有帮助!

I have a php file that contains a HTML form, then PHP logic, then an HTML footer...in that order.

I am trying to get the values from the form into some php validation logic in the botton of the page (but before the footer) using <?php VALIDATION LOGIC HERE ?>.

the problem is when the validation finds an error, the php logic will die() and hence, my HTML footer will not be executed.

is there a way to get my HTML footer to execute despite my php die();? ...or is there a better overall way to integrate my HTML and PHP? Thanks! Anyhelp would be much appreciated.


EDIT:
I actually have die() almost everywhere in my code where I am about to connect to a database. If the user credentials are correct, they connect..if credentials are wrong then it will die()..

Is this good practice and use of die()? it seems the solution to my problem is to use return() INSTEAD OF die()...in order to continue processing the HTML footer.

Also, I have situations such as mysql_connect() or die(). How can i would continue processing the remaining HTML page when die() is executed before the HTML is processed? ..i don't think mysql_connect() or return; is good practice right?

Thanks so much again in advance! The feedback has been very helpful!

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

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

发布评论

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

评论(5

七色彩虹 2024-08-22 12:25:12

与其他状态一样,您应该有多个文件; header.php、index.php、footer.php、formvalidator.php。
在您的index.php 文件中,您应该包含header.php 和footer.php。
在表单标记中,操作设置为加载 formvalidator.php

在表单验证器脚本中,您可以有如下内容:

  // All form fields are identified by '[id]_[name]', where 'id' is the 
  // identifier of the form type.
  // The field identifier we want to return is just the name and not the id.
  public function getFormData() {
    $formData = array();
    foreach ($_POST as $key => $value) 
    {
      // Remove [id]_ 
      $name = preg_replace('!.*_!', '', $key);

      if (is_array($value)) {
        $formData[$name] = implode(',', $value);
      } else {
        $formData[$name] = $value;
      }
    }
    return $formData;
  } 

现在您可以循环遍历数组并验证每个字段。
如果发现错误,请回显错误消息,否则处理该表单。

更新
回复您的更新。

你不应该“永远”使用 die()。相反,退出您所在的函数并返回错误消息。如果你只是死掉(),你永远不知道哪里出了问题。

除非单击提交按钮,否则无法对表单进行服务器验证。
您可以将我给您的代码放在与表单相同的 PHP 文件中,当您提交时,您只需重新加载同一页面(只需设置 action=" ;")

如果您想在提交之前验证字段,则必须使用 javascript 进行此操作,例如 jQuery.validate

嗯...看来您需要更多有关如何将 PHP 与 HTML 混合的知识。
查看初学者指南,了解如何在PHP。

As other states, you should have multiple files; header.php, index.php, footer.php, formvalidator.php.
In your index.php file, you should include header.php and footer.php.
In the form tag, action is sett to load formvalidator.php

In the form validator script, you could have something like this:

  // All form fields are identified by '[id]_[name]', where 'id' is the 
  // identifier of the form type.
  // The field identifier we want to return is just the name and not the id.
  public function getFormData() {
    $formData = array();
    foreach ($_POST as $key => $value) 
    {
      // Remove [id]_ 
      $name = preg_replace('!.*_!', '', $key);

      if (is_array($value)) {
        $formData[$name] = implode(',', $value);
      } else {
        $formData[$name] = $value;
      }
    }
    return $formData;
  } 

Now you can loop through the array and validate each field.
If you find an error, echo an error message, otherwise process the form.

Update
Answer to your update.

You should "never" use die(). Instead, exit thefunction you are in and return an error message. If you simply die(), you never know what went wrong where.

It is not possible to do server validation of a form unless you click the submit button.
You can put the code I gave you in the same PHP file as the form, and when you submit, you simply reload the same page (just set action="<?= $_SERVER['PHP_SELF'] ?>")

If you want to validate fields before submit, you must to this using javascript, like e.g. jQuery.validate.

Hmm... seem like you need some more knowledge of how to mix PHP with HTML.
Take a look at this beginners guide on how to work with forms in PHP.

×眷恋的温暖 2024-08-22 12:25:12

我将使用外部文件进行表单处理和验证,然后在错误/成功时重定向回表单页面,显示错误/成功消息。

I would use an external file for form processing and validation, then redirect back to the form page on error/success, displaying an error/success message.

就此别过 2024-08-22 12:25:12

我只制作一个 header.php 和一个 footer.php 文件。如果有错误就返回而不是死掉。

这样,您就可以在页面顶部放置:

<?php include('header.php');?>

///put in whatever html there may be


<?php 
/// put your form and processing info here
///just return if you need to prevent further processing
?>

///put in whatever html there may be
<?php include('footer.php');?>

I just make one header.php and one footer.php file. If there is an error just return instead of die.

This way at the top of your page you can just put:

<?php include('header.php');?>

///put in whatever html there may be


<?php 
/// put your form and processing info here
///just return if you need to prevent further processing
?>

///put in whatever html there may be
<?php include('footer.php');?>
清秋悲枫 2024-08-22 12:25:12

有很多更好的方法来做你正在做的事情。但要回答你的第一个问题,你可以创建一个名为 footer 的函数,它返回一个带有需要在页脚中显示的 html 的字符串,并调用 die(footer());但是...你为什么用 die ?难道你不能只计算错误并将它们显示在结果中的某个位置吗?你不应该那样终止脚本。

对于第二个问题。您可以使用 BenTheDesigner 所说的那样,一个 html 页面,其中的表单操作指向一个 php 脚本,该脚本进行验证,如果出现问题,则返回表单,如果没有,则转到其他地方。但在那里,您也应该删除 die() 函数并调用其他函数来重定向您。您可以使用 smarty 之类的模板系统将逻辑与 html 演示文稿分开。您可以将所有内容写在一个文件中,但尝试将整个逻辑写在文件顶部,将所有 html 写在底部。并使用 显示 php 内容,或不同 html 结果的简单条件。但不要使用 die()。我猜这只会让事情变得复杂。

there are many better ways to do what you are doing. but to answer your first question, you can create a function called footer that returns a string with html needed to be displayed in the footer and call the die(footer()); but... why do you use die ? can't you just count the errors and display them somewhere in the result ? you should not kill the script that way.

And for the second question. you can use as BenTheDesigner said, a html page with the form action pointing to a php script that validates and either returns to the form if something went wrong or go somewhere else if not. but there too, you should remove the die() function and call something else to redirect you. you can use a template system like smarty to separate your logic from your html presentation. you can write it all in a single file but try to write you're entire logic at the top of the file and all the html at the bottom. and use <?=$var?> to display php stuff, or simple conditionals for diferent html results. but don't use die(). it just complicate things I guess.

葬花如无物 2024-08-22 12:25:12

使用 die() 不是一个好的做法,因为它不会向访问您网站的用户显示友好的消息。你应该绝对确定你想要使用模具。例如,当您怀疑存在黑客攻击时,您可以在程序中使用它。相反,尝试使用 if else 结构来显示或隐藏内容。这是您可能想要实现的可能原型:

  // if there is a validation error, show it, otherwise not
  if ($error == true)
  {
    // show footer
  }

  <!-- Your footer goes normally here -->

Using die() is not a good practice because it will not show a friendly message to the user visiting your site. You should be absolute sure you want to use die. For example, you may use it in procedures when you suspect there is a hacking attempt. Instead try to use if else structure to show or hide things. Here is the possible prototype that you may want to implement:

  // if there is a validation error, show it, otherwise not
  if ($error == true)
  {
    // show footer
  }

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