Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i am not sure i understand the question that well. if you are doing ajax posting and dynamic updating of the form, you may want to consider using the json_encode (php) and $.parseJson (jQuery) to send the parameters back to the client. that way you can send a ready-made html straight to the browser id.</p> <p>The cities function in javascript is a bit confusing to me. the parameters are: success:function (data, textStatus, jqXHR)</p> <p>the ajax.post function parameters are: jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ) so, the data is something like: var data = {product_id:$('#product_id').val()}; this will send the data through as a POST variable, instead of as a GET variable as is the case in your code.</p> <p>some comments: in the view, end the change the line:</p> <pre><code>&lt;form action="&lt;?php echo base_url(); ?&gt;welcome/add" method="post"&gt; </code></pre> <p>into:</p> <pre><code>&lt;form action="&lt;?php echo base_url('index.php'.DIRECTORY_SEPARATOR.'welcome'.DIRECTORY_SEPARATOR.'add'); ?&gt;" method="post"&gt; </code></pre> <hr> <p>in the model, don't have the ?> closing tag, this all becomes: (which worked for me!)</p> <p>controller:</p> <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends CI_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;model('welcome_model'); } public function index() { $data = array(); $data['category'] = $this-&gt;welcome_model-&gt;select_category(); $data['product'] = $this-&gt;welcome_model-&gt;select_product(); $this-&gt;load-&gt;view('purchase_entry', $data); } function get_products($category) { header('Content-Type: application/x-json; charset=utf-8'); echo(json_encode($this-&gt;welcome_model-&gt;select_product_by_category($category))); } function add() { $result = $this-&gt;welcome_model-&gt;select_product_by_id($this-&gt;input-&gt;post('product_id', TRUE)); $insert = array( 'id' =&gt; $this-&gt;input-&gt;post('product_id', true), 'qty' =&gt; $this-&gt;input-&gt;post('quantity', true), 'price' =&gt; $this-&gt;input-&gt;post('unit_price', true), 'name' =&gt; $result-&gt;product_name ); $this-&gt;cart-&gt;insert($insert); redirect("welcome/index"); } } </code></pre> <p>model:</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome_Model extends CI_Model { public function select_category() { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('category'); $query_result= $this-&gt;db-&gt;get(); $result=$query_result-&gt;result(); return $result; } public function select_product() { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('product'); $query_result= $this-&gt;db-&gt;get(); $result=$query_result-&gt;result(); return $result; } public function select_product_by_category($category_id) { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('product'); $this-&gt;db-&gt;where('category_id',$category_id); $query_result= $this-&gt;db-&gt;get(); $result=$query_result-&gt;result(); return $result; } public function select_product_by_id($product_id) { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('product'); $this-&gt;db-&gt;where('product_id',$product_id); $query_result= $this-&gt;db-&gt;get(); $result=$query_result-&gt;row(); return $result; } } </code></pre> <p>view</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript" src="&lt;?php echo base_url(); ?&gt;js/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $('#category_id').change(function(){ $("#product_id &gt; option").remove(); var category_id = $('#category_id').val(); $.ajax({ type: "POST", url: "&lt;?php echo base_url('index.php/welcome/get_products/').'/';?&gt;"+category_id, success: function(cities) { $.each(cities,function(id,name) { var opt = $('&lt;option /&gt;'); opt.val(id.product_id); opt.text(name.product_name); $('#product_id').append(opt); }); } }); }); }); &lt;/script&gt; &lt;style type="text/css"&gt; a{text-decoration: none;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="&lt;?php echo base_url('index.php'.DIRECTORY_SEPARATOR.'welcome'.DIRECTORY_SEPARATOR.'add'); ?&gt;" method="post"&gt; &lt;table width="400px" align="center"&gt; &lt;caption&gt;&lt;h3&gt;Purchase Invoice&lt;/h3&gt;&lt;/caption&gt; &lt;tr&gt; &lt;td&gt;Invoice Date :&lt;/td&gt; &lt;td&gt; &lt;input type="text" name="invoice_date" value=""/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Select Category :&lt;/td&gt; &lt;td&gt; &lt;select name="category_id" id="category_id"&gt; &lt;option&gt;--- Select Category ---&lt;/option&gt; &lt;?php foreach ($category as $aCategory) { ?&gt; &lt;option value="&lt;?php echo $aCategory-&gt;category_id; ?&gt;"&gt;&lt;?php echo $aCategory-&gt;category_name; ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Select Product :&lt;/td&gt; &lt;td&gt; &lt;select name="product_id" id="product_id"&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Unit Price :&lt;/td&gt; &lt;td&gt; &lt;input type="text" name="unit_price" value=""/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Quantity :&lt;/td&gt; &lt;td&gt; &lt;input type="text" name="quantity" value=""/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt; &lt;input type="submit" value="Submit"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;form action="" method="post"&gt; &lt;h2 align="center"&gt;Invoice Details&lt;/h2&gt; &lt;table border="1px" cellpadding="5px" cellspacing="1px" style="margin-top:10px;border-collapse:collapse" width="500px" align="center"&gt; &lt;tr align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;"&gt; &lt;th&gt;Serial&lt;/th&gt; &lt;th&gt;Product Name&lt;/th&gt; &lt;th&gt;Quantity&lt;/th&gt; &lt;th&gt;Unit Price&lt;/th&gt; &lt;th&gt;Amount&lt;/th&gt; &lt;th&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;?php if ($cart = $this-&gt;cart-&gt;contents()) ?&gt; &lt;?php $i = 1; foreach ($cart as $item) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $i++; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $item['name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $item['qty']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $item['price']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $item['subtotal']; ?&gt;&lt;/td&gt; &lt;td&gt; &lt;a href=""&gt;Update&lt;/a&gt;&amp;nbsp;|&amp;nbsp;&lt;a href=""&gt;Remove&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload