从获取输入值到codeigniter控制器,并在knockout.js中初始化


Getting input value from from to codeigniter controller and initializing in knockout.js

这是以前问过的一个问题,但我需要认真的帮助,所以从朋友那里重新发布。我在这里有一个输入字段。

<form  action="index.php/money/save_userinput" method="post" >
            <input type="text" placeholder="Amount in &euro;">
            <a  type="submit" style="background-color:#fff; color:#66cccc;" href="<?php echo base_url();?>index.php/money/firstpage">GetCash</a> 
  </form>

然后,我尝试将这个值从输入字段获取到控制器中,该控制器在调试时没有接收到任何值。这就是函数。

public function save_userinput(){
        $this->load->helper('form');
        $form_data = $this->input->post();
    }

我现在要做的是使用第一页中的值,并在一个完全不同的页面中的淘汰js滑块中获取这些值。这是滑块代码。

<input id="ex2" data-slider-id="ex2Slider" type="text" data-bind="sliderValue: {value: amount, min:0, max: 5000, step: 1, tooltip: 'always', formatter:formatter2}"
            style="display: none;">

我有一个淘汰js页面,它有功能

self.amount = ko.observable(2500);
        self.formatter1 = function(amount) {
          return amount + ' kk';
        }

我需要把我从控制器得到的值放入可观察值中,但我根本不确定如何做到这一点,我尝试了不同的方法,但都不起作用。我曾想过用Ajax调用该函数,但我不确定哪一个应该起作用。

由于您是从表单发布的,最好使用php并像这样回显它,并且您不能使用<a>标记,需要是<input>,否则它将无法提交,并且您需要在输入字段中有一个名称。

<?php echo form_open('money/save_userinput');  ?>
    <input  type="text" placeholder="Amount in &euro;"  name="writtenamount"/>
    <input type="submit" value="invest"/>
    <?php echo form_close();?>

在您的代码点火器控制器中,创建一个数据数组来获得这样的变量。

public function invest_first_page(){
        $this->load->helper('form');
        $userProvidedAmount = $this->input->post("writtenamount");
        $data =  array(
        'userProvidedAmount' => $userProvidedAmount
         );
        $this->load->view("yoursecondpage", $data);
        }

在你的第二页中,你有输入滑块,这样写你的代码。

<input id="ex2" data-slider-id="ex2Slider" type="text"
 data-bind="sliderValue: {value: amount, min:0, max: 5000, step: 1, tooltip: 'always', formatter:formatter2}"
 style="display: none;">
<span id="Amount" data-value="<?php echo $userProvidedAmount ; ?>"</span>

最后,现在在你的淘汰js文件中,这样写。

self.amount = ko.observable($('#Amount').data('value'));
        self.formatter1 = function(amount) {
          return amount + ' kk';
        }

希望这能奏效。