从控制器检索 Ajax 数据

问题描述 投票:0回答:1
您好,我已在 Codeigniter 控制器中调用了数据库值,并计算了数据库值并将其存储在一个新变量中。那么,我如何获取数据库值和在控制器中创建的新变量,因为我想在视图中显示所有数据库值和新的计算值。请推荐我。

我的控制器:欢迎

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public $total; public function __construct() { parent::__construct(); $this->load->library('table'); $this->load->model('product_database'); } public function index() { $data['show_table'] = $this->view_table(); $this->load->view('welcome_message', $data); } public function view_table(){ $result = $this->product_database->show_all_data(); if ($result != false) { return $result; } else { return 'Database is empty !'; } } public function AddtoCart(){ $id = $this->input->post('product_id'); $qty = $this->input->post('qty'); $this->db->where('id', $id); // Select where id matches the posted id $query = $this->db->get('productlist', 1); // Select the products where a match is found and limit the query by 1 foreach ($query->result() as $row) { $data = array( 'id' => $id, 'qty' => $qty, 'price' => $row->price, 'name' => $row->name ); $total=$qty*$row->price; echo $total; } } } ?>

我的观点:welcome_message

<!DOCTYPE HTML> <html> <head> <title>Product list</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script> <script type="text/javascript"> $(document).ready(function() { $("#add").click(function(event) { event.preventDefault(); jQuery.ajax({ type: "POST", url: "<?php echo base_url(); ?>" + "index.php/Welcome/AddtoCart", dataType: 'json', data: {name: }, success: function(res) { if (res) { alert(); // Show Entered Value jQuery("#total").html(res.total); } } }); }); }); </script> </head> <body> <div class="container"> <div class="message"> <?php if (isset($read_set_value)) { echo $read_set_value; } if (isset($message_display)) { echo $message_display; } ?> </div> <div> <?php if (isset($show_table)) { echo "<div class='productlist'>"; if ($show_table == 'Database is empty !') { echo $show_table; } else { echo '<h2>Product List</h2><br/><br/>'; ?> <div class="row"> <div class="col-sm-8"> <div class="well"> <div class="table-responsive"> <?php echo "<table width='98%', >"; echo '<tr><th class="e_id">Id</th><th>ProductName</th> <th>Price</th> <tr/>'; $i=1; foreach ($show_table as $value) { ?> <tr class="well" > <?php echo "<td width='30%' height='27px'>" . $value->id . "</td>" . "<td width='70%' height='27px'>" . $value->name . "</td>" . "<td height='27px'>" . $value->price . "</td>"; ?> <?php echo form_open('/Welcome/AddtoCart'); ?> <td><input type="number" name="qty" value="1" style="width:40px;" min="1" max="99"></td> <input type="hidden" name="product_id" value="<?php echo $value->id ?>" /> <td><input type="submit" value="Add" id="add" width="100%"></td> <td><input type="submit" value="Rmv" id="rmv" width="100%"></td></br> <?php echo form_close(); ?> </tr> <?php $i=$i+1;} echo '</table>'; ?> </div> </div> </div> <div class="col-sm-4"> <div class="well"> <h4>Cart</h4> Total Rs:<input type="text" value="" id="total"></br><br> <input type="submit" value="Checkout" id="chk_out"> </div> </div> </div> <?php } echo "</div>"; } ?> </div> </div> </body> </html>

还有我的模特

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Product_database extends CI_Model { public function show_all_data() { $this->db->select('*'); $this->db->from('productlist'); $query = $this->db->get(); if ($query->num_rows() > 0) { return $query->result(); } else { return false; } } } ?>
    
php ajax codeigniter
1个回答
0
投票
您可以用以下代码代替

echo $total

 :

$total= array('total'=> $total); $youarray=array_merge($data, $total); echo json_econde($youarray,true);
    
© www.soinside.com 2019 - 2024. All rights reserved.