如何在视图中将变量从控制器传递给ajax请求,然后在php脚本(codeigniter)中将其检索

时间:2019-05-17 11:32:01

标签: javascript php mysql ajax codeigniter

您好,我想传递此变量: ID_person 进行查看,然后将其发送到php脚本以从数据库中获取请求的数据。

控制器:

 public function view($id){
        $data = array();

        if(!empty($id)){
            $data['zakaznici'] = $this->Zakaznici_model->getRows($id);              //$data['temperatures'] = $this->Temperatures_model->getRows($id); 
              $data['title'] = 'Údaje o zákazníkovi';
              $data['ID_person'] = $id;


            $this->load->view('zakazniciview', $data);

        }else{
            redirect('/zakaznici');
        }
    }

到目前为止,我在我的视图中正在使用此请求:

<script type="text/javascript">


$(function() {

  $.ajax({
    url: "http://localhost/skolaa/chart_vypozicky.php",
    type: "GET",
    success: function(data) {
      chartData = data;
      var chartProperties = {
        caption: "Celková suma za prenájmy počas jednotlivých rokov",
        xAxisName: "Rok",
        yAxisName: "Suma ",
        rotatevalues: "0",
        useDataPlotColorForLabels: "1",
        theme: "fusion"

      };
      apiChart = new FusionCharts({
        type: "column2d",
        renderAt: "chart-container",
        width: "550",
        height: "350",
        dataFormat: "json",
        dataSource: {
          chart: chartProperties,
          data: chartData
        }
      });
      apiChart.render();
    }
  });
});
</script>

它正在工作,但是我需要以某种方式从控制器中获取该 ID_person 变量,并将其发送到 chart_vypozicky.php 脚本,然后在此脚本的查询中进行检索。 / p>

Php脚本:

<?php
    //address of the server where db is installed
    $servername = "localhost";
    //username to connect to the db
    //the default value is root
    $username = "root";
    //password to connect to the db
    //this is the value you would have specified during installation of WAMP stack
    $password = "";
    //name of the db under which the table is created
    $dbName = "prenajom_sportovisk";
    //establishing the connection to the db.
    $conn = new mysqli($servername, $username, $password, $dbName);
    //checking if there were any error during the last connection attempt
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    //the SQL query to be executed
    $query = "SELECT SUM(cena) as price, YEAR(DATUM) as rok FROM sportoviska_zakaznici where ID= "; //I need to retrieve that id variable here
    //storing the result of the executed query
    $result = $conn->query($query);
    //initialize the array to store the processed data
    $jsonArray = array();
    //check if there is any data returned by the SQL Query
    if ($result->num_rows > 0) {
      //Converting the results into an associative array
      while($row = $result->fetch_assoc()) {
        $jsonArrayItem = array();
        $jsonArrayItem['label'] = $row['rok'];
        $jsonArrayItem['value'] = $row['price'];
        //append the above created object into the main array.
        array_push($jsonArray, $jsonArrayItem);
      }
    }
    //Closing the connection to DB
    $conn->close();
    //set the response content type as JSON
    header('Content-type: application/json');
    //output the return value of json encode using the echo function.
    echo json_encode($jsonArray);
?>

是否有可能做到这一点?我将非常感谢所有建议。

1 个答案:

答案 0 :(得分:0)

在您看来,传递ID_person,然后在chart_vypozicky.php页面中使用$ _GET ['ID_person']

cmd
<script type="text/javascript">


$(function() {

  $.ajax({
    url: "http://localhost/skolaa/chart_vypozicky.php",
    type: "GET",
    data:{ID_person:'<?php echo $ID_person; ?>'},
    success: function(data) {
      chartData = data;
      var chartProperties = {
        caption: "Celková suma za prenájmy počas jednotlivých rokov",
        xAxisName: "Rok",
        yAxisName: "Suma ",
        rotatevalues: "0",
        useDataPlotColorForLabels: "1",
        theme: "fusion"

      };
      apiChart = new FusionCharts({
        type: "column2d",
        renderAt: "chart-container",
        width: "550",
        height: "350",
        dataFormat: "json",
        dataSource: {
          chart: chartProperties,
          data: chartData
        }
      });
      apiChart.render();
    }
  });
});
</script>


使用此更新的代码