Validating information in HTML - Code Positioning

后端 未结 1 1903
我寻月下人不归
我寻月下人不归 2021-01-06 06:53

I am having trouble getting my code validation to work. I have written validation for a name, surname and email address, however, I don\'t know where to insert a command for

1条回答
  •  有刺的猬
    2021-01-06 07:42

    You don't put it in the HTML unless you are sending the page to itself, in which case it is usually best to put the PHP at the top of the page. It would need to be named as a .php page not .html then. That way if, for example, you wanted to make the values of the form stay as what was submitted you could echo them after they are cleaned up by setting the textbox value

     value=""
    

    for example. If you are submitting to insert_data.php all the PHP just lives on that page at the top. You seem to have too many

    - you can only submit once. Best to put your cleanup code at the top of insert_data.php and submit to that.

    If it is on the same page you would need to wrap it in

        if( isset($_POST["first_name"])){
         // do the cleanup
        }
    

    or you will get messages for the empty inputs which have not had the chance to be submitted as the page loads. And do the same for the email address which you definitely don't want to have blank if you are subsequently going to use it for a mail form's Reply-To: address (the From: should always be an address on your server or you will be back here posting wondering why it doesn't work!)

    You could include the validation script but that is possibly a bit risky and for the length of the code there is probably little advantage in having it as an include. Risks of including unsafely: http://www.webhostingtalk.com/showthread.php?t=199419

    You would then use - assuming your includes are in a folder called inc/

    include "inc/validate_data.php";
    

    at the top of your insert_data.php page - without the brackets shown in that article - it is a declaration, not a function.

    Another good article on includes:

    http://green-beast.com/blog/?p=144

    If you were looping out posts, for example, the code to do that would be somewhere among the HTML inside the div where you wanted them to appear.

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