0

what I am trying to do is to pass a variable from my view to a controller function..The problem is that I am never routed to my controller's function store.So I cant check if I get the variable as well. The ajax function seems to work(the alert is displayed) but I never get the message success from my controller..

So far my route file is :

Route::get('pois', array(
    'uses' => 'MapController@create',
    'as' => 'pois.create'
    ));

Route::post('pois', array(
    'uses' => 'MapController@store',
    'as' => 'pois.get'
    ));

My MapController is:

public function create()
{

    $bar=new Map;
    $rest=new Map;
    $mag=new Map;

    $bar = Map::where('type', '=', 'bar')->take(10)->get();
    $rest = Map::where('type', '=', 'restaurant')->take(10)->get();
    $mag = Map::where('type', '=', 'magazine')->take(10)->get();

    return View::make('pois.markers')->with('bar',$bar)->with('rest',$rest)->with('mag',$mag);

}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    dd('suceess');

}

And my javascript-jquery script is bellow:

function ajaxCall(id) {



       $.ajax
        ({
                type: "POST",
                url: "pois",
                data: {"id" : id}, 
                success: function(response)
                  { 
                    alert('ok');
                  }
        });
}



function checkBoxes(elem){

if(document.getElementById(elem).checked==true){

  ajaxCall(elem); 

}
else{

clearMarkers(elem);

}

}

Bellow is my HTML as well :

@section('main_contents')

    <br/>

    <div class="text-center">





<label class="checkbox-inline">
  <input type="checkbox" id="bar" name="group" value="option1" onClick="checkBoxes(this.id)"> Καφετέριες
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="restaurant" name="group"  value="option2" onClick="checkBoxes(this.id)"> Εστιατόρια
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="magazine" name="group"  value="option3" onClick="checkBoxes(this.id)"> Μαγαζιά
</label>



<br/>

</div>

     <div id="map" style="height:500px;"></div>

     @stop

2 Answers 2

1

You are not receiving success message because you are not returning any message from your controller.

change dd('success'); to return 'success';

public function store()
{
    return 'success';

}

Your Ajax code:

$.ajax
        ({
                type: "POST",
                url: "pois",
                data: {"id" : id}, 
                success: function(response)
                  { 
                    alert(response);
                  }
        });

Edit:

Change URL url: "pois", to url: "/pois",

Jquery code:

$( document ).ready(function() {

    $(".checkbox").change(function() {
        if(this.checked) {

            var id = $('.checkbox').val();

            var request = $.ajax({
              url: '/pois',
              type: "POST",
              data: { id : id },
              dataType: "html"
            });

            request.done(function(result) {
                if(result == 'success') {
                    alert("Success");
                } else {
                    alert ("Sorry! unable to add the data");
                }
            });

            request.fail(function(jqXHR, textStatus) {
              console.log( "Request failed: " + textStatus );
            });
        }
    });
});

HTML code:

@section('main_contents')
    <br/>
    <div class="text-center">

        <label class="checkbox-inline">
            <input type="checkbox" id="bar" class="checkbox" name="group" value="1"> Καφετέριες
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="restaurant" class="checkbox" name="group"  value="2"> Εστιατόρια
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="magazine" class="checkbox" name="group"  value="3"> Μαγαζιά
        </label>

        <br/>

    </div>

     <div id="map" style="height:500px;"></div>

@stop

I haven't tested the code but should work.

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

5 Comments

No its still not working...The dd function should work...it s shortcut for var_dump and die...the problem is i never reach my store function..
I think, your Ajax URL has problem. try to change the URL to /pois
I have tried that also...No luck though..Does it mind that my script has some javascript and some other jquery functions..?
Which browser are you using? if you using Chrome, check the network section from developer tool. you will see the error.
Would you mind if i change your javascript and HTML. I will use pure JQuery and html5
0

While making ajax call to controller... you can check like this

$jsonResponse = [
                 'imageReceived' =>  $r->hasFile('profilePicture')
                ];
return $jsonResponse;

This will return response to console with true or false like this

{"imageReceived":false}

Comments

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.