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'
));