0

What wants to be achieved ?

On-change the selection of a Select list, this selectedIndex is picked up by the controller, sent to the model the SQL query and results are returned via ajax underneath the select list. That is very easy to do in an ordinary php environment, but I am puzzled within the Laravel environment.

If it is not clear: what I want is to have this: http://www.w3schools.com/php/php_ajax_database.asp done in a Laravel environment

UPDATED: I have improven the code using the indications of Itachi:

This would be if I could use simple Ajax, but was adviced to use JQUERY/JSON instead, dont know why this would not work.

<script>        
    function showHint(str)
        {
        if (str.length==0)
          {
          document.getElementById("txtHint").innerHTML="";
          return;
          }
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","gethint.php?q="+str,true);
        xmlhttp.send();
        }   

    </script>

And then the PHP get stuff etc, would be easy.

So, the JQUERY/JSON would go more or less like this, though I dont know how to complete it

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

    $.ajax({
        url: '<?php echo route("hint");?>',
        type: 'POST',
        data: { especialidades: $('especialidades').val() },
        dataType: 'json',
        success: THIS WOULD BE A FUNCTION THAT WOULD PRINT THE RESULTS FROM THE CONTROLLER
         }
    });

    e.preventDefault();
});

And my own form looks like this:

<form role="form"  class="bg-success" id="ajax">
  <div class="form-group">
<select name ="especialidades" id = "especialidades" class="form-control" onchange="showHint(this.value)">                 
   <?php foreach($data['kategori'] as $detalle): ?>
   <option value="<?php echo $detalle->id_specialty; ?>"><?php echo $detalle->spec_description; ?></option>  
    <?php endforeach;?>
     </select>
   </div>
   </form>
  <div id="txtHint"><b>Person info will be listed here.</b></div>

And the controller should look like this:

class Hint extends BaseController{

    public $restful = true;
    public function post_appajax()
    {
         NEED TO GET THE SELECTED INDEX SENT BY THE JQUERY SCRIPT IN THE VIEW: HOW??   

        SOMETHING EQUAL TO THIS ===> ::json(Input::get('especialidades'));

    }
  }

AND THE ROUTE FILE GOES LIKE THIS: (by Itachi)

Route::post('hint', array(
    'as'    =>    'hint',
    'uses'  =>    'Hint@getHint'
));
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • can you rephrase your question? i don't get what the problem is. – itachi Feb 08 '14 at 17:09
  • i got what you want. i am asking what problems you are having? **WHAT EXACTLY THE PORTION THAT YOU CAN'T DO?** – itachi Feb 08 '14 at 17:22
  • 1) use Jquery. 2) there are **LOTS** of different ways to do that. depends on how you are mapping the routes. (in simple words, show the routes file) – itachi Feb 08 '14 at 17:30
  • do you want this controller to be restful or not? you are mixing up a lot of codes here. – itachi Feb 08 '14 at 17:56
  • I really dont mind about restful or not, I think that means that you can get to see what http verb your method uses, but whatever. Yes, it is kind of messy, and I am learning how to write a URL in laravel in the controller, yet... – Idontgetit Feb 08 '14 at 18:08

2 Answers2

0

it is actually very simple.

Routes.php

Route::post('hint', array(
    'as'    =>    'hint',
    'uses'  =>    'HintController@getHint'
));

HintController.php

class HintController extends BaseController {

    public function getHint()
    {
          return Response::json(//whatever you want);
    }
}

View

$.ajax({
    url: '<?php echo route("hint");?>', //<-------- see this
    type: 'POST',
    data: { especialidades: $('especialidades').val() },
    dataType: 'json',
    success: SEND IT TO THE CONTROLER HOWEVER YOU CAN...
     }
});

Rest will be upto you.

itachi
  • 6,323
  • 3
  • 30
  • 40
  • I very much appreciate your help and time. At the controller, the return Response::json, what I would want is that it sends that selectedIndex to the model I dont understand how it can Return something if I dont see code that actually has received it, and at the view, when you mark "see this", is that the correct route to the controller, just like that? echo route? and the "success" at the Jquery, I dont know how to take it from there :/ – Idontgetit Feb 08 '14 at 18:22
  • 1. it will be a big tutorial if i need to write all the steps. better, try to understand how laravel works and then dive into above programs. 2. yup, its pretty easy to write urls for **named** routes. 3) you need to parse the JSON output and then output it. it isn't possible to write all in one answer. that's why i advice, learn the basic stuffs 1st. then try complex problems. – itachi Feb 08 '14 at 18:26
  • I want to put it as very helpful but my reputation points dont allow me to mark that. As you mention, it would take me probably 6 months to learn JSON, and then Laravel, but what I just wanted was to get that stuff solved I cant be learning that many languages before I get my issue solved, I learn by doing, dont have time to do it otherwise, very thankful I am though. – Idontgetit Feb 08 '14 at 18:33
  • [**JSON**](http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript), [**laravel routing**](http://laravel.com/docs/routing), [**laravel controller**](http://laravel.com/docs/controllers), [**laravel response**](http://laravel.com/docs/responses#special-responses) there ya go. only 4 page. 1 hour max. – itachi Feb 08 '14 at 18:36
  • I have been to all those pages long before, otherwise I would have not been able to write a single line in this post. How do you think I even got to the point of being able to have specific doubts if I had not come that far reading before, but my issue is far more complicated than that basic stuff. I already know the basic stuff, I have populated select lists from the database etc but this stuff is different, I still dont know why the Ajax snippet was not applicable etc. Again, I continue being thankful because you have been the only one to answer, nobody else seems to know how. – Idontgetit Feb 08 '14 at 19:08
0

According to the method name in your controller the route should be (Laravel-3:RESTfull controller)

Route::post('hint', array( 'as' => 'hint', 'uses' => 'Hint@appajax'));

Your Controller

class Hint extends BaseController{

    public $restful = true;

    public function post_appajax()
    {
        // ...
    }
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307