您好,我正在使用下面的 jquery 将 post 请求发送到 codeigniter 中的 PHP Controller ,但是当我将值从 Controller 传递到模型作为参数时,我看到的是查询的结果是空的,并且传递的值是空的,但是查询直接在mysql中工作正常。
所以问题是在 Controller 中获取值后,当我将其作为参数传递给我的模型并在查询中传递该值时,没有出现结果,但应该出现。
我的 javascript 函数在文档加载时被调用:
function stockUpdate(symbol)
{
$.post("stock/stock_update",
{
symbol: symbol
},
function success(data)
{
alert(data);
}
});
}
我的 PHP Controller :
class Stock extends CI_Controller {
function stock_update()
{
// get symbol
$symbol = $this->input->post('symbol');
// get the price for symbol and calculate unit price
$result = $this->user_stock_model->get_price($symbol);
print_r($result); // nothing gets printed
}
}
我的 user_stock_model:
class User_stock_model extends CI_Model {
public function get_price($symbol)
{
// get user id
$id = $this->session->userdata('id');
echo $symbol; // value gets printed
// fetch total price where symbol as given
$sql = "SELECT shares, price FROM users_stock ".
"WHERE id = ? and symbol = ?";
$query = $this->db->query($sql, array($id, $symbol));
return $query->row_array(); // empty results
}
}
由于符号变量在此函数中不包含任何值,因此我的查询结果为空。
我还想告诉大家的是,如果我从 Chrome 开发者工具控制台运行完全相同的 jquery 脚本,它绝对可以正常工作。
最佳答案
看起来在发送请求之前(即在 document.ready 时)未设置变量“symbol”。您是否尝试在 $.post 之前记录“符号”?
关于javascript - jquery 使用 codeigniter 框架发布数据在 PHP 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25006088/