简体   繁体   中英

Form input fields validation via jQuery and PHP

I want to validate my form input fields on button click in jQuery. But I don't want to use client side validation. How can I add PHP server-side validation on button click?

I am using modal box for form.

I know it is a beginner's level question, but currently I am unable to figure it out because of my low expertise.

Yes you can validate using PHP and it's not a big deal. I'll provide a simple example here to pass the form data to PHP for validation.

<?php
if( isset($_POST['submitForm']) )
{
    $userName = $_POST['userName'];
    $userAge = $_POST['userAge'];
    
    if ($userAge > 21)
    {
      echo "Hey " + userName;
    }
}
?>
<html>
<body>
<form method="POST" action=""> 
    Name: <input type="text" name="userName">
    Age: <input type="number" name="userAge">
    <input type="submit" name="submitForm" value="Validate"> 
</form>
</body>
</html> 

If you wish to pass the data to another page to validate, just give the file name in action attribute, like action="Your_PHP_File.php" and validate separately. Just a tip : If you wish to redirect on success (heavily used):

header("Location: Your_URL_Here");

Anyways, this is not a better user experience. You must use client side validation using JavaScript/any JavaScript framework, and use language like PHP only to communicate with the server, such as storing the data in the database, retrieving data from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM