Tagged: laravel 5.7 ajax get request

jQuery Ajax Calling Laravel Example From Scratch 0

jQuery Ajax Calling Laravel Example From Scratch

Today, We want to share with you jQuery Ajax Calling Laravel Example From Scratch.In this post we will show you Laravel Ajax Call to a function in controller, hear for JQuery Ajax Post Request Example in Laravel 5.7 we will give you demo and example for implement.In this post, we will learn about Laravel 5.7 Jquery Ajax Request Example From Scratch with an example.

jQuery Ajax Calling Laravel Example From Scratch

There are the Following The simple About jQuery Ajax Calling Laravel Example From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop AJAX call to a Laravel controller function / method, so the laravel 5.7 ajax post to controller for this example is following below.

Laravel pass data to a controller using Ajax

 $.ajax({
    method: 'POST',
    url: '/httpajaxRequest',
    dataType: 'json'
    data: {
        productID: 0124570,
        productName: 'Bonding Gift'
     },
})

Add Laravel Blade File:

resources/views/httpajaxRequest.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Pakainfo.com - Laravel 5.7 jQuery Ajax  Request example</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>
<body>
    <div class="container">
        <h1>Laravel 5.7 jQuery Ajax  Request example</h1>
        <b>Laravel Ajax Call to a function in controller</b>
        <form >
            <div class="form-group">
                <label>Member Name:</label>
                <input type="text" name="name" class="form-control" placeholder="Member Name" required="">
            </div>
            <div class="form-group">
                <label>Member Password:</label>
                <input type="password" name="password" class="form-control" placeholder="Member Password" required="">
            </div>
            <div class="form-group">
                <strong>Member Email:</strong>
                <input type="email" name="email" class="form-control" placeholder="Member Email" required="">
            </div>
            <div class="form-group">
                <button class="btn btn-success btn-data-post">Submit</button>
            </div>
        </form>
    </div>
</body>
<script type="text/javascript">
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $(".btn-data-post").click(function(e){
        e.preventDefault();
        var name = $("input[name=name]").val();
        var password = $("input[name=password]").val();
        var email = $("input[name=email]").val();
        $.ajax({
           type:'POST',
           url:'/httpajaxRequest',
           data:{name:name, password:password, email:email},
           success:function(data){
              alert(data.success);
           }
        });
    });
</script>
</html>

Add Controller methods:

Creating an AJAX call to a Laravel controller function / method

app/Http/Controllers/ProductController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
    public function httpajaxRequest()
    {
        return view('httpajaxRequest');
    }
    public function ajaxRequestPost()
    {
        $input = request()->all();
        return response()->json(['success'=>'Got Simple jQuery Ajax Http Request.']);
    }
}

Add Routes for Ajax Request:

routes/web.php

JQuery Ajax Post Request Example in Laravel 5.7 Routing

Laravel 5.7 jQuery Ajax GET and POST examples

Route::get('httpajaxRequest', '[email protected]');
Route::post('httpajaxRequest', '[email protected]');

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 jQuery Ajax Calling Laravel Example From Scratch.
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 Laravel AJAX Request GET and POST Example 0

PHP Laravel AJAX Request GET and POST Example

Today, We want to share with you PHP Laravel AJAX Request GET and POST Example.In this post we will show you Laravel 5.7 Jquery Ajax Request Example From Scratch, hear for Ajax requests using jQuery and Laravel 5.7 we will give you demo and example for implement.In this post, we will learn about jQuery Ajax Call to Laravel Script with JSON Return with an example.

PHP Laravel AJAX Request GET and POST Example

There are the Following The simple About PHP Laravel AJAX Request GET and POST Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Simple Ajax request example with JQuery and Laravel, so the Laravel 5.7 Jquery Ajax Request Example From Scratch is following below.

Example 1 : Laravel Requests Ajax GET and POST

resources/views/product/create.php

    $(document).ready(function()
    {

        $( ".btn-product-save" ).click(function() {

            if($('#product_info').val() == '')
            {
                $(".product_infoerror").text("Enter Describe your Products Information here in details");
            }
            else{
                $("#loading").show();

                var productsobj = {};
                productsobj.user_id  = {{Auth::user()->id}};


                productsobj.product_info =$('#product_info').val();
                productsobj.productable_type = $('#productable_type').val();
                productsobj.product_id = $('#product_id').val();

                productsobj.product_team_display = 1;

                productsobj._token = '{{csrf_token()}}';


                $.ajax({
                    type:"POST",
                    dataType:"json",
                    url:"{{ url('products') }}",
                    cache: false,
                    data: productsobj,
                    success: function (data) {
                        //  console.log(data);
                    },
                    error: function (data) {
                        console.log('Error:', data);
                        var obj = {
                        };
                    }
                });
            }
        });
    });

routes/web.php

	Route::post('products', '[email protected]');

app/http/controllers/ProductController

	public function products(Request $request)
    {

          DB::table('productslist')->insert([
            'product_info' => $request['product_info'],
            'product_id' => $request['product_id'],
            'user_id' => $request['user_id'],
            'product_team_display' => $request['product_team_display'],
            'productable_type' => $request['productable_type'],
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);

        $product_id = DB::getPdo()->lastInsertId();

        $affected = DB::table('products')->where('id', '=', $request['product_id'])->update(array('productable_type' => 1));

        }

        $productslist = DB::table('productslist')
            ->join('users', 'productslist.user_id', '=', 'users.id')
            ->select('productslist.*', 'users.name AS uname')
            ->where('productslist.id', '=', $product_id)->get();
        return response()->json(['product'=>$productslist]);
    }

Example 2 : Laravel AJAX Calling For API

index.php

$( ".btn-product" ).click(function() {
    var productsobj = {};
    productsobj.product_id = $('#product_id').val();
    $.ajax({
        type:"POST",
        dataType:"json",
        url:"<?php echo $api_url.'updateproduct'?>",
        cache: false,
        data: productsobj,
        beforeSend: function(xhr){ xhr.setRequestHeader('Authorization', '<?php echo $your_secreate_key;?>'); },
        success: function (data) {
            //console.log(data['message']);
            window.location = 'index.php?res='+data['message'];
        },
        error: function (data) {
            console.log('Error:', data);
            var obj = {
            };
        }
    });
});

routes/api.php

Route::post('updateproduct', 'API\[email protected]');

app/http/controllers/API/ProductController

public function updateproduct(Request $request)
{
    $affected = DB::table('products')->where('id', '=', $request['product_id'])->update(array('status_id' => 0));
    return $this->sendResponse($request['product_id'], 'The Product has been updated!.');
}

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 AJAX Request GET and POST Example.
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.