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

Login form using php,Ajax and jquery with mysql

Login form using php,Ajax and jquery with mysql, Redirect one to another page using ajax with return result.

Create a index.php and add this code to it

<html> <body> <div class="wrap-input100 validate-input" data-validate = "Enter username"> <input class="input100" type="text" id="user" name="username" placeholder="Email"> <span class="focus-input100" data-placeholder="&#xf207;"></span> </div> <div class="wrap-input100 validate-input" data-validate="Enter password"> <input class="input100" type="password" id="pass" name="pass" placeholder="Password"> <span class="focus-input100" data-placeholder="&#xf191;"></span> </div> <div class="container-login100-form-btn"> <input class="input100" type="button" id="logBtn" name="logBtn" placeholder="Login" value="Login"> </div> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script> $('#logBtn').click(function(event){ user = document.getElementById("user").value; password = document.getElementById("pass").value; $.ajax({ type:"POST", url:"Login.php", async: false, data: {user:user,password:password}, success: function(data){ alert(data); if(data=="admin"){ window.location="https://..Main/<file-name>.php"; } if(data=="user"){ window.location="https://.....<file-name>.php"; } } }); }); </script> </body> </html>
Create a Login.php

<?php

     $servername = "localhost";
     $username = "root";
     $password = "root";
     $dbname = "demo";

     $conn = new mysqli($servername, $username, $password, $dbname);

     $user = $_POST['user'];
     $pass = $_POST['password'];

     $sql = "SELECT * FROM users WHERE email='$user' AND pass='$pass'";

     $result = mysqli_query($conn, $sql);

     if (mysqli_num_rows($result) > 0) {
         $sql_1 = "SELECT * FROM users WHERE email='$user' AND pass='$pass' AND type='admin'";
          $result_1 = mysqli_query($conn, $sql_1);
         if (mysqli_num_rows($result_1) > 0){
             echo "admin";
             exit(0);
           }
            else{
            echo "user";
            exit(0);
         }

      } else {
         $msg = "username/password invalid";
         echo $msg;
      }

     mysqli_close($conn);
     ?>

laravel : Accessing The Request from URL using get method


To obtain an instance of the current HTTP request via dependency injection,you should type-hint the Illuminate\Http\Request class on your controller method.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {
  echo "==>".   $name = $request->input('name');
    }
}

web.php

Route::get('/category', 'CategoryController@index')->name('category');

http://127.0.0.1:8000/category?name=test

Laravel : How to call controller index function using routes

In Laravel  Let's go with the example using controller name is Product

1. Create controller in Controllers case sensitive name so, create like a example given example : ProductController.php

//controller path
<project_name>/app/Http/Controllers/<controller_name.php>
Example product controller

//controller path
<project_name>/app/Http/Controllers/ProductController.php

Laravel : setup laravel in localhost with linux

Laravel setup in localhost before we need basic requirement to fulfill laravel works, you will need to make sure your server meets the following requirements:
PHP >= 7.1.3
OpenSSL PHP Extension
PDO PHP Extension
Mbstring PHP Extension
Tokenizer PHP Extension
XML PHP Extension
Ctype PHP Extension
JSON PHP Extension
BCMath PHP Extension

Installing Laravel

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

Linux Samba Server : share server files directory with linux or window pc

How to create a linux server to share directory of projects(php,.net java) with other linux system PC or window system PC. 

How to create a network share via Samba using the Command-line interface/Linux Terminal, simple and brief way targeting Windows users.

1. Install Samba in linux server 14.04 or 16.04 18.04

sudo apt-get update
sudo apt-get install samba  

Laravel 5 - Remove public from url and run without php artisan

In laravel it's easy to step to run laravel project without using php artisan serve and removing public from url also

Here are the steps to follow.

1. Renaming the server.php to index.php

2. Copy the .htaccess from public folder to root folder (example : admin main folder)

Laravel run/access without php artisan

Laravel is Framework of php language,  its access through command php artisan server but when we run the project using live/domain name then..

its

http://localhost/<project_name>/public/



linux Add or remove user from group

How can I add or remove user from group in linux / ubuntu /debian system.

 To create a user enter:

$ sudo adduser username


 To create a GROUP enter:

$ sudo groupadd groupname

 To Add user from GROUP enter:

$ sudo adduser username group name

To Remove User from GROUP enter:

$ sudo deluser user group

AJax, PHP Tutorial generate a random string/coupon code

How to call ajax with php its easy step to call ajax lets we go for more we need to pass a url in ajax to call php function/variable/data from php for without refresh the page


    $.ajax({   
url: "string.php",
method: 'post',
data: {type:'string_action',length: length},
dataType: 'html',
success: function(response){
  response=response.replace(/(\r\n|\n|\r)/gm,"");
     $('#p_code').val(response);
     
}
 });

Popular Posts