从 from 获取输入值到 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 €">
<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();
}
我现在要做的是使用第一页中的值,并在完全不同的页面中的 knockout 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;">
我有一个knockout 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 €" 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>
最后,现在在你的 knockout js 文件中,这样写。
self.amount = ko.observable($('#Amount').data('value'));
self.formatter1 = function(amount) {
return amount + ' kk';
}
希望这能奏效。
这是一个以前问过的问题,但我在这方面需要认真的帮助,所以从朋友那里重新发布。我这里有一个输入框。
<form action="index.php/money/save_userinput" method="post" >
<input type="text" placeholder="Amount in €">
<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();
}
我现在要做的是使用第一页中的值,并在完全不同的页面中的 knockout 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;">
我有一个knockout 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 €" 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>
最后,现在在你的 knockout js 文件中,这样写。
self.amount = ko.observable($('#Amount').data('value'));
self.formatter1 = function(amount) {
return amount + ' kk';
}
希望这能奏效。