steave
steave

Reputation: 135

How to insert form values into database via jquery ajax using laravel?

controller: UserController.php

<?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use DB;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;

    class UserController extends Controller {
        public function insertform() {
            return view('contact-us');
        }
        public function enq()
        {
            $name = $this->input->post('name');
            $phone = $this->input->post('phone');
            $email = $this->input->post('email');
            $msg = $this->input->post('msg');
            $data=array('name'=>$name,"phone"=>$phone,"email"=>$email,"msg"=>$msg);
            DB::table('enquiry')->insert($data);
            echo "Record inserted successfully";
        }
    }

View: Contact-us.blade.php

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            name = $("#name").val(); 
            phone = $("#phone").val();
            email = $("#email").val();
            msg = $("#msg").val();
            $.ajax({
                type:"POST",
                data:{"name":name,"phone":phone,"email":email,"msg":msg},
                url:"/enq",
                success:function(data){
                    $("#success").html(data);
                }
            });
        });
    });
</script>
<div id="success"></div>
<form method="post">
    <input type="text" placeholder="Your Name" name="name" id="name">
    <input type="text" placeholder="Phone Number" name="phone" id="phone">
    <input type="text" placeholder="Email Adress" name="email" id="email">
    <textarea placeholder="Massege" name="msg" id="msg"></textarea>
    <input type="submit" name="submit" id="submit" value="submit now" class="btn-blue">
</form>

web.php

<?php
Route::get('contact-us', function () {
    return view('contact-us');
});
Route::post('enq','UserController@enq');

I am new in laravel Here what am I doing I am going to insert a simple form value into database. Now, what happen when I click on submit button it show nothing. I have no idea about that. So, How can I do this? Please help me.

Thank You

Upvotes: 1

Views: 1902

Answers (2)

akhil
akhil

Reputation: 106

  1. Add an additional data, token in ajax request.

        $.ajax({
            type:"POST",
            data:{"name":name,"phone":phone,"email":email,"msg":msg,"_token":"{{csrf_token()}}"},
            url:"{{URL::to('enq')}}",
            success:function(data){
                $("#success").html(data);
            }
        });
    
  2. To check for errors, you can press F12 in keyboard and see the console. Error are showed in red color and if the error code is 500, then its related to php

Upvotes: 0

Yegor Keller
Yegor Keller

Reputation: 41

Change your enq function

public function enq(Request $request)
       {
           $name  = $request->name;
           $phone = $request->phone;
           $email = $request->email;
           $msg   = $request->msg;
           $data=array('name'=>$name,"phone"=>$phone,"email"=>$email,"msg"=>$msg);
           DB::table('enquiry')->insert($data);
           echo "Record inserted successfully";
       }

And also configure CSRF token

add <meta name="_token" content="{!! csrf_token() !!}"/> in your head section

and then

 $.ajaxSetup({
        headers:
            {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
    });

Add this code before your ajax call

Upvotes: 1

Related Questions