Pages

How to pass a variable in route using javascript and ajax in laravel

Get the value of the using the id, define the route of the laravel with id, looking for passing to route to controller as per following.

 

 var $val = $("#pid").val();
 var ur = '{{ route("ajx.subproduct",["id" =>  ":val"]) }}';
 var url =ur.replace(':val', $val);

complete tutorial

 html>
   <head>
      <title>Laravel Ajax Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
	<script>
	    $(document).ready(function () {
		$("#mdivsubproduct").hide();

		$("#pid").change(function (e) {
		    e.preventDefault();
		    var $val = $("#pid").val();
		    var ur = '{{ route("ajx.subproduct",["id" =>  ":val"]) }}';
		    var url =ur.replace(':val', $val);
		      var data = 'pid=' + $val + '&feature=pin';
		        $.ajax({
		            url: url,
		            data: data,
		            type: 'get',
		            success: function (output) {
		                 $("#mdivsubproduct").show();
		                $("#divsubproduct").html(output);
		                
		            }
		        })
		   
		});
	    });   
	</script>      

   </head>
   
   <body>
	Product :
	 <select name="pid" id="pid" class="form-control">
	    <option value="">Select Product</option>
	     @foreach($products as  $product)
		 <option value="{{ $product->pid }}">{{ $product->name }}</option>
		  @endforeach
	</select>

	Sub Product :
	<div class="col-md-6 col-sm-6" id="divsubproduct">
		                   
	</div>

   </body>

</html>                        
web.php

Route::get('/associate/ajxsubproducct/{id}', 'ProductController@Ajaxsubproduct')->name('ajx.subproduct');


In Controller add this function

ProductController.php

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function Ajaxsubproduct(Request $request, $id)
    {

        $pid = $id;
        $Subproducts = DB::table('sub_products')->where('pid', '=', $pid)->get();
        
        $data ='<select name="sub_pid" id="sub_pid" class="form-control">
        <option value="">Select Product</option>';
            foreach($Subproducts as  $product){
                $data.='<option value="'. $product->pid.'">'. $product->name .'
                </option>';
            }
         $data.='</select>';
        
        
         return $data;
      
    }

Javascript : Auto submit form after timer/countdown in Javascript / html

how to form submit on timer/countdown you can pass time through in javascript CountDown(5,div); function, example 10 second pass CountDown(10,div);


 Let's create a index.php file.


 <html>
<body>
    <form action="" method="post" name="MCQuestion" id="MCQuestion">
    Name:<input type="test" name="name" value="ABC">
    <div><span id="display" style="color:#FF0000;font-size:15px"></span></div>
<div><span id="submitted" style="color:#FF0000;font-size:15px"></span></div>
</form>
<script>
var div = document.getElementById('display');
var submitted = document.getElementById('submitted');

    function CountDown(duration, display) {

            var timer = duration, minutes, seconds;

            var interVal=  setInterval(function () {
                minutes = parseInt(timer / 60, 10);
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;
        display.innerHTML ="<b>" + minutes + "m : " + seconds + "s" + "</b>";
                if (timer > 0) {
                    --timer;
                }else{
            clearInterval(interVal)
                    SubmitFunction();
                    }

            },1000);

    }

    function SubmitFunction(){
    submitted.innerHTML="Time is up!";
    document.getElementById('MCQuestion').submit();

    }
    CountDown(5,div);
</script>
</body>
</html>

LARAVEL Running Raw SQL Queries

In Laravel, Once you have configured your database connection, you may run queries using the  DB facade. The DB facade provides methods for each type of query: select, update,  insert, delete, and statement.

Top useful Linux Commands with Examples

In the Linux most useful commands makes you more easy to manage files system.

Let's learn most known Linux commands.

1) Listing files or Directories
     
         If you want to look the list of  directory or files on your Linux system, use the 'ls' command.

How to push code to github

Adding an existing project to GitHub using the command line, Putting your existing work project on GitHub can let you share and collaborate in lots of best ways.

What is GITHUB ?
   
      GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as a wikis and basic task management tools for every project.

CREATE New Repository to git and push project from local directory

STEP 1 :  Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files.

You can add these files after your project has been pushed to GitHub from local project.

STEP 2 :  Open Terminal.
           
Change the current working directory to your local project.
       
Initialize the local directory as a Git repository.

Installation of Node.js on Linux

Node.js is a JavaScript run time built on Chrome’s V8 JavaScript engine. Node.js can be installed in different ways on Ubuntu Linux system machine. 

You can use Ubuntu’s official repository to install Node.js or another way to use Node Source repository.

How To Install the Apache Web Server on Ubuntu 16.04

Step 1: Install Apache

Apache is available within Ubuntu’s default software repositories, so we will install it using conventional package management tools

PHP : Multi-dimensional array sort by sub array value

Multi-dimensional array - how to sort array by sub array value using  array_multisort() and array_map() function.

You can sort array ascending(SORT_ASC) or Descending(SORT_DESC) order with Sub Array value.

PHP Rest API

In this tutorial learn how to create an REST API for android or IOS/iphone application in php let's go with step by step.

API Tutorial

STEP 1 : create table in database, if you don't have database please create it.

CREATE TABLE IF NOT EXISTS `categories` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `details` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=2 ;

Call php function from ajax?

You can call PHP function like this using ajax, but not as directly call from AJAX url.

Here, Ajax Part  for explanation, set path url of db.php for call ajax and get result

 $.ajax({
      url: "http://localhost/db.php",
    data: frm_mail_data,
    cache: false,
    processData: false,
    contentType: false,
    type: 'POST',
        success: function (result) {
            alert(result);
            document.getElementById('div_result').innerHTML=result;
           }
  });

Step 1. Create index.php

Popular Posts