RAHULINDIA122

undefined index error

Mar 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.96 KB | None | 0 0
  1. html form
  2. ------------------------------------------------------------------------------------------------------
  3. <form onsubmit="return false"  class="form-horizontal" name="ads_post" id="post_form" enctype="multipart/form-data">
  4. <div class="form-group">
  5.       <label class="control-label col-sm-2" >Name</label>
  6.       <div class="col-sm-10">
  7.        <input type="text" class="form-control" id="name"  placeholder="Enter Name" name="user_name">
  8.        
  9.       </div>
  10.       <span class="name_error"></span>
  11.     </div>
  12.  
  13. <div class="form-group">
  14.       <label class="control-label col-sm-2" >Image</label>
  15.       <div class="col-sm-10">
  16.       <table>
  17.         <tr>
  18.         <td><input type="file"  class="file" name="file" id="input-upload-img1"></td>
  19.        
  20.         </tr>
  21.      </table>
  22.       </div>
  23.       <span class="img_error"></span>
  24.     </div>
  25. <div class="form-group">        
  26.                       <div class="col-sm-offset-2 col-sm-10">
  27.                         <button type="submit" class="btn btn-primary" name="ad_submit">Submit</button>
  28.                       </div>
  29.                     </div>
  30. </form>
  31.  
  32. --------------------------------------------------------------------------------------------------------
  33. validate.js
  34. --------------------------------------------------------------------------------------------------------
  35. $(document).ready(function(){
  36.  
  37.  
  38.      function verify_name(name){
  39.     $(".name_error").hide();
  40.     if(name == ""){
  41.     $(".name_error").show();
  42.     $(".name_error").html("plz provide your name");
  43.     return false;
  44.     }
  45.    
  46.    else{   
  47.    
  48.     $(".name_error").show();
  49.     $(".name_error").html("validating......");
  50.     $.ajax({
  51.     url:"classes/process.php",
  52.     method:"post",
  53.     data:{check_name:1,name:name},
  54.     success:function(data){
  55.  
  56.     $(".name_error").show();
  57.     if(data == "invalid_name"){
  58.     $(".name_error").html("Plz provide valid name");
  59.     return false;
  60.  
  61.     }
  62.     else if(data == "good"){$(".name_error").html("ok");
  63.    
  64.    
  65.     }
  66.     },error: function(req, status, error) {
  67.           // alert('Error: ' + req.status);
  68.             console.log(error);
  69.             console.log();
  70.             }
  71.    
  72.     })
  73.    }
  74.    
  75. }
  76.  
  77.  
  78.  
  79.  
  80.    function verify_img(img){
  81.    
  82.     $(".img_error").hide();
  83.     if(img == ""){
  84.     $(".img_error").show();
  85.     $(".img_error").html("plz Choose an image");
  86.     return false;
  87.     }
  88.     else{
  89.     $(".img_error").show();
  90.     $(".img_error").html("validating....");
  91.     $.ajax({
  92.    
  93.             url:"classes/process.php",    
  94.             type: "POST",            
  95.             data: {check_img:1,img:img},
  96.             success: function(data)  
  97.             {
  98.            
  99.             $(".img_error").show();
  100.     if (data=="size_large"){
  101.    
  102.     $(".img_error").html("image should be 1 mb");
  103.    
  104.     }
  105.     else if(data=="invalid_format"){
  106.    
  107.     $(".img_error").html("only jpg,jpeg,png formates are allowed");
  108.    
  109.     }
  110.    
  111.     else{
  112.     $(".img_error").html("ok");
  113.    
  114.     }
  115.    
  116.    
  117.    
  118.     },error: function(req, status, error) {
  119.           // alert('Error: ' + req.status);
  120.             console.log(error);
  121.             console.log();
  122.             }
  123.    
  124.      })
  125.     }
  126.    
  127. }
  128.  
  129.  
  130. $("#input-upload-img1").focusout(function(){
  131.         var img=$("#input-upload-img1").val();
  132.         verify_img(img);
  133.  
  134.           })
  135.          
  136.          
  137.          
  138. $("#name").focusout(function(){
  139.     var name=$("#name").val();
  140.     verify_name(name);
  141.  
  142.     })
  143.          
  144.  
  145.     $("#post_form").on("submit",function(){
  146.    
  147.     var name = $("#name").val();
  148.    
  149.     var image = $("#input-upload-img1").val();
  150.    
  151.     if(name == ""){$(".name_error").show();
  152.     $(".name_error").html("plz provide your name");
  153.     return false;
  154.    
  155.     }
  156.    
  157.  
  158.     else if(image == ""){$(".img_error").show();
  159.     $(".img_error").html("plz select a image");
  160.     return false;
  161.    
  162.     }
  163.  
  164.     else{
  165.      
  166.     $.ajax({
  167.           url:"classes/process.php",
  168.           method:"post",
  169.           data:$("#post_form").serialize(),
  170.           success:function(data){
  171.           //if(data=="ok"){
  172.           //window.location.href="index.php";
  173.           alert(data);
  174.           //}
  175.           },error: function(req, status, error) {
  176.           // alert('Error: ' + req.status);
  177.             console.log(error);
  178.             console.log();
  179.             }
  180.          
  181.          })
  182.    
  183.    
  184.     }
  185.       })
  186.  
  187. })
  188.  
  189.  
  190. ---------------------------------------------------------------------------------------------------
  191. process.php
  192. ---------------------------------------------------------------------------------------------------
  193.  
  194. <?php
  195.  
  196. include "dbcon.php";
  197.  
  198. class DataOperation extends database{
  199.  
  200. public function insert_image($table,$fields){
  201.      
  202.                      $name=$_FILES['file']['name'];
  203.                      //$type=$_FILES['file']['type'];
  204.                      //$extension=strtolower(substr($name,strpos($name,'.')+1));
  205.                      //$size=$_FILES['file']['size'];
  206.                      //$max_size=1000000;
  207.                      $temp_name=$_FILES['file']['tmp_name'];
  208.                      $location="../upload/";
  209.                     if( move_uploaded_file($temp_name,$location.$name)){
  210.                      $sql="";
  211.                      $sql.="insert into ".$table ;
  212.                      $sql.="(".implode(",",array_keys($fields)).") values ";
  213.                      $sql.="(' ".implode(" ',' ",array_values($fields))." ' )";
  214.                      $query=mysqli_query($this->con,$sql);
  215.                      $img_id = mysqli_insert_id($this->con);
  216.                      if ($query){
  217.                                   return $img_id;
  218.                              }
  219.                     }
  220.                     else
  221.                     {
  222.                       echo"something went wrong";
  223.                     }
  224.     }  
  225.    
  226.    
  227.     public function verify_name($name){
  228.      $regexp = "/^[A-Za-z_]{2,5}$/";
  229.      //if(!preg_match($regexp,$name)){
  230.      //return "invalid_name";
  231.      return (!preg_match($regexp,$name)) ? "invalid_name" : 'good';
  232.      
  233.      
  234.     }
  235.    
  236.     public function verify_img($file){
  237.    
  238.     $extension=strtolower(substr($file,strpos($file,'.')+1));
  239.     $size=$_FILES['file']['size'];
  240.     $max_size=1000000;
  241.     $type=$_FILES['file']['type'];
  242.     if($extension=='jpg'||$extension=='jpeg'||$extension=='png'&& $type=='image/jpeg'&& $type=='image/jpg'&& $type=='image/png')
  243.              {  
  244.                  if($size<=$max_size)
  245.                  {
  246.                     return "ok";
  247.                  }
  248.                  
  249.                  else{
  250.                  
  251.                   return "size_large";
  252.                  }
  253.              }
  254.              
  255.     else {
  256.    
  257.       return "invalid_format";
  258.     }
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265. }
  266.  
  267.  $obj = new DataOperation;
  268.  if(isset($_POST["check_name"])){
  269.      $name=$_POST["name"];
  270.      echo $data=$obj->verify_name($name);
  271.      exit();
  272.  }
  273.  
  274.  if(isset($_FILES['file']['check_img'])){
  275.      $file=$_FILES['file']['img'];
  276.      echo $data=$obj->verify_img($file);
  277.      exit();
  278.  }
  279.  
  280.  if(isset($_POST["user_name"])){
  281.      
  282.      
  283.      
  284.       $data=$obj->verify_name($_POST["user_name"]);
  285.          if($data == "invalid_name"){
  286.              echo "invalid name";
  287.              exit();
  288.             }else {
  289.             $name= $_POST["user_name"];
  290.         }
  291.        
  292.        
  293.         $data=$obj->verify_img($_FILES['file']['name']);
  294.          if($data == "invalid_extension"){
  295.              echo "Only jpg,jpeg,and png files allowed";
  296.              exit();
  297.             }else {
  298.             $file=$_FILES['file']['name'];
  299.         }
  300.        
  301.        
  302.         $ad_image = array(
  303.                 "img1" => $file,
  304.                
  305.                 );
  306.         $img_id = $obj->insert_image("img_upload",$ad_image);
  307.        
  308.         //user information
  309.             $user_info = array(
  310.                 "name_user" => $name,
  311.                
  312.                 );
  313.                 $id = $obj->insert_record("user",$user_info);
  314.                
  315.                 $myarray=array(
  316.                
  317.                 "user_id"=>$id,
  318.                 "img_id"=>$img_id,
  319.                 );
  320.                
  321.                 if ($obj->insert_record("classified",$myarray)) {
  322.                 //header("location:../index.php?msg=Successfully inserted");
  323.                 echo "success";
  324.                 exit();
  325.             }
  326.        
  327.         }
  328.  
  329.  ?>
Add Comment
Please, Sign In to add comment