Pages

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

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;
      
    }

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.

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 : 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

How to display records using database in php using codeigniter framework ?


This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in step-by-step fashion.

PHP: Delete an element from an array

PHP: Delete an element from an array 


 $array = array(0 => "x", 1 => "y", 2 => "z");
    unset($array[0]);
print_r($array);

//Output: Array ( [1] => y [2] => z )

What does the => operator mean in the following code?


What does the => operator mean in the following code?

$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
    echo "{$i}: {$type}\n";
}

//Output:    0: car 1: truck 2: van 3: bike 4: rickshaw

How to Add tinymce editor to Activity Post Form in buddypress

Add a rich text editor/tinymce editor in activity stream instead of the actual textarea (id= #wats-new-textarea”)

copy the file with the path to the theme/child theme

buddypress/bp-templates/bp-legacy/buddypress/activity/post-form.php to the

right place in your child-theme/theme:

/child-theme/buddypress/activity/post-form.php

Great!

forward to the here!

We’re going to use the wp_editor function to accomplish this task.

Note: if we want use wp_editor then wp_editor can replace any textarea. This means that if you use these function, that you have to remove the original textarea first.


First, we need to remove the whats new textarea from this file(/child-theme/buddypress/activity/post-form.php) , around line 36, and replace it with a custom action hook: whats_new_textarea


Before
<div id="whats-new-textarea">
   <textarea name="whats-new" id="whats-new" cols="50" rows="10"><?php if ( isset( $_GET['r'] ) ) : ?>@<?php echo esc_textarea( $_GET['r'] ); ?> <?php endif; ?></textarea>
</div>
After
<div id="whats-new-textarea">  
   ?php do_action( 'whats_new_textarea' ); ?>    
</div> 
save the file.

Now go to /plugins/bp-custom.php (if the file doesn’t exist, you have to create it) and paste in this snippet.

 <?php

function bpfr_whats_new_tiny_editor() {
// deactivation of the visual tab, so user can't play with template styles
add_filter ( 'user_can_richedit' , create_function ( '$a' , 'return false;' ) , 50 );

// building the what's new textarea
if ( isset( $_GET['r'] ) ) :
$content = esc_textarea( $_GET['r'] );
endif;

// adding tinymce tools
$editor_id = 'whats-new';
$settings = array(
'textarea_name' => 'whats-new',
'teeny' => true,
'media_buttons' => true,
'drag_drop_upload' => true,
'quicktags' => array(
'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close'));

// get the editor
wp_editor( $content, $editor_id, $settings );
}
add_action( 'whats_new_textarea', 'bpfr_whats_new_tiny_editor' ); ?>



Save and enjoy it! :)




  




 



Popular Posts