title: Ajax return value as a PHP variable
body: <p>I'm new in ajax, and i want to create ajax to return  value as a PHP variable,  i'm stuck trying to get the value from an ajax. I have the following form (view):</p>
  
  <pre><code>&lt;td&gt;
      &lt;input type="text" name="date_analysis[]" id="date_analysis" class="date_analysis input-group date" data-date="" data-date-format="dd-mm-yyyy" size="12" value="&lt;?php $a=set_value('date_analysis[0]'); echo $a; ?&gt;"/&gt;&lt;/td&gt;
  &lt;td&gt;
      &lt;input type="text" name="sulfur_ws[]" class="sulfur_ws" id="sulfur_ws" size="10" onkeyup="getValues()" value="&lt;?php $a=set_value('sulfur_ws[0]'); echo $a; ?&gt;"/&gt;&lt;/td&gt;
  &lt;td&gt;
      &lt;input type="text" name="sulfur_vnaoh[]" class="sulfur_vnaoh" id="sulfur_vnaoh" size="10" onkeyup="getValues()" value="&lt;?php $a=set_value('sulfur_vnaoh[0]'); echo $a; ?&gt;"/&gt;&lt;/td&gt;    
  &lt;td&gt;
      &lt;input type="text" name="sulfur_total[]" class="sulfur_total" id="sulfur_total" size="10" onkeyup="getValues()" value="&lt;?php $a=set_value('sulfur_total[0]'); echo $a; ?&gt;"/&gt;&lt;/td&gt;
  </code></pre>
  
  <p>Display:</p>
  
  <p><a href="https://i.stack.imgur.com/VnxHD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VnxHD.png" alt="enter image description here"></a></p>
  
  <p>Total field should be fillin automatically by formula: <strong>sulfur_vnaoh * aVariable * 32.03* 1000 / sulfur_ws</strong></p>
  
  <p><strong>aVariable</strong> is a value from ajax (the value is different for each date analys). Here is my code :</p>
  
  <p>Model Function:</p>
  
  <pre><code>function getdata_naoh($datetest){
      $result = $this-&gt;db1-&gt;query("select round(avg(cast(t_dtl.hasil_m as decimal(6,4))),4) as hasil_m from t_hdr JOIN  t_dtl ON t_hdr.headerid = t_dtl.headerid where id_solvent ='NaOH' AND status='ok' AND concentrate='0.0100' AND date_start &lt;='$datetest' AND date_finish &gt;='$datetest'");
      if($result-&gt;num_rows()&gt;0){
          return $result-&gt;result_array();
      }else{
          return array();
      }
  }
  </code></pre>
  
  <p>Controller Function:</p>
  
  <pre><code>function get_NaOH(){
      $date_analysis = trim($this-&gt;input-&gt;post('date_analysis'));
      $datetest = trim($this-&gt;input-&gt;post('date_analysis'));
      $datetest = substr($datetest,6,4).'-'.substr($datetest,3,2).'-'.substr($datetest,0,2);
  
      $dtnaoh = $this-&gt;M_tambahan-&gt;getdata_naoh($datetest);
  
      foreach($dtnaoh as $row){
          $data1 .= $row[hasil_m];
      }
  
      $data =  $data1;
      echo $data;
  }
  </code></pre>
  
  <p>and View javascript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function getValues() {
          var obj = document.getElementsByTagName('input');  
  
          for(var i=0; i &lt; obj.length; i++) {
            if (obj[i].name == "sulfur_vnaoh[]") { 
                var dt_sulfur_vnaoh = obj[i].value;
            };
            if (obj[i].name == "sulfur_ws[]") { 
                var dt_sulfur_ws = obj[i].value
            };
            if (obj[i].name == "sulfur_hasil[]") {
              if ((dt_sulfur_ws != '')&amp;&amp;(dt_sulfur_vnaoh != '')) {
  
  
                var totalSulfur = ((eval(dt_sulfur_vnaoh))*aVariable*32.03*1000/(eval(dt_sulfur_ws))).toFixed(2);
                obj[i].value = totalSulfur;
              } else {
                  obj[i].value = '';
              };
            };
          }
      };
  &lt;/script&gt;
  </code></pre>
  
  <p>// ajax script</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
       $(document).ready(function(){         
  
          $(document).on('change', function() {
              var that = $(this);
              console.log(that.next());
              var col_date_analysis =  that.parent().prev().find('.date_analysis');
              var date_analysis = col_date_analysis.val();
  
              $.ajax({
                        type :"post",
                        url : "&lt;?php echo base_url();?&gt;index.php/additionals/file/C_utils/get_NaOH",
                        data : { date_analysis: date_analysis },
                        success: function(data){
                            // code here
                        }
                    });
            });
          });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>If i run the model query, it will give result value like below image:</p>
  
  <p><a href="https://i.stack.imgur.com/p0UKi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p0UKi.png" alt="enter image description here"></a></p>
  
  <p>I want that value to become a variable <strong>aVariable</strong> to complete the formula.</p>
  
  <p>Any help would be appreciated greatly, thanks.</p>