dimanche 26 mai 2019

Redirect to same ajax page after reload or form submission with old input data

I have a dashboard with some side menus. Each menus loads a page through ajax call after it is clicked. By default when dashboard is loaded it loads the page, there is also a "dashboard" menu to switch between others like profile and add student.

But if I am at profile page and I refresh It goes back to dashboard page. Also if I try to add student by filling the form and if the form has any error it goes back to dashboard again with all old input data lost.

Web.php

Route::prefix('admin')->group(function(){
Route::get('/dashboard','AdminController@dashboard')->name('admin.dashboard');
Route::get('/login', 'Auth\AdminLoginController@showLoginForm')->name('admin.login');
Route::post('/login', 'Auth\AdminLoginController@login')->name('admin.login.submit');
Route::post('/add_student','AdminController@add_student');

    Route::middleware('ajax')->group(function(){
    Route::get('/summary','AdminController@summary')->name('admin.summary');
    Route::get('/profile', 'AdminController@profile')->name('admin.profile');
    Route::get('/add_student_form','AdminController@add_student_form')->name('admin.add_student');
});

});

AdminController.php

 public function add_student_form(){
    return view('admin.add_student');
}

public function add_student(Request $request)
{
    $this->validate($request,[

        'name'=>'required|string',
        'course_id'=>'required',
        'dob'=>'required|date',
        'gender'=>'required|string',
        'category'=>'required|string',
        'qualification'=>'required|string',
        'fathername'=>'required|string',
        'mothername'=>'required|string',
        'mob_no'=>'required|string|max:10',
        'address'=>'required|string',
        'email'=>'required|email',
        'PIN'=>'required|string|max:6',
    ]);

    $student = new Student;
    $student->name = $request['name'];
    $student->roll_no ='NITTI'.rand(10000,99999);
    $student->password = Hash::make($student['mob']);

    $student->fees = new StudentFees;
    $student->fees->paid_fees = $request['paid_fees'];
    $student->fees->balance_fees = 7000;
    $student->detail = new StudentDetail;
    $student->detail->course_id = $request['course_id'];
    $student->detail->gender = $request['gender'];
    $student->detail->category = $request['category'];
    $student->detail->qualification = $request['qualification'];
    $student->detail->fathername = $request['fathername'];
    $student->detail->mothername = $request['mothername'];
    $student->detail->mob_no = $request['mob_no'];
    $student->detail->email = $request['email'] ;
    $student->detail->address = $request['address'];
    $student->detail->PIN=$request['PIN'];
    $student->detail->dob = $request['dob'];
    $student->push();
    return redirect()->back();

}

add_student.blade.php

  <form action="/admin/add_student" method="POST">
      @csrf
   //It has all the fields here and a submit button
   </form>

dashboard.blade.php

   <div class="container-fluid">
      <div class="row">
         <div class="col-2  sidebar">
            <img src="" class="fit-image">
             <p class="text-center"></p>
             <hr>

         <ul class="list-group">
            <li class="list-group-item" id="summary">Dashboard</li>
            <li class="list-group-item" id="admin_profile">Profile
            </li> 
            <li class="list-group-item" id="students">Students <i class="fas fa-angle-right ml-5"></i>
           <ul class="sidenav list-group">
            <li class="list-group-item" id="add_student">Add Student</li>
            <li class="list-group-item" id="viewall">View All </li>
          </ul></li> 
        </ul>
       </div>
         <div class="col-10 dashboard" id="admin">
         </div>
        </div>
         </div>

dashboard.js

    $(document).ready(function(){
      $.ajax({
          url:'/admin/summary',
          type: "GET",
          success: function(data){
            $data = $(data); 

            $('#admin').html($data).fadeIn();    
            }
        });
      });

      $(document).on('click','#summary', function(){
        $.ajax({
            url:'/admin/summary',
            type: "GET",
            success: function(data){
              $data = $(data); 
              $('#admin').html($data).fadeIn();    
              }
          });
        });


    $(document).on('click','#admin_profile', function(){
        $.ajax({
            url:'/admin/profile',
            type: "GET", 
            success: function(data){
              $data = $(data); 
              $('#admin').html($data).fadeIn();    
              }
          });
        });
          $(document).on('click','#add_student', function(){
            $.ajax({
                url:'/admin/add_student_form',
                type: "GET", 
                success: function(data){
                  $data = $(data);  produced
                  $('#admin').html($data).fadeIn();    
                  }
              });
            });

I tried to redirect()->back->withInput('the fields');, but it didn't work. If there is better way to load the pages which holds the same page after reloading or form submission I would welcome that.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2VWcTfG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire