0

I am just migrating from traditional PHP to Laravel framework using the MVC concept. I want to be able to fetch data from the database based on the selection made from a dropdown list box. I keep getting this error: " Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException ".

See my code: Route File:

Route::get('/process-grpid','PagesController@processgrpid');

my Controller action code:

public function processgrpid($pno){
        $pno = $request->get('pno');
        $det = Stock::where('itemName',$pno)->get();
        return $det;       
    }

my view page:

<script type="text/javascript">
$(document).ready(function() { 
    $("select.partno").change(function(){
        var selectedPno = $(".partno option:selected").val();
       $.ajax(
           {
               type: "get",
                url:"/processgrpid",
               data:{pno:selectedPno},

               success:function(data){
                  // var det = JSON.parse(data);
                $("#desc").html('<input type="text"  placeholder="Enter Item Description" class="form-control" name="descr" required="required"/>');
               }
            }
        );

    });
});
    </script>


 <table>
     <tr>
              <th>Item Code/Part NO:</th>
                <td>
                <select name="partno" class="partno form-control">
                <option>Select PartNo</option>
                <option value="N/A">N/A</option>
                @foreach($pstock as $stock)
                <option value="{{ $stock->itemName }}">{{ $stock->itemName }}</option>
                @endforeach
                </select></td>
                 <th>Description:</th>
                  <td id="desc"></td></tr>
                 <tr>
    </table>

I don't know what am doing wrong. I have checked stackoverflow, but non of the soution meet my needs. All i wanted is to get the description of stock item from the database based on the dropdown list of partno. Please, help me look and guide me accordingly .

1
  • Your route is process-grpid when your Ajax is calling processgrpid Commented Jul 4, 2018 at 16:24

2 Answers 2

1

You made a minor mistake in calling route through ajax request.

Change url:"/processgrpid" to url:"/process-grpid".

Reason: As you defined route as

Route::get('/process-grpid','PagesController@processgrpid');

So now public url will be process-grpid instead of controller function processgrpid.

Also update controller function as:

public function processgrpid(Request $request) {
    $pno = $request->get('pno');
    $det = Stock::where('itemName', $pno)->get();
    return $det;
}
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you very much. It works. Please, how do i get the description value displayed in the input field : i have parse the result like var det = Json.parse(data), and i can see the Json object with key value paired, how can i display the value in the input field. $("#desc").html('<input type="text" placeholder="Enter Item Description" class="form-control" name="descr" required="required"/>');
Try this: $("#desc").html('<input type="text" placeholder="Enter Item Description" class="form-control" name="descr" required="required" value="' + det.label_name + '"/>'); . replace label_name with actual label in det variable.
@Locepreet Singh: I have tried it but it is not displaying anything in the input field.
I have tried it but it is not displaying anything in the input field. I also added this at the top : var data = JSON.parse(data)
What is the value in var data;
|
0

you should pass $request to your controller as below:

public function processgrpid(Request $request){
        $pno = $request->pno;
        $det = Stock::where('itemName',$pno)->get();
        return $det;       
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.