通过AJAX将Javascript $ _GET变量传递给CodeIgniter模型

时间:2010-12-29 16:22:32

标签: php javascript ajax google-maps codeigniter

早上好,我正在使用Google Maps V3来实施项目。

在我看来,infowindow会打开用户输入数据。我想将这些数据保存到我的数据库中。

我的addMarker模型函数应该获取输入的数据并将其保存到我的数据库中。 目前,infoWindow打开,我可以输入信息。但是这些数据没有发送到 我模型中的addMarker函数。

function saveData() {


      var name = escape(document.getElementById("name").value);
      var item = escape(document.getElementById("item").value);
      var type = document.getElementById("weapon").value;
      //var latlng = marker.getPosition();
     var url= "http://localhost:8888/index.php/site_model/addMarker?name="+ name
    +"&item="+item+"&weapon="+weapon;

 downloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.responseText <= 1) {
          infowindow.close();
          document.getElementById("message").innerHTML = "Location added.";
        }

else
    {alert('NOT done');
    alert('TEST');
    }
      });

function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

Site_model中的addMarker Model函数

function addMarker()
    {
        $data = array(
            'name' =>$this->input->post('name'),
            'item' =>$this->input->post('item'),
            'lat' =>$this->input->post('lat'),
            'lng' =>$this->input->post('lng'),
            'weapon' =>$this->input->post('weapon'),
            'injured' =>$this->input->post('injured')
        );

        $insert = $this->db->insert('data', $data);
        return $insert;
    }

1 个答案:

答案 0 :(得分:0)

看起来你试图在uri中使用“$ _GET”查询字符串,但试图通过Codeigniter“$ _POST”访问来访问信息。

确保在application / config.php中启用了查询字符串

通常是config.php中的第151行

变化:

$config['enable_query_strings'] = FALSE;

要:

$config['enable_query_strings'] = TRUE;

这将使您能够使用查询字符串从网址中获取“$ _GET”信息。

另外,请确保在“site_model”的处理功能中使用

$this->input->get('name');

而不是

$this->input->post('name');