down2323 2009-12-18 08:50
浏览 34
已采纳

PHP和HTML集成

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!

  • 写回答

5条回答 默认 最新

  • douhuan6065 2009-12-18 09:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?