开发者

Pass Javascript $_GET variables to CodeIgniter Model via AJAX

Good Morning, Im using the Google Maps V3 to implement a project.

In my view, an infowindow opens that the user input data. I want to save 开发者_开发知识库that data to my database.

My addMarker model function should grab the entered data and save it to my database. Currently, the infoWindow opens and I can enter information. But that data isn't sent to the addMarker function in my model.

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() {}

addMarker Model function in Site_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;
    }


It looks like you are attempting to use a "$_GET" querystring in the uri, but trying to access the info via Codeigniter "$_POST" access.

Make sure you have enabled querystrings in your application/config.php

which is usually line 151 in config.php

Change:

$config['enable_query_strings'] = FALSE;

To:

$config['enable_query_strings'] = TRUE;

This will enable you to use querystrings to grab "$_GET" information from the url.

Also, Make sure in your processing function in "site_model" you use

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

instead of

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜