php - How to pass a looped dropdown value from view to controller using route in Laravel?

103

I have this dropdown with looped item in it. All I want is when I change the value of the selected item on dropdown it will send the value to the controller using route.

This is the code of looped item in my dropdown in my view namedresolution_time_of_calls.blade.php:

<div >  
    <form action="" method="POST">
       <select name="year">
          <option selected name="years" disabled>--Select Year-- </option> 
         <?php 
          $start = "2017";
          $end = (int)date("Y");        
            for($i=$start; $i<=$end; $i++){         
               echo '<option value='.$i.'>'.$i.'</option>';
             }
            ?>             
       </select> 
    </form> 
  </div>

This is a screenshot of my dropdown:enter image description here

All I want is when I changed the Year on mydropdown, the year below the graph will change its year.

I don't have a route for that yet because I don't know what code to write.

This is my controller where the dropdown value should be passed. The value that will be sent by the route should be passed into this function and I don't know how because I'am a newbie in Laravel. Thanks in advance for the help!

public function resolution_time_of_calls(){         

        //declarations            
        $arr_val = array();
        $arr_val1 = array();
        $arr_val2 = array();
        $arr_monthname = array();
        $arr_monthInt = array();                   

        //SELECTING MONTHS  
        $quer = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt
            ,monthname(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Month
            ,year(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Year               
            FROM rtdb.Tickets T
            LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
            AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
            WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
            AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))=2018 
            GROUP BY Month,MonthInt,Year Order by MonthInt asc
            ");   
        $chart = new SampleChart;                            
        $chart->title('Resolution Time of Calls');                 
        foreach($quer as $query){
            array_push($arr_monthname, $query->Month.", ".$query->Year);  
            array_push($arr_monthInt, $query->MonthInt);   
        }                     
        $chart->labels($arr_monthname); 

        ///////////////////// TOTAL, AVERAGE,MAXIMUM Query  ////////////////////
        foreach($arr_monthInt as $month){
            $sql = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt

                ,AVG(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS AVG   
                ,MAX(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS MAX               
                FROM rtdb.Tickets T
                LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))=2018 
                GROUP BY MonthInt Order by MonthInt asc");
        }
        foreach($sql as $value){

            array_push($arr_val1, $value->AVG );
            array_push($arr_val2, $value->MAX );
        }

        $chart->dataset('Average Hours', 'bar', $arr_val1)->backgroundcolor('green');   
        $chart->dataset('Maximum Hours', 'bar', $arr_val2)->backgroundcolor('blue');   
        $chart->height(600);


    return view('resolution_time_of_calls',['chart'=>$chart]);
}  
482

Answer

Solution:

You may use Javascript onchange event to get the value of selected year and pass it to the controller as route parameter.

People are also looking for solutions to the problem: mysql - PHP echo json_encode line by line instead of at the end to tackle memory problem, is that a bad idea?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.