Search code examples
phpfunctioncodeigniteronkeyup

number convert into words onkeyup in codeigniter


I will try to convert Number into words using onkeyup event in CodeIgniter.

not necessary to read more description, I just add because StackOverflow does not add my question because of less description.

It's my view file

   <label for="name" class="field-wrapper required-field">Enter Amount of 
    Value:</label>
       <?php echo 

   form_input(['name'=>'price','id'=>'price','onkeyup'=>'myFunction()']); ?>
       <div class="col-lg-10">
      <?php echo form_error('price',"<p class='text-danger'>","</p>");?>
      </div>
        <input type="submit" value="Conver Number to Words" name="convert"/>
    </div>
    <div class="form-group">
      <fieldset>
        <label class="control-label" for="readOnlyInput">In Words:<br><span 
     id="demo"></span> </label>

      </fieldset>
    </div>

    <script>
    function myFunction() {
        var x = document.getElementById("price").value;
        document.getElementById("demo").innerHTML = x;
    }
    </script>

now, the number convert into words function is in my Controller, How can I send a value of price & return into x?

It's my controller:

 public function pay_number($price)
  {
      $ones = array(
      1 => "one",
      2 => "two",
      3 => "three",
      4 => "four",
      5 => "five",
      6 => "Six",
      7 => "seven",
      8 => "eight",
      9 => "nine",
      10 => "ten",
      11 => "eleven",
      12 => "twelve",
      13 => "thirteen",
      14 => "fourteen",
      15 => "fifteen",
      16 => "sixteen",
      17 => "seventeen",
      18 => "eighteen",
      19 => "nineteen"
      );
      $tens = array(
      2 => "Twenty",
      3 => "Thirty",
      4 => "Forty",
      5 => "Fifty",
      6 => "Sixty",
      7 => "Seventy",
      8 => "Eighty",
      9 => "Ninety"
      );
      $hundreds = array(
      "hundred",
      "thousand",
      "million",
      "billion",
      "trillion",
      "quadrillion"
      ); //limit t quadrillion
        $price = number_format($price,2,".",",");
        $num_arr = explode(".",$price);
        $wholenum = $num_arr[0];
        $decnum = $num_arr[1];
        $whole_arr = array_reverse(explode(",",$wholenum));
        krsort($whole_arr);
        $rettxt = "";
        foreach($whole_arr as $key => $i)
        {
          if($i < 20)
          {
          $rettxt .= $ones[$i];
          }
          elseif($i < 100)
          {
            $rettxt .= $tens[substr($i,0,1)];
            $rettxt .= " ".$ones[substr($i,1,1)];
          }
          else
          {
              $rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
              $rettxt .= " ".$tens[substr($i,1,1)];
              $rettxt .= " ".$ones[substr($i,2,1)];
          }
            if($key > 0)
            {
              $rettxt .= " ".$hundreds[$key]." ";
            }
        }
          if($decnum > 0)
          {
            $rettxt .= " and ";
            if($decnum < 20)
            {
              $rettxt .= $ones[$decnum];
            }
            elseif($decnum < 100)
            {
              $rettxt .= $tens[substr($decnum,0,1)];
              $rettxt .= " ".$ones[substr($decnum,1,1)];
            }
        }
        echo $rettxt;
    }

Thank you for help :)


Solution

  • You can use jquery load(..) with that.

    var x = document.getElementById("price").value;
    $("#demo").load( "controller/pay_number/"+x );