title: Usng JSON in codeigniter with form submit
body: <p>Im trying to learn how to use JSON from within codeigniter.  I'm trying to use a model to run the ajax code, but its not working.  </p>
  
  <p>I need to learn how to pass variables through to the model, and back out; or, if thats incorrect, I need to learn the correct process. The below is my code.</p>
  
  <p><strong>HTML</strong>  </p>
  
  <pre><code>&lt;div id="structures"&gt;
      &lt;h1&gt;Build&lt;/h1&gt;
      &lt;form name="buildForm" id="buildForm" method="POST"&gt;
          &lt;select name="buildID" class="buildClass"&gt;
          &lt;option value="0" selected="selected" data-skip="1"&gt;Build a Structure&lt;/option&gt;
      &lt;option name='Town Center' value='1' data-icon='../img/structures/tc.png' data-html-text='Town Center&lt;i&gt;
          500 minutes&lt;br&gt;50000 gold&lt;/i&gt;'&gt;Town Center&lt;/option&gt;
      &lt;option name='Barracks' value='2' data-icon='../img/structures/barracks.png' data-html-text='Barracks&lt;i&gt;
          25 minutes&lt;br&gt;1500 gold&lt;/i&gt;'&gt;Barracks&lt;/option&gt;
      &lt;option name='Dragon Roost' value='3' data-icon='../img/structures/droost.png' data-html-text='Dragon Roost&lt;i&gt;
          200 minutes&lt;br&gt;5000 gold&lt;/i&gt;'&gt;Dragon Roost&lt;/option&gt;
      &lt;option name='Mage Hall' value='4' data-icon='../img/structures/mage.png' data-html-text='Mage Hall&lt;i&gt;
          40 minutes&lt;br&gt;300 gold&lt;/i&gt;'&gt;Mage Hall&lt;/option&gt;
      &lt;option name='Test Lab' value='6' data-icon='../img/structures/testlab.png' data-html-text='Test Lab&lt;i&gt;
          1 minutes&lt;br&gt;10 gold&lt;/i&gt;'&gt;Test Lab&lt;/option&gt;
          &lt;/select&gt;
          &lt;div id="buildSubmit"&gt;
              &lt;input id ="btnSubmit" class="button" type="submit" value="Submit"/&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Here is my ajax/js/json</strong>
  I'm trying a very simple example.  I'd like to post the value of the OPTION above (1-6) to the model function insert_build.  I don't know if its doing it, as I can't really think of a good way to test it.  However, I would assume if it returned anything, I would be alerted.  I am not.</p>
  
  <pre><code>$(function(){
      $(".button").click(function(e, value){
          e.preventDefault();
  
          $.ajax({
          type: "POST",
          url: "&lt;?php $this-&gt;structure_model-&gt;insert_build() ?&gt;",         //the script to call to get data          
          str_id: value,
          dataType: 'json',                //data format      
          success: function(data)          //on receive of reply
              {
                  alert("success!");
              } 
          });
   });
  });
  </code></pre>
  
  <p><strong>The model code</strong></p>
  
  <pre><code> public function insert_build()
          {
              $str_id = $this-&gt;input-&gt;post('str_id');
              echo " TESTING $str_id";
          }
  </code></pre>
  
  <p>Any help would be greatly appreciated.  Why doesn't this work?  I am still trying to understand the fundamentals of Codeigniter and JSON / JS.  </p>
  
  <p>Thanks</p>