user3266023 user3266023 - 2 months ago 18
Ajax Question

ajax won't work for pages that have id in their url l?

I am new to using ajax. I have ajax program that live search for a users when a person write to find users in the search bar.what it does is on every key up it use ajax to bring data from database for recommendation to help the user to choose.
But the problem is it work good for most pages except for pages that edit and show specific data.

here is the ajax

function suser(str){

var xhttp;
{ document.getElementById("srtd").innerHTML = ""
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var res=xhttp.responseText;
var i=JSON.parse(res);
var j=i.length;
document.getElementById("srtd").innerHTML = "";
var d=document.getElementById("srtd");
var u=document.createElement("ul");"list-style-type:none";

for(var k=0; k<j ; k++){
var li=document.createElement("li");

li.innerHTML="<a href='livsearres/"+ i[k].id +"' class='btn' >"+ i[k].name +" "+i[k].fname+" "+i[k].gname+"</a>";

//document.getElementById("srtd").innerHTML="<a href='showuser/"+ i[0].id +"' class='btn mybtn-n' >"+ i[0].name +" "+i[0].fname+"</a>";

}"GET", "lusrser/"+str, true);

this is my controller that return the users for the ajax

public function livesearch($str)

$users=User::where('name', 'LIKE', $str.'%')->orWhere('userid', 'LIKE', $str.'%')->get();
$searchs = explode(" ", $str);

if (count($searchs) == 1)
$users=User::where('name', 'LIKE', $str.'%')->orWhere('userid', 'LIKE', $str.'%')->get();
return $users;
elseif (count($searchs) == 2)
$users=User::where('name', 'LIKE', $searchs[0].'%')->where('fname', 'LIKE', $searchs[1].'%')->get();
return $users;
elseif (count($searchs) == 3)
$users=User::where('name', 'LIKE', $searchs[0].'%')->where('fname', 'LIKE', $searchs[1].'%')->where('gname', 'LIKE', $searchs[2].'%')->get();
return $users;
return $users;


on every pages there is search input that is similar

<input onkeyup="suser(this.value)" type="search" name="search" class="form-control" placeholder="Search" >

the URLs are


because only on showuser and edituser is not working i think it has to do with the additional data of the id of being edited or shown can any body help me how to solve it.

error message is

Sorry, the page you are looking for could not be found.

NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54


If you are using a blade file you can use this trick.

Define a javascript variable like this.

var SiteUrl = '{{ url::to("") }}';

and you can concat this with your ajax url like this."GET", SiteUrl+"lusrser/"+str, true);

if you have added '/' at your end in your baseURL then use above

else you can use"GET", SiteUrl+"/lusrser/"+str, true);

Hope this help.. else let me know again.