Muhammad Sipra
Muhammad Sipra

Reputation: 846

passing url of method in controller , in ajax call

Here is my ajax call

<script>
 jQuery(document).ready(function() {
  $("#VEGAS").submit(function(){
   var form_data =  $("#VEGAS").serialize();
    var routeUrl = "<?= url('/'); ?> /PUBLIC/vpage";
    $.ajax({
        url: routeUrl,
        type: "POST",
        data: form_data,
        success: function(result) {
            $('#msg-text-group').html('successfully added!');
            $('#msg-group').delay(1000).hide('slow');
           }
         });
      return false;
     });
   }); 
 </script>

Here is my controller function

   public function vegaspage(Request $request) {


    $inputs = Input::except('_token');
    $validator = Validator::make($inputs, Vegas::$vegas_d);
    if ($validator->fails()) {
        $messages = $validator->messages()->first();
        return Redirect::to('/vegas')->withMessage($messages);
    } else {
        if (Input::get('submit_1')) {
            $group_data = new Vegas();
            $var = "The Venetian";
            $username = Session::get('login');
            $group_data->user_name = $username;
            $group_data->firstname = input::get("firstname");
            $group_data->lastname = input::get("lastname");
            $group_data->email = input::get("email");
            $group_data->phone = input::get("phone");
            $group_data->check_in = input::get("checkin");
            $group_data->check_out = input::get("checkout");
            $group_data->date = input::get("checkboxG4");
            $group_data->hotel_name = $var;
            $group_data->nofguest = input::get("selecter-guest");
            $group_data->nofrooms = input::get("selecter-rooms");
            $group_data->nofbeds = input::get("selecter-beds");
            $group_data->save();
     }
    }

here is my route

Route::post('/vpage' , array('as' =>'vpage' ,'uses' =>'VegasController@vegaspage'));

After giving form and ajax call same id control is coming to ajax call but the give url is not hitting. There is an error in my ajax url please suggest me any solution. ?

Upvotes: 1

Views: 287

Answers (1)

Fatimah
Fatimah

Reputation: 732

The culprit is the return false, it stops the DOM propagation, delete the return false and instead use e.preventDefault like so:

  $("#VEGAS").submit(function(e){
     e.preventDefault();

     // continue your ajax request

url in the ajax call should simply be exactly what is in your route.

   url: '/vpage'

Upvotes: 1

Related Questions