【问题标题】:Getting input value from from to codeigniter controller and initializing in knockout.js从from获取输入值到codeigniter控制器并在knockout.js中初始化
【发布时间】:2016-01-13 13:43:10
【问题描述】:

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

<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';
        }

我需要将从控制器获得的值放入 observable 中,但我完全不知道该怎么做,我尝试了不同的方法,但没有奏效。我正在考虑使用 Ajax 调用该函数,但我不确定哪个应该工作。

【问题讨论】:

    标签: javascript php codeigniter knockout.js knockout-2.0


    【解决方案1】:

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

    <?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';
            }
    

    希望这能解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多