Support the ongoing development of Laravel.io →
Requests Input Forms
Last updated 2 years ago.
0

You could use the same Post controller and detect if it is a ajax request.

if (Request::ajax())
{
    //
}
Last updated 2 years ago.
0

Thanks for the rapid response! Let me explain a little better. So let's say I have this controller.

public function myAction() {
    //I can do this without AJAX
    if (Input::get('submit1')) {
        return '1 works';
        //^with Ajax -> Response::json('msg' => '1 works'); (not the problem)
    } elseif (Input::get('submit2')) {
        return '2 works';
        //^with Ajax -> Response::json('msg' => '2 works'); (also not the problem)
    } else {
        return 'do not collect $200. do not pass go.';
    }
}

However, the problem is that the jQuery makes the controller action unaware of what "Input::get('submit1')" actually means [if that makes sense]. Request::ajax() does a fine job of telling the controller action that AJAX was in fact used, but Laravel doesn't know what to do with Input::get('submit1'). Let me show in code:

This won't work...

public function myAction() {

    if (Request::ajax()) {
        if (Input::get('submit1')) {
            return Response::json('msg' => '1 works');
        } elseif (Input::get('submit2')) {
            return Response::json('msg' => '2 works');
        } else {
            return Response::json('msg' => 'Laravel couldn\'t tell which submit sent the post request');
        }
    }

}

And This horrible looking code won't work...

public function myAction() {

    if (Input::get('submit1')) {
        if (Request::ajax()) {
            return Response::json('msg' => '1 works');
        } else {
            return '1 didn\'t work';
        }
    } elseif (Input::get('submit2')) {
        if (Request::ajax()) {
            return Response::json('msg' => '2 works');
        } else {
            return '2 didn\'t work.';
        }
    } else {
        if (Request::ajax()) {
            return Response::json('msg' => 'Laravel couldn\'t tell which submit sent the post request');
        } else {
            return 'neither worked';
        }
    }
}

In the above code (the second sample), the only value that is ever returned is "Laravel couldn't tell which submit sent the post request." My AJAX call works, but bypasses my conditional logic for 'submit1' and 'submit2', even when their events are properly fired. It is as if the jQuery file itself is "destroying" the ability to get a boolean value from the Input::get() call, which I have previously relied on.

Here is my AJAX, if that helps. (without the header information).

$('#frm').submit(function(e) {

		e.preventDefault();

		var strData = 'text='+$('input#text').val()+'&token'+$('input[name=_token]').val();

		$.ajax({
			type: 'POST',
			data: strData,
			success: function(data) {
				$('#label').text(data.msg);
			}
		}, 'json');
		return false;
	});

Please let me know if I lost you.

Last updated 2 years ago.
0

Is there a way I could pass this info (which submit was used) into the 'data' attribute of the $.ajax({}) call to solve my problem?

Last updated 2 years ago.
0

Response::json(); takes either a string or array as an argument. Your not passing your argument correctly. I see your trying to, but you need to wrap 'something' => 'bar' in braces.

What your doing:

Response::json('something' => 'something');

What you need to be doing:

Response::json(  [  'something' => 'something'  ]  );
Last updated 2 years ago.
0

DonaldRecord said:

Is there a way I could pass this info (which submit was used) into the 'data' attribute of the $.ajax({}) call to solve my problem?

Yes, you should be able to.

One way is a hidden field and a onclick for each of the submit buttons, then you could add the hidden field in to the data array your sending in via ajax.

There is the $(document.ActiveElement) but I haven't used it and not sure it is over all supported.

Likely a few other ways to accomplish it.

Last updated 2 years ago.
0

Jaesung2061: I posted my code incorrectly. I actually use this:

return Response::json(array( 'something' => 'something'));

Wrapping the values in an associative array produces the same result.

So how does the Laravel controller action read the values from the 'data' attribute of the AJAX function? This answer will solve my problem (hopefully :P ).

Last updated 2 years ago.
0

DonaldRecord said:

So how does the Laravel controller action read the values from the 'data' attribute of the AJAX function? This answer will solve my problem (hopefully :P ).

Since your using POST in the Ajax function, the Laravel controller will read it as a standard form post. The only difference is the browser sets a header which tells Laravel the request is Ajax post.

Your not getting which submit button was clicked because your not including that in the strData array you set and post with Ajax. Unfortunately you have to do a workaround to get it to work as even a form.serialize doesn't include the submit button by default.

There are several ways to get this to work. I searched for "including submit button value with Jquery Ajax" and found several posts related to the topic.

You just have to figure out which method you want to use.

Last updated 2 years ago.
0

Thank you, when I get some time tonight, I will execute that search and see what I find :)

Last updated 2 years ago.
0

Answering your question on how Laravel reads form data. First, don't pass in data the way your doing it now.

Turn this:

var strData = 'text='+$('input#text').val()+'&token'+$('input[name=_token]').val();

Into this:

var strData = $('#frm').serialize();

What .serialize() will do is stringify all your inputs for you just as you did manually. The key's name will be whatever you named your form input.

So you have an 2 inputs like this:

<input type="text" name="body" />
<input type="hidden" name="_token" value="SomeRandomCharacters" />

Say the user types "A sentence" into the first input. form.serialize() will turn that into:

body=A+sentence&_token=SomeRandomCharacters

From your controller, if you wanted to access body you would write:

Input::get('body');

I hope that made sense, I was never too good at explaining :).

Last updated 2 years ago.
0

Both answers were great! Thank you to both of you! Unfortunately I can only mark one of them as the solution. I executed that search, read some additional material, and experimented with my code and got it to do what I needed to do. The code I used is very similar to what jaesung2061 posted. Now I know a substantial amount more than I previously did about AJAX! (AJAJ hahaha). Thanks again.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.