Question

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

$name=trim($_POST["name"]);


if($name == "" ) {
$error= "error : You did not enter a name.";
$code= "1" ;
}

?>

My html form like this not exactly. in this part m not getting what is the mean of <?php if(isset($code) && $code == 1)

<html>
<?php if (isset($error)) { echo "<p class='message'>" .$error. "</p>" ;} ?>
<style type="text/css" >
.error{border:1px solid red; }
.message{color: red; font-weight:bold; 
}
</style>
<tr>
<td width= "82" >Name: </td>
<td width= "238" ><input name= "name" type= "text" <?php if(isset($code) && $code == 1){echo "class=error" ;} ?> ></td>
</tr>
<tr>

And also need a help: when javascript is disable on client side that time is it okey to use both js and server side validation (this script) in one page ..??? HOW ??... Suggestions always welcome ...

Was it helpful?

Solution

I'm not 100% understanding your question, but will do my best to answer it. I believe you are referring to how to validate values on form submit through PHP.

You can do this the old way without jQuery using a standard POST or GET

form.html

<html>
<head>
   <title>Form Page</title>
</head>
<body>
  <form action="validation.php" method="POST">
     <input type="text" name="email" />
  </form>
</body>
</html>

validation.php

 <?php

  $email = $_POST['email'];

  //any validation here (this validates if email has @ sign)
  $is_valid = strpos($email, "@");
  if($is_valid === false){
         //@ sign is not in string thus email is invalid, send message
        echo "Error";
   } else{
       //is valid
       echo "Valid";
   }

   ?>

The $_POST is used to receive variables that were posted. The $_GET is used to receive variables that are sent GET method. The GET method sends data through the url as a query string. Consequently, it should not be used for sensitive information as this poses a security risk.

You can also use the more recent jQuery method using AJAX so it doesn't reload the entire page to validate.

form_jquery.html

<html>
<head>
   <title>Form Page</title>
   <script src="http://code.jquery.com/jquery-1.9.0.min.js" type="text/javascript"></script>
   <script type="text/javascript">
       $(document).ready(function(){
           $("form").submit(function(){
                   //do javascript validation here

                   //do php validation here
                   $.post("validation.php", {email: $("#email").val()}, function(data){
                          alert(data); //validation msg
                   }, "text");
            });
       });
   </script>
</head>
<body>
  <form>
     <input id="email" type="text" name="email" />
  </form>
</body>
</html>

If JavaScript is disabled on the client, you cannot use JavaScript client validation or jQuery (jQuery is a JavaScript framework). You can only use server-side.

In response to this code:

if(isset($code) && $code == 1) 

The isset checks to see if $code has a value (i.e. not null) and the $code == 1 checks to evaluate if $code is equal to 1. If that condition is met, it assigns the CSS class "error" to the input text box giving it a red border.

Applying your example, jQuery would be best suited for you.

name_jquery.html

<html>
<head>
   <title>Form Page</title>
   <script src="http://code.jquery.com/jquery-1.9.0.min.js" type="text/javascript"></script>
   <style type="text/css">
      .error{border:1px solid red; }
   </style>
   <script type="text/javascript">
        $(document).ready(function(){
           $("form").submit(function(){
                   //do javascript validation here

                   //do php validation here
                   $.post("validation.php", {email: $("#name").val()}, function(data){
                         if(data.error == "1"){ //1 means error
                              $("#name").addClass("error");
                              $("body").append("<p id=\"error\">"+data.message+"</p>");
                         } else{
                              alert('your form is valid');
                              //clear css error class in case was invalid before
                              $("#name").removeClass("error");
                              $("#error").remove();
                         }
                   }, "json");
            });
       });
   </script>
</head>
<body>
  <form>
     <input id="name" type="text" name="name" />
  </form>
</body>
</html>

validation.php

<?php

  $name=trim($_POST["name"]);

  if(empty($name)) {
      $error= "error : You did not enter a name.";
      $code= "1" ;
  } else{
      $code = "0";
  }

  echo json_encode("error"=> $code, "message"=>$error); 

   ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top