Tagged: jQuery and Ajax

PHP Laravel Inline CRUD Using jQuery and AJAX 0

PHP Laravel Inline CRUD Using jQuery and AJAX

Today, We want to share with you PHP Laravel Inline CRUD Using jQuery and AJAX.In this post we will show you Create Inline CRUD Using PHP, Laravel, jQuery and AJAX, hear for HTML5 Inline Editing with Laravel 5.7, MYSQL & jQuery Ajax we will give you demo and example for implement.In this post, we will learn about Inline Table Editing using jQuery Ajax Laravel and MySQL with an example.

PHP Laravel Inline CRUD Using jQuery and AJAX

There are the Following The simple About PHP Laravel Inline CRUD Using jQuery and AJAX Full Information With Example and source code.

As I will cover this Post with live Working example to develop jQuery AJAX Inline CRUD with PHP Laravel, so the jQuery AJAX Inline CRUD using PHP Laravel MySQL for this example is following below.

Phase 1 : Install Laravel Application

Simple HTML5 Inline Editing with Laravel 5.7, MYSQL & jQuery Ajax

Laravel 5.7 version application using bellow composer command

composer create-project --prefer-dist laravel/laravel system_project

Phase 2: MySQL .env files Database Configuration

database configuration in Laravel env files

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=MyDBNAme
DB_USERNAME=DATABASE_USERNAME
DB_PASSWORD=MY_DATABASE_PASSWORD

Phase 3: Make a productlist Table and Model

make a Laravel migration for productlist table using Laravel 5.7

Table Name : productlist

php artisan make:migration create_productlist_table

database/migrations

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagslistTable extends Migration
{
    public function up()
    {
        Schema::create('productlist', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('productlist');
    }
}

run simple Laravel commands migration

Make a Laravel 5.7 migration and Model

php artisan migrate

php artisan make:model ProductList

app/ProductList.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductList extends Model
{
    public $table = "productlist";
    public $fillable = ['name'];
}

Phase 4: Define Laravel Routes

routes/web.php

Route::get("multipleInptFrm","[email protected]");
Route::post("multipleInptFrm","[email protected]");

Phase 5: Create ProductController

create new controller as ProductController

php artisan make:controller ProductController

app/Http/Controllers/ProductController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\ProductList;
use Validator;

class ProductController extends Controller
{
    public function moreAddData()
    {
        return view("moreAddData");
    }
    public function moreAddDataPost(Request $request)
    {
        $rules = [];
        foreach($request->input('name') as $key => $value) {
            $rules["name.{$key}"] = 'required';
        }

        $validator = Validator::make($request->all(), $rules);
        if ($validator->passes()) {

            foreach($request->input('name') as $key => $value) {
                ProductList::create(['name'=>$value]);
            }

            return response()->json(['success'=>'done']);
        }
        return response()->json(['error'=>$validator->errors()->all()]);
    }
}

Phase 6: Make a Laravel Blade File

resources/views/moreAddData.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Inline Table Editing using jQuery Ajax Laravel and MySQL - Pakainfo.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  

 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>


<div class="container">
    <h2 align="center">Laravel - HTML5 Inline Editing with Laravel 5.7, MYSQL & jQuery Ajax</h2>  
    <div class="form-group">
         <form name="product_name_add" id="product_name_add">  


            <div class="alert alert-danger message-errors-display" style="display:none">
            <ul></ul>
            </div>


            <div class="alert alert-success message-display" style="display:none">
            <ul></ul>
            </div>


            <div class="table-responsive">  
                <table class="table table-bordered" id="dynamic_field">  
                    <tr>  
                        <td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td>  
                        <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>  
                    </tr>  
                </table>  
                <input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />  
            </div>


         </form>  
    </div> 
</div>


<script type="text/javascript">
    $(document).ready(function(){      
      var postURL = "<?php echo url('multipleInptFrm'); ?>";
      var i=1;  


      $('#add').click(function(){  
           i++;  
           $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');  
      });  


      $(document).on('click', '.btn_remove', function(){  
           var button_id = $(this).attr("id");   
           $('#row'+button_id+'').remove();  
      });  


      $.ajaxSetup({
          headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });


      $('#submit').click(function(){            
           $.ajax({  
                url:postURL,  
                method:"POST",  
                data:$('#product_name_add').serialize(),
                type:'json',
                success:function(data)  
                {
                    if(data.error){
                        displayMessageError(data.error);
                    }else{
                        i=1;
                        $('.dynamic-added').remove();
                        $('#product_name_add')[0].reset();
                        $(".message-display").find("ul").html('');
                        $(".message-display").css('display','block');
                        $(".message-errors-display").css('display','none');
                        $(".message-display").find("ul").append('<li>Record Inserted Successfully.</li>');
                    }
                }  
           });  
      });  


      function displayMessageError (msg) {
         $(".message-errors-display").find("ul").html('');
         $(".message-errors-display").css('display','block');
         $(".message-display").css('display','none');
         $.each( msg, function( key, value ) {
            $(".message-errors-display").find("ul").append('<li>'+value+'</li>');
         });
      }
    });  
</script>
</body>
</html>

run command

php artisan serve

// open bellow URL
http://localhost:8000/multipleInptFrm

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Laravel Inline CRUD Using jQuery and AJAX.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

PHP Ajax Image Upload Resize Crop using jQuery 0

PHP Ajax Image Upload Resize Crop using jQuery

Today, We want to share with you PHP Ajax Image Upload Resize Crop using jQuery.In this post we will show you ajax image upload and crop with jquery and php, hear for Image Upload,Crop and Resize Using PHP, jQuery and Ajax we will give you demo and example for implement.In this post, we will learn about Live Image Upload, Crop and Resize using jQuery and PHP MySQL with an example.

PHP Ajax Image Upload Resize Crop using jQuery

There are the Following The simple About PHP Ajax Image Upload Resize Crop using jQuery Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP laravel crop image before upload, so the php image resize on upload for this example is following below.

Step 1: Javascript With Include External CDN

Javascript Source Code

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() 
{ 

$('body').on('change','#memberprofile', function()
 {
var A=$("#memberImgStatus");
var B=$("#memberloadbtn");

$("#memberFrm").ajaxForm({target: '#flimgpreview', 
beforeSubmit:function(){
A.show();
B.hide();
}, 
success:function(){
A.hide();
B.show();
}, 
error:function(){
A.hide();
B.show();
} }).submit();
});

}); 
</script>

Step 2 : HTML Interface

index.php

<?php
include('db.php');
session_start();
$member_session_profile='1'; // User login session value
?>
<div id='flimgpreview'>
</div>
<form id="memberFrm" method="post" enctype="multipart/form-data" action='memberprofileimg.php'>
Upload image: 
<div id='memberImgStatus' style='display:none'><img src="loader.gif" alt="Profile Uploading...."/></div>
<div id='memberloadbtn'>
<input type="file" name="memberprofile" id="memberprofile" />

</div>
</form>

Step 3 : CREATE A Table

Users MySQL Table

CREATE TABLE `members` (
`uid` int(11) AUTO_INCREMENT PRIMARY KEY,
`username` varchar(255) UNIQUE KEY,
`password` varchar(100),
`email` varchar(255) UNIQUE KEY,
`profile_image` varchar(200)
)

Step 4: jquery Ajax Call To server side

memberprofileimg.php

<?php
include('db.php');
session_start();
$member_session_profile='1'; 
$path = "uploads/";

$fileimg_validtype = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
include_once 'includes/getExtension.php';
$fileorimgnm = $_FILES['memberprofile']['name'];
$size = $_FILES['memberprofile']['size'];
if(strlen($fileorimgnm))
{
$fltypeimgext = strtolower(getExtension($fileorimgnm));
if(in_array($fltypeimgext,$fileimg_validtype))
{
if($size<(1024*1024)) 
{
$orignal_upload = time().$member_session_profile.".".$fltypeimgext;
$livefileUpload = $_FILES['memberprofile']['tmp_name'];

//Re-sizing image. 
include 'includes/resizewithCompressImg.php';
$widthArray = array(200,100,50); 
foreach($widthArray as $changewidthnew)
{
$filename=resizewithCompressImg($fltypeimgext,$livefileUpload,$path,$orignal_upload,$changewidthnew);
echo "<img src='".$filename."' class='img'/>";
}

//Original Image
if(move_uploaded_file($livefileUpload, $path.$orignal_upload))
{
//Insert upload image files names into user_uploads table
mysqli_query($db,"UPDATE members SET profile_image='$orignal_upload' WHERE uid='$member_session_profile';");
echo "<img src='uploads/".$orignal_upload."' class='flimgpreview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB"; 
}
else
echo "Invalid file format.."; 
}
else
echo "Please select image..!";
exit;
}
?>

Step 5 : Serever Side Resize with Compress an Image

resizewithCompressImg.php

Simple Your Images Re-sizing image into All the different some more pixel dimensions.

<?php
function resizewithCompressImg($fltypeimgext,$livefileUpload,$path,$orignal_upload,$changewidthnew)
{

if($fltypeimgext=="jpg" || $fltypeimgext=="jpeg" )
{
$src = imagecreatefromjpeg($livefileUpload);
}
else if($fltypeimgext=="png")
{
$src = imagecreatefrompng($livefileUpload);
}
else if($fltypeimgext=="gif")
{
$src = imagecreatefromgif($livefileUpload);
}
else
{
$src = imagecreatefrombmp($livefileUpload);
}

list($width,$height)=getimagesize($livefileUpload);
$changeheightnew=($height/$width)*$changewidthnew;
$tmp=imagecreatetruecolor($changewidthnew,$changeheightnew);
imagecopyresampled($tmp,$src,0,0,0,0,$changewidthnew,$changeheightnew,$width,$height);
$filename = $path.$changewidthnew.'_'.$orignal_upload;
imagejpeg($tmp,$filename,100);
imagedestroy($tmp);
return $filename;
}
?>

getExtension.php

function getExtension($captionstr)
{
$i = strrpos($captionstr,".");
if (!$i)
{
return "";
}
$l = strlen($captionstr) - $i;
$flext = substr($captionstr,$i+1,$l);
return $flext;
}

Step 6 : Database Config File

db.php

Simple Main Global file Database config file.

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'mypass');
define('DB_DATABASE', 'YOUR_DATABASE_NAME');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Ajax Image Upload Resize Crop using jQuery.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.