1

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

3
  • 1
    press F12 on keyboard, if using chrome, inspect tool will open. Go to Network tab. click on XHR sub tab. You will see your enq url call and now debug. If still ain't see anything then, in .env file APP_DEBUG set this to true. Don't forget to fire terminal command, php artisan config:cache to reflect config changes throughout the project. Commented May 7, 2019 at 10:42
  • I have already check as per your instruction but unable to insert form value what happen with my code @RahulMeshram Commented May 7, 2019 at 10:48
  • Where are you stuck? Your ajax call working fine? If Yes, do you get data in enq method()? Commented May 7, 2019 at 10:49

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0
  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

2 Comments

Failed to load resource: the server responded with a status of 404 (Not Found) i.e. /enq
change url:"/enq" to url:"{{URL::to('enq')}}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.