2016-01-13 77 views
0

这是一个以前问过的问题,但我需要严重的帮助,所以从朋友张贴它。我在这里有一个输入字段。从输入值到codeigniter控制器并初始化在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调用函数,但我不确定哪个应该起作用。

回答

1

由于您从表单发布,最好使用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'; 
     } 

希望这会做到这一点。