简体   繁体   中英

validate register form client side with javascript and server side with php

I'm new to PHP and Web stuff. I have a task that validate the register form at client by javascript and server by PHP. I don't understand what that means.

I made a register form, and validated all field with javascript

<form name="register" onSubmit="return validateForm()"> 
//...code display form here...
<input type="submit" name="Submit" value="Submit">
</form>

<script language="javascript">
//...validate function here...
function validateForm() {
//...
}
</script>

I put the above code in register.php. And now how do I make a validation on PHP server side? I need to create a new php file and use post method to pass data from this register.php to new ones? and after validation I post data back to register with error if there is?

I do know how to validation form in PHP but I don't know how to connect a javascript validated client form to php validate server form.

Also It is required that if there's something wrong, reload the page and display error to user, all inputted data must be persisted.

This also I don't know how to do it. Could you tell me how to do it, or some very simple code snippet? Help is really appreciated. Thanks in advance.

I believe you can just duck type the data. Obviously this is just pseudo-code:

validateForm() {

    if (typeof formData === 'duck')
        // do logic here
}

Well my initial thoughts would be to drop the onSubmit method from the form and call method="POST" or method="GET". Then I would use onBlur for each form field that will call a javascript function to validate that specific field. Finally you set the action='validate.php' or some other filename that contains the PHP validation code. Upon success redirect to where you want to go. Upon failure, refresh the page and carry over the $_POST or $_GET variables by temporarily setting it equal to a $_SESSION variable.

An all-in-one-file form handler would look something like this:

myfile.php:

<?php

$errors = array()
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['name']) && !empty($_POST['name'])) {
       $name = $_POST['name'];
    } else {
       $errors[] = "Please provide your name";
    }
}
?>

<html>

<head>
     <title>Name Form</title>
</head>

<body>
<?php if (count($errors) > 0) { ?>
<ul>
   <li><?php echo implode("</li>\n\t", $errors); ?></li>
</ul>
<?php } else { ?>
<p>Pleased to meet you, <?php echo htmlspecialchars($name) ?></p>
<? } ?>

<form method="POST" action="">
<p>Please enter your name: <input type="text" size="20" name="name" value="<?php echo htmlspecialchars($name) ?>" /></p>

<input type="submit" value="Submit" />

</form>

Of course, this violates all sorts of "best pratices" for designing a web application, but it does demonstrate every stage of presenting an form and doing data validation server-side.

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