lundi 14 novembre 2016

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;
        if(str.length==0)
        { document.getElementById("srtd").innerHTML = "" 
            document.getElementById("srtd").style.border="0px";
            return;}
        else{
        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");
        u.style="list-style-type:none";

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

         u.appendChild(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>";


        }
      }
      xhttp.open("GET", "lusrser/"+str, true);
      xhttp.send();}
    }

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;
      }
      else 
    {
        $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

route::get('/indexuser','UserController@index');
route::get('/createuser','UserController@create');
route::post('/createuser','UserController@store');
route::get('/edituser/{id}','UserController@edit');
route::post('/edituser','UserController@update');
route::get('/showuser/{id}','UserController@show');

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire