Note that there are some explanatory texts on larger screens.

plurals
  1. POCould not pass array value from Javascript to Codeigniter controller
    primarykey
    data
    text
    <p>In my HTML, when a user selects from a dropdown option, the selected item is stored in a Javascript variable. I'm trying to pass this value(s) to my controller variable in CodeIgniter, but it fails.</p> <p><strong>Here's my HTML code:</strong></p> <pre><code>&lt;form name="change_app_formTrader" id="change_app_formTrader"&gt; &lt;label&gt;Approver Name&lt;/label&gt; &lt;select name="app_id[]"&gt; &lt;option value="0"&gt;select approver&lt;/option&gt; &lt;option value="approver1"&gt;Approver One&lt;/option&gt; &lt;option value="approver2"&gt;Approver Two&lt;/option&gt; &lt;option value="approver3"&gt;Approver Three&lt;/option&gt; } ?&gt; &lt;/select&gt; &lt;/form&gt; &lt;input type="button" onclick="save_appchangeteto()" value="Save Changes"/&gt; </code></pre> <p><strong>Here's my javascript code for save_appchangeteto():</strong></p> <pre><code>var tetoApprover; var appword; function save_appchangeteto(){ tetoApprover = []; appword = ''; var appid = 0; $('form[name="change_app_formTrader"]').find(':input').each(function(){ appid = $(this).val(); appword = this.options[this.selectedIndex].text; push_approverteto(appid,appword); }); populate_approverteto(); } /*save_appchangeteto()*/ function push_approverteto(appid,appword){ tetoApprover.push({ 'appid':appid, 'appword':appword }); } function populate_approverteto(){ var htm = ''; var x = 0; $.each(tetoApprover,function(){ x+=1; htm += '&lt;tr&gt;'; htm += '&lt;td&gt;'+x+'&lt;/td&gt;'; htm += '&lt;td&gt;'+this.appword+'&lt;/td&gt;'; htm +='&lt;td&gt;&lt;/td&gt;'; htm +='&lt;td&gt;&lt;/td&gt;'; htm += '&lt;/tr&gt;'; }); $('#approverTblTrader').html(htm); } /*populate_approverteto*/ </code></pre> <p><strong>Then when the user clicks the submit button, it executes the below code:</strong></p> <pre><code>$('#submitTradersRequest').click(function(){ $('.rc-loader').html('&lt;img src="'+BASE_URL+'assets/img/ajax-loader.gif"/&gt;&amp;nbsp;Please wait..'); $(this).prop('disabled',true); tetoApprover = (tetoApprover.length&lt;1)?1:tetoApprover; $.ajax({ url: BASE_URL+'create/process', type: "POST", data: {approvers:tetoApprover}, success: function(data){ var response = $.parseJSON(data); if(response.status!='Success'){ $('.rc-loader').html('&lt;span class="alert alert-error"&gt;'+response.msg+'&lt;/span&gt;'); } } }).fail(function() { alert('Error'); }); $(this).prop('disabled',false); }); </code></pre> <p><strong>But in my controller, the <code>tetoApprover</code> was not passed at all. The code executed was the one inside the else below, which means the variable for approvers has no value at all:</strong></p> <pre><code>$post = $this-&gt;input-&gt;post(); if($post['approvers']){ foreach($post['approvers'] as $val){ $count++; $status = 0; $token = sha1(rand().date('m/d/y')); if($count==1){ $status = 1; } $params = array('scalar'=&gt;array( $reqid, $val['appid'] ) ); $this-&gt;create_model-&gt;updatestatus($params); } } else { //this is where the process currently goes $val['appid'] = 4; //test $params = array('scalar'=&gt;array( $reqid, $val['appid'] )); $this-&gt;create_model-&gt;updatestatus($params); } </code></pre> <p>I've been working on this for days, but could not find where I went wrong. Please help.</p> <p><strong>UPDATE:</strong></p> <p>I have confirmed that <code>push_approverteto()</code> is working properly because <code>populate_approverteto()</code> is successfully updating the list of approvers on the display.</p> <p>In other words, the values are successfully saved in the variable <code>tetoApprover</code>.</p> <p>However, it fails to be passed to the controller variable <code>$post['approvers']</code>. That is my main problem because i could not save the user-given values into my database.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload