1

I am beginner in `Laravel`. I am creating a admin panel in Laravel. I am getting data from database by id (Edit Page). And In my edit page, my form is not submitting correct. I mean form's `action` attribute didn't set fine. Wait, here is my code:
`View.blade.php`
<div class="card chapterCard">
        <div class="card-body">
            @foreach($data as $chapter)
               // See this in action
                <form method="POST" action="{{ action('ChaptersController@store') }}">
                    @csrf
                    <h2 class="h2-responsive text-center">Edit Chapter</h2>
                    <div class="form-group row">
                        <div class="col-md-12">
                            <div class="md-form">
                                <label for="chapterName">Chapter Name</label>
                                <input type="text" name="chapterName" id="chapterName" class="form-control" value="{{ $chapter->chapter }}">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div>
                                <button type="submit" class="btn btn-primary btn-block">Save</button>
                            </div>
                        </div>
                    </div>
                </form>
            @endforeach
        </div>
    </div>

Route

Route::prefix('/admin')->group(function () {

    Route::get('/chapters/sahih_bukhari/edit/{chapter_number}', 'ChaptersController@editSahihBukhari')->name('edit_chapter_sahih_bukhari');
    
});
// For edit Form
Route::post('store', 'ChaptersController@store');

ChaprtersController.php

class ChaptersController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware('auth');
    }

    public function editSahihBukhari($chapter_number) {
        $chapter = Chapters::where([
                ['chapter_number', $chapter_number],
                ['source', 'Sahih Bukhari']
            ])->get();
        return view('edit_chapter_sahih_bukhari', ['data' => $chapter]);
    }

   // For Edit Form
    public function store(Request $request) {
        print_r($request->input());
    }
}

enter image description here

When i click on submit, to check if it is working, It redirects me to http://localhost:8000/store. It means it goes to another page. But I want it not to o to another page after submitting, It should stay on the same page. I don't know what i am doing wrong. I googled a lot, I found a lot of answers but all of them were doing this that "After submitting, it goes to another page" But I want it to stay on the same page. Please help me, how can it be done. I am stuck

Zain Shabir
  • 300
  • 2
  • 11
  • Your form is a POST form (https://www.w3schools.com/tags/ref_httpmethods.asp), and by default that generally causes the page to change. This might be helpful: https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit. "Basically, include event.preventDefault();" – jaypb Jul 01 '20 at 16:00
  • This is not the answer of my question brother. The thing i want that after submitting, It should save data etc, and It should stay on same page after submitting like we have did in simple php? – Zain Shabir Jul 01 '20 at 16:03
  • In simple PHP, the reason it stays on the same page is because it doesn't submit the form using a POST, it processes the form submission using PHP script. If you cannot process the form using PHP script, the other option is to use an AJAX form submission. If you can't use PHP script or AJAX form submission, you have to do a POST like you currently are now, and redirect. – jaypb Jul 01 '20 at 16:13

3 Answers3

1

You did :

public function store(Request $request) {
        print_r($request->input());
}

This will give you an output with your input data, not redirect.

So you need to do like that :

public function store(Request $request) {
        // do what you want, like save data to db
       return redirect('home/dashboard'); // after save data to db, it will redirect you to home/dashboard page, It will redirect on server side not client side
}
STA
  • 30,729
  • 8
  • 45
  • 59
  • Buddy, I don't want to redirect. Is there any way that it should stay on same page? I mean, router is wrong or something else. – Zain Shabir Jul 01 '20 at 16:01
  • Yes, you can send user to view `return view('home/dashboard');` if you want you can send a measaage with it like `return redirect('home/dashboard')->with('message', 'Your data submitted successfully');` – STA Jul 01 '20 at 16:03
  • For the same page you can do like these : `return redirect()->back();` for send a message `return redirect()->back()->with('message', 'Submitted Successfully');` – STA Jul 01 '20 at 16:07
  • Instead of using `redirect('home/dashboard');`, Isn't it good to use `redirect()->back()` ?.. Because It's redirecting back and my page is by id, so it will be good for me. But how can i send message with `redirect()->back()` ? – Zain Shabir Jul 01 '20 at 16:08
  • You need to get the `message` like that https://stackoverflow.com/a/36709439/4575350 – STA Jul 01 '20 at 16:10
  • Oh, yes. This is fine now. – Zain Shabir Jul 01 '20 at 16:13
  • @Zain happy coding, peace be upon you. – STA Jul 01 '20 at 16:14
0

In your ChaptersController:

public function store(Request $request) {
    print_r($request->input());
return redirect()->back(); //redirects the user back to the page, you can play with it however you want
}

More On redirect here: https://laravel.com/docs/7.x/redirects

Harout
  • 175
  • 2
  • 3
  • 12
  • It's fine, but not the right way, i guess. Because It's returning back to the old page. It should not go to another page firstly, So I don't have to return back. Buddy, simple php stays on same page. And laravel does not, why? I don't get it – Zain Shabir Jul 01 '20 at 15:57
  • 1
    Only if you submit the form with Ajax it will not redirect. – Harout Jul 01 '20 at 16:03
  • Then why simple php stays on same page? I think it's because of route because it's changin url. Can't it be the same url in `route ::post` ? – Zain Shabir Jul 01 '20 at 16:05
  • I do not understand you, you are submitting an information and sending it to ChaptersController@store and store is displaying what you have written that is print. Dont print if you dont want anything showing. – Harout Jul 01 '20 at 16:07
0
Route::prefix('/admin')->group(function () {

    Route::get('/chapters/sahih_bukhari/edit/{chapter_number}', 'ChaptersController@editSahihBukhari')->name('edit_chapter_sahih_bukhari');
    
    Route::post('/chapters/sahih_bukhari/edit/{chapter_number}', 'ChaptersController@store')->name('post_chapter_sahih_bukhari');;
});
Cameron
  • 532
  • 3
  • 9
  • Post and get at the same endpoint, alternatively add Route::post('/chapters/sahih_bukhari/edit/{chapter_number}', 'ChaptersController@store')->name('somepost'); and use action="{{route('somepost')}}" – Cameron Jul 02 '20 at 01:27