Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- html form
- ------------------------------------------------------------------------------------------------------
- <form onsubmit="return false" class="form-horizontal" name="ads_post" id="post_form" enctype="multipart/form-data">
- <div class="form-group">
- <label class="control-label col-sm-2" >Name</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="name" placeholder="Enter Name" name="user_name">
- </div>
- <span class="name_error"></span>
- </div>
- <div class="form-group">
- <label class="control-label col-sm-2" >Image</label>
- <div class="col-sm-10">
- <table>
- <tr>
- <td><input type="file" class="file" name="file" id="input-upload-img1"></td>
- </tr>
- </table>
- </div>
- <span class="img_error"></span>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-2 col-sm-10">
- <button type="submit" class="btn btn-primary" name="ad_submit">Submit</button>
- </div>
- </div>
- </form>
- --------------------------------------------------------------------------------------------------------
- validate.js
- --------------------------------------------------------------------------------------------------------
- $(document).ready(function(){
- function verify_name(name){
- $(".name_error").hide();
- if(name == ""){
- $(".name_error").show();
- $(".name_error").html("plz provide your name");
- return false;
- }
- else{
- $(".name_error").show();
- $(".name_error").html("validating......");
- $.ajax({
- url:"classes/process.php",
- method:"post",
- data:{check_name:1,name:name},
- success:function(data){
- $(".name_error").show();
- if(data == "invalid_name"){
- $(".name_error").html("Plz provide valid name");
- return false;
- }
- else if(data == "good"){$(".name_error").html("ok");
- }
- },error: function(req, status, error) {
- // alert('Error: ' + req.status);
- console.log(error);
- console.log();
- }
- })
- }
- }
- function verify_img(img){
- $(".img_error").hide();
- if(img == ""){
- $(".img_error").show();
- $(".img_error").html("plz Choose an image");
- return false;
- }
- else{
- $(".img_error").show();
- $(".img_error").html("validating....");
- $.ajax({
- url:"classes/process.php",
- type: "POST",
- data: {check_img:1,img:img},
- success: function(data)
- {
- $(".img_error").show();
- if (data=="size_large"){
- $(".img_error").html("image should be 1 mb");
- }
- else if(data=="invalid_format"){
- $(".img_error").html("only jpg,jpeg,png formates are allowed");
- }
- else{
- $(".img_error").html("ok");
- }
- },error: function(req, status, error) {
- // alert('Error: ' + req.status);
- console.log(error);
- console.log();
- }
- })
- }
- }
- $("#input-upload-img1").focusout(function(){
- var img=$("#input-upload-img1").val();
- verify_img(img);
- })
- $("#name").focusout(function(){
- var name=$("#name").val();
- verify_name(name);
- })
- $("#post_form").on("submit",function(){
- var name = $("#name").val();
- var image = $("#input-upload-img1").val();
- if(name == ""){$(".name_error").show();
- $(".name_error").html("plz provide your name");
- return false;
- }
- else if(image == ""){$(".img_error").show();
- $(".img_error").html("plz select a image");
- return false;
- }
- else{
- $.ajax({
- url:"classes/process.php",
- method:"post",
- data:$("#post_form").serialize(),
- success:function(data){
- //if(data=="ok"){
- //window.location.href="index.php";
- alert(data);
- //}
- },error: function(req, status, error) {
- // alert('Error: ' + req.status);
- console.log(error);
- console.log();
- }
- })
- }
- })
- })
- ---------------------------------------------------------------------------------------------------
- process.php
- ---------------------------------------------------------------------------------------------------
- <?php
- include "dbcon.php";
- class DataOperation extends database{
- public function insert_image($table,$fields){
- $name=$_FILES['file']['name'];
- //$type=$_FILES['file']['type'];
- //$extension=strtolower(substr($name,strpos($name,'.')+1));
- //$size=$_FILES['file']['size'];
- //$max_size=1000000;
- $temp_name=$_FILES['file']['tmp_name'];
- $location="../upload/";
- if( move_uploaded_file($temp_name,$location.$name)){
- $sql="";
- $sql.="insert into ".$table ;
- $sql.="(".implode(",",array_keys($fields)).") values ";
- $sql.="(' ".implode(" ',' ",array_values($fields))." ' )";
- $query=mysqli_query($this->con,$sql);
- $img_id = mysqli_insert_id($this->con);
- if ($query){
- return $img_id;
- }
- }
- else
- {
- echo"something went wrong";
- }
- }
- public function verify_name($name){
- $regexp = "/^[A-Za-z_]{2,5}$/";
- //if(!preg_match($regexp,$name)){
- //return "invalid_name";
- return (!preg_match($regexp,$name)) ? "invalid_name" : 'good';
- }
- public function verify_img($file){
- $extension=strtolower(substr($file,strpos($file,'.')+1));
- $size=$_FILES['file']['size'];
- $max_size=1000000;
- $type=$_FILES['file']['type'];
- if($extension=='jpg'||$extension=='jpeg'||$extension=='png'&& $type=='image/jpeg'&& $type=='image/jpg'&& $type=='image/png')
- {
- if($size<=$max_size)
- {
- return "ok";
- }
- else{
- return "size_large";
- }
- }
- else {
- return "invalid_format";
- }
- }
- }
- $obj = new DataOperation;
- if(isset($_POST["check_name"])){
- $name=$_POST["name"];
- echo $data=$obj->verify_name($name);
- exit();
- }
- if(isset($_FILES['file']['check_img'])){
- $file=$_FILES['file']['img'];
- echo $data=$obj->verify_img($file);
- exit();
- }
- if(isset($_POST["user_name"])){
- $data=$obj->verify_name($_POST["user_name"]);
- if($data == "invalid_name"){
- echo "invalid name";
- exit();
- }else {
- $name= $_POST["user_name"];
- }
- $data=$obj->verify_img($_FILES['file']['name']);
- if($data == "invalid_extension"){
- echo "Only jpg,jpeg,and png files allowed";
- exit();
- }else {
- $file=$_FILES['file']['name'];
- }
- $ad_image = array(
- "img1" => $file,
- );
- $img_id = $obj->insert_image("img_upload",$ad_image);
- //user information
- $user_info = array(
- "name_user" => $name,
- );
- $id = $obj->insert_record("user",$user_info);
- $myarray=array(
- "user_id"=>$id,
- "img_id"=>$img_id,
- );
- if ($obj->insert_record("classified",$myarray)) {
- //header("location:../index.php?msg=Successfully inserted");
- echo "success";
- exit();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment