Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel ajax post request

            let route = "{{ route('srcname') }}";
            let token = "{{ csrf_token()}}";
            $.ajax({
                url: route,
                type: 'POST',
                data: {
                    _token:token,
                    name:'name',
                    email:'email'
                    phone:'09876767'
               
                },
                success: function(response) {
                    console.log(response)

                },
                error: function(xhr) {
                    //Do Something to handle error
                }});

        }
        
        
        
        
        // laravel web
        Route::post('srcname', [Controllername::class, 'filter'])->name('srcname');
        
        // Controller
         public function filter(Request $request){ return $request->name ;}
        
        
        
        
Comment

laravel send ajax

// sending ajax from view to controller
<script>
let data = {
	_token: "{{ csrf_token() }}", //required to use token if method is post
	name: "ajax",
    request: 1
}
$.ajax({
	type: "post",
    url: "{{ URL::to('/') }}/ajax_request_url",
    data: data,
    success: function(response) {
		console.log(response);
    }
});
</script>


// controller
class Controller_name extends Controller {
	function name(Request $request) {
  		$post = $request->input();	
  		header('Content-Type: Application/json');
  		echo json_encode($post);
	}
}

// web.php
route::any('/ajax_request_url', 'AppHttpControllersController_name@name');
Comment

how to send ajax request in laravel

//script
<script>
  $(".buttonToClick").click(function(e){
      e.preventDefault();

      var name = $("input[name=name]").val();
      var email = $("input[name=email]").val();
      var message = $("input[name=message]").val();
      var _token   = $('meta[name="csrf-token"]').attr('content');

      $.ajax({
        url: "/ajax-request",
        type:"POST",
        data:{
          name:name,
          email:email,
          message:message,
          _token: _token
        },
        success:function(resp){
          alert(resp);
        }, error: function(){
          alert("Error");
        }
       });
  });
</script>

//route in web.php
	Route::post('/ajax-request', [yourControllerName::class, 'getAjaxRequest');
    
//Controller function
public function getAjaxRequest(Request $request){
	$data = $request->all();
    echo "Success";
}
Comment

PREVIOUS NEXT
Code Example
Php :: group in route in laravel 
Php :: php append line to file 
Php :: php array to csv 
Php :: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in 
Php :: How to create an array from a CSV file using PHP 
Php :: carbon date minus days 
Php :: laravel append to model 
Php :: run python script from batch file with arguments 
Php :: php curl pass user:password 
Php :: How do I get PHP errors to display 
Php :: oci_execute(): ORA-01810: format code appears twice in 
Php :: How to insert time in table using CodeIgniter 
Php :: wordpress wp_enqueue_script footer 
Php :: php array_map passing parameters 
Php :: how to add title to wordpress php 
Php :: create user with tinker php laravel 
Php :: Woocommerce Display field value on the admin order edit page [Custom Field Display 2] 
Php :: format datetime ISO php 
Php :: how to pass variable in inside function into where in laravel 
Php :: check file size validation laravel 
Php :: php add to associative array 
Php :: php echo alot of html 
Php :: how to create a logout button in wordpress 
Php :: loop object property laravel 
Php :: phpmailer send attachment 
Php :: the action you have requested is not allowed. in codeigniter 
Php :: sort multidimensional array php by key 
Php :: laravel where like 
Php :: laravel route view 
Php :: Format and show date PHP 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =