Click here to Skip to main content
15,937,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to pass variables from javascript to php same page.
But I am not getting value.
I am using codeigniter.
I need value from item_id to $prod_id for query.

What I have tried:

var item_id =(item_obj=='') ? $('#div_'+id).attr('data-item-id') : item_obj.item_id;

info='<select class="form-control" id="warehouse_type_'+rowcount+'" name="warehouse_type_'+rowcount+'" ><?php 

		$prod_id = '<script>document.writeln(item_id)</script>';

		$userstore_id = $this->session->userdata('store_id');
		$query1="SELECT warehouse_id, SUM(quantity) as total_quantity, SUM(pending_quantity) as total_pending_quantity, SUM(quantity_pur_returned) as total_quantity_pur_returned, SUM(quantity_sold) as total_quantity_sold, SUM(quantity_sale_returned) as total_quantity_sale_returned FROM db_purchase_lines WHERE product_id = '$prod_id' and store_id = '$userstore_id' and supv_status = 'Approved' group by warehouse_id";
		echo '<option>select warehouse</option>';
		foreach($q1->result() as $res1){
			$warehouse_id = $res1->warehouse_id;
			$warehouse_name_n = $this->db->query("select warehouse_name from db_warehouse where id='$warehouse_id'")->row()->warehouse_name;
			$Avv_qnty = ($res1->total_quantity - $res1->total_pending_quantity - $res1->total_quantity_pur_returned - $res1->total_quantity_sold) + $res1->total_quantity_sale_returned;
			echo '<option value="'.$warehouse_name_n .'">'.$warehouse_name_n . ' - Qty(' . $Avv_qnty . ')' .'</option>';

str+='<td id="td_'+rowcount+'_304" class="text-right">'+ info +'</td>';
Updated 11-Dec-22 18:20pm
Member 15627495 8-Dec-22 12:08pm    
the only ways to achieve : submit , or asynchronous call
Ruaim Rabbie 8-Dec-22 12:53pm    
I am not clear. Please give me an example.

1 solution

Generally you do it either via Page submit (GET/POST) or via a XMLHttpRequest (async call).
There are more ways like Callback, Cookies, Session, etc too based on what makes more sense for you.

An example via page GET/POST & via Cookies: How to pass JavaScript variables to PHP ? - GeeksforGeeks[^]

An example on XMLHttpRequest: AJAX PHP[^]

An example with more ways:[^]

Try out and see which works for you more.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900