How to validate form in php

前端 未结 1 1593
無奈伤痛
無奈伤痛 2021-01-28 09:15

I\'m hoping there is a solution for this, I\'m a noob in php, I couldn\'t figure it out.

I got some help from here, to validate the html form in php. It is working ok..b

1条回答
  •  耶瑟儿~
    2021-01-28 10:12

    As you are a newcomer to PHP I would give you some tips for a beginner as I can.

    First is that you are outputting an HTML code and processing PHP afterwards. This might not be a good practice (and solution to your problem is quite connected to this). What you are doing is showing a form all the time, without restrictions and checking/validating via PHP afterwards. A good practice is to never output html code before any kind of php goes (there are other issues like of header() function besides your example).

    What you would rather do is:

    
    
    ....

    Another thing I would give you as a tip is that when building static pages like that (I mean less structured, less object oriented, you know..) a good practice is to store what your are 'echoing / printing' in variables and output them nicely where applicable.

    So instead of

     declaration!!
    ?>
    

    You would go with

    Error1: Provide an username'; }
       elseif (empty($name)) { $errors .= '
    Error2: Provide a name'; } else { /*.....*/ } // Since we have our errors variable with errors (or empty one) we can check for it perhaps in HTML context ?>
    There are some errors! :(

    I hope these humble opinions will help you at least maybe to solve your problem. Good luck

0 讨论(0)
提交回复
热议问题