PHP Using client and server validation WITHOUT using third-party code

EDIT: thanks for the help. I received an email that we did not need the client side, so I abandoned this idea in favor of the actual completion of the task on time.

Before asking, yes, this is a job assignment. No. I am not looking for the code you need. I am new to practically no experience with HTML / PHP / javascript, but this is the second part of the task, so I already have part of my own code from the first part, which was very easy compared to this part. The task does not specifically say that we should use client-side validation, but I think that would be good practice.

I need someone to clearly show me how to use client and server side validation. I already have javascript validation, but I can change it since it displays a warning window for every error. I CAN’t use third-party code, such as jQuery, which everyone seems to like on the Internet, and the course I am doing does not like to actually teach us useful content, so we are all on our own.

Then the data from the form will be entered into the database via MySQL (which I do not expect to do), and, having looked at the minimum information from w3schools on this topic, I understand that I need to submit the form to the form myself.

The form itself is quite simple: it contains a name, DoB, email, zip code, etc. My current .js uses only alpha, only number, date format, email format, radio button and checkbox, and each field is checked to make sure it is not empty.

I believe this is a complete tutorial for me on how to do this. My internet search was barren, but at least I still have a week until that happens.

Any help is appreciated, but simple and clear help will be even more. I will continue to try the Internet for help until then and come back here if I find useful material for someone else with the same problem (which I am sure is 90% of my class .....)

Thanks in advance.

+3
source share
3 answers

. .

add_record.php

<?php
if(isset($_POST['name'])) {

  //get post data
  $name = trim($_POST['person_name']);
  $email = trim($_POST['email']);
  $message = trim($_POST['message']);

  //This is server-side check!
  if (strlen($name) > 10){
    echo "FAILED! You tried to submit a name which is greater than 10 chars."
  }else{
    //insert to the database here..

    //and send out a "success message or render HTML
    echo "SUCCESS!";
  }
}else {
    echo "Error! Proper parameters were not provided!";
}

a.html

<html>
<head>
<script type="text/javascript">
function checkForm(){
    //client side (JS) validation. This happens before submitting.
    var name = document.forms[0].person_name.value;
    if (name.length > 10){
        alert("Name is too long");
        return false;
    }
    //do some more checks here..
    //return true if all checks have passed, false otherwise.

    //the return value of this function is checked before submit. The form is submitted only when this function returns a true.
    return true;
}
</script>
</head>
<body>
<form action="add_record.php" method="POST" onsubmit="return checkForm()">
Name: <input type="text" name="person_name"/>
Email: <input type="text" name="email"/>
Message: <input type="text" name="message"/>
<input type="submit"/>
</form>
</body>
</html>

. - , "" . "", .

+4

php = "post", . - :

<form action="validate.php" method="post">
<!-- form fields -->
</form>

validate.php $_POST [ "var_name" ], var_name - , . , , - :

$age = $_POST["age"];
if (ctype_digit($age) && $age > 0) {
    // correct format
}

It seems that you have already passed the client-side verification. I'm not quite sure if I understood your problem correctly - let me know where exactly your problems are, and I will try to help.

0
source

All Articles