0
<?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 ...

Harshad Patel
  • 9
  • 1
  • 1
  • 5
  • you need to be more specific.. – Majid Laissi Jan 17 '13 at 22:40
  • 1
    you can use validation on client and server side, but the fact you're asking "how in one page" suggests you're jumping in at the deep end, with Zend by the looks of it. It might be a good idea for you to learn HTML and some Javascript before moving on to server side code. – Popnoodles Jan 17 '13 at 22:40

1 Answers1

1

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); 

   ?>
jth_92
  • 1,120
  • 9
  • 23
  • That's no way to validate an emailaddress: [How to validate an email address in PHP](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863). – PeeHaa Jan 17 '13 at 23:02
  • 1
    I was not trying to validate an email address. The goal was to provide an example for how server side validation works. – jth_92 Jan 17 '13 at 23:07
  • @ jth_92 , Sir This is all i want .. u made my day .. its easy and with complete description ... BOSS i have to learn kind of stuff you have.. – Harshad Patel Jan 17 '13 at 23:12