codemode's avatar

No redirect after form submission

Hello, I have a very simple form -

<form method="POST" action="check-imc">
                        <div class="form-group">
                            <label for="peso">Peso (Kg) : </label>
                            <input type="text" name="peso" class="form-control"/>
                            <label for="altura">Altura (m) : </label>
                            <input type="text" name="altura" class="form-control"/>
                            <br />
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="submit" value="Enviar" class="btn btn-primary pull-right"/>
                        </div>
                    </form>

And i have a post route like - Route::post('check-imc', 'ImcController@store')

In which there is store function, which stores the data.

I do not want to redirect/refresh after i click the submit button. Is that possible? Thanks!

0 likes
16 replies
Snapey's avatar

Only if you submit the form with Ajax

You will then be left with a form full of data, but the record written to the database.

1 like
codemode's avatar

Hi @Snapey , thank you for your reply. Could you give me a simple example for this code (i'm still new to laravel) -

View -

<form method="POST" action="check-imc">
                        <div class="form-group">
                            <label for="peso">Peso (Kg) : </label>
                            <input type="text" name="peso" class="form-control"/>
                            <label for="altura">Altura (m) : </label>
                            <input type="text" name="altura" class="form-control"/>
                            <br />
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="submit" value="Enviar" class="btn btn-primary pull-right"/>
                        </div>
                    </form>

Controller -

public function store(Request $request)
    {
        $imc = new Imc;
        $imc->peso = $request->peso;
        $imc->altura = $request->altura;
        $altura2 = ($imc->altura) * ($imc->altura);
        $imc->imccalculado = ( $imc->peso / $altura2 );
    
    $imc->save();
    
     }

So the value of $imc->imccalculado should be displayed on the view file without refreshing..

Thanks a lot!

Snapey's avatar

unfortunately the forum is not really the place to teach you javascript. I suggest you stick to basics and submit as a traditional form.

Then, later have a look at vue. There are plenty of examples about.

1 like
codemode's avatar

Anyone who can help here :( .... i need to show this code to an institute. I'll really appreciate it.

amitshrestha221's avatar

Doesn't the action needs to be binded inside {{ route(' ') }} or {{ url('') }} ??

codemode's avatar

@amitshrestha221 No... The {{}} are not really required in the action if you're not passing any dynamic values to the route.

jlrdw's avatar

People new to Laravel, stop trying to learn php, javascript, html, css, etc in one day. Take the time to learn (a year, or 2, or 3, however long) before telling a client, yes I can build you a site.

Some folks on the forum have been programming for years.

Some of you install laravel with no knowledge of php, javascript, html, css, etc, and expect laravel to be like a magic code generator that whips out an expertly crafted site in minutes. (Or a day)

STOP it rather google phprunner, php code generators, etc.

AND stop expecting others to completely code your site for you.

Of course if another forum member doesn't mind, well that's their choice.

WELDING = good example

New to welding person:

 "I am new to welding and need to pass a 6g weld test, can you quickly show me how"?

The answer, it takes many months / years of training and practice to become competent enough to pass a 6g weld test.

YOU DON'T LEARN IT IN A QUICK WHIPPED UP LESSON.

Same for coding, it is not a one evening lesson.

codemode's avatar

@jlrdw everyone has their reasons to what they are doing. Yes there are better ways of doing things with time, but again - People sometimes have their reasons. Do you want me to write about my personal life, and proceedings here? No.

If this troubles you, please avoid writing, rather than typing something negative.

1 like
jlrdw's avatar

What I wrote is not negative, but the truth. Or do you think you can learn to pass a 6g weld test with one lesson?

Or learn to be an expert in laravel super fast.

I think not.

And I was referring to new to laravel folks in general with the "whip" it out mindset, who tries to fast forward and expect to write an enterprise site with 2 days of laravel experience.

everyone has their reasons to what they are doing

Some people choose right, some choose wrong.

i need to show this code to an institute

Can you share what institute?

codemode's avatar

@jlrdw Dude, i have no idea why you'd blow your steam on something that others decide/chose. This bothers you so much? just avoid it.

1 like
jlrdw's avatar

What got me is

Could you give me a simple example for this code (i'm still new to laravel)

That forum member I happen to know writes top quality, professional web sites. He probably took quite some time to learn the "craft", and become expert.

Ask doctor for simple example of heart transplant. Are you beginning to catch on how wanting a quick example just whipped out sounds.

"Dude" learn to code the regular way, not have a quick whipped up example.

Oh:

Dude, i have no idea why you'd blow your steam on something that others decide/chose

Well coding can affect many people, if you code a site without knowing what you are doing, and it deals with personal information, it could somehow affect my security. As I have no idea who you are coding for. But it could be a company that also happens to have my personal information.

So you need to STOP and learn proper security before anything. Not a quick 5 minute security lesson, but a long drug out time frame to properly learn.

codemode's avatar

@jlrdw If such little things get you, then you really need to calm your nerves a bit. Again, i'm not here to continue a silly conversation on personal decisions. You seem to be having a bad day today.. i wish you a better rest of the day.

jlrdw's avatar

Hey good luck learning laravel, but please learn proper security. That is a serious matter these days.

1 like
sohantanna's avatar

@codemode, what @jlrdw said is completely mature and correct. The problems that the are being faced has nothing to do with laravel literally, but its being posted on a laravel forum. Odds are it will take a few months before its figured out OR you could pretty much waste some time by being told how to do it now and then coming back again and again before figuring out you should probably take time out to learn something before attempting it. Its not negative its reality.

Please or to participate in this conversation.