Check $_POST data with jquery before submitting the form

后端 未结 3 1320
面向向阳花
面向向阳花 2021-01-23 21:37

I have a form that sends the store the First Name of the user in a database. I was checking the information send by the user using regex in php. To mak

3条回答
  •  后悔当初
    2021-01-23 22:14

    First of all have in mind that the validation of users input is implementing at the server side of an application only!!! You can not validate input data at client side with JS because it can be passed very easy(either by disabling javascript or by using tools like Curl).

    However you can increase user experience like validate an input before submitting the form or inform the user that forgot to fill in an input.

    To inform the user about a not fill in input you can just use the new html 5 attribute required like above

    Username: 
    

    the required attribute will not let the user submit the form unless he had filled the associated input.

    Also you can use the maxlength attribute to address a use case like "A password must have X max letters.

    Password: 

    How to validate input at server side

    There are many techniques for this and you can find all of them here at Stackoverflow. Ι will refer the top voted post How can I prevent SQL injection in PHP? which answer exactly your question.

    Just two bullets that compact the above post that i suggest you read otherwise

    • Always escape your data

    • Use mysqli instead of mysql

    How can I submit the form right after the alert('Valid Name.'); ?

    this is very easy just use this code

        

    the above code will "send" user's input for process at action_page.php using POST method, where you can read using $_POST supergloba table like $firstname = $_POST['fistsname'] etc.

提交回复
热议问题