• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

load Products in a select menu everytime I add a row using ajax

Hello Experts...

Im making a invoice program and using strictly Javascript...  but im having problems getting the products to load in the Select menu, The first row is okay cause its hard coded on the template, but when I click add row everythng else is dynamically added to the page and im using ajax to populate the select menu. Can I ask someone to go over my code and let me know what im missing or doing wrong, maybe a better way to acheive my goal?? Thanks Guys cant wait to see what im missing... its buggin me!!


Here is the Javascript Function that adds the row on the template
function add_row(newid) {
	var container 		= document.getElementById('newProductRow');
	var product_row		= document.createElement('div');
		product_row.id 	= "product_row3";
		container.appendChild(product_row);
		
	var productSelect 				= document.createElement('div');		
		productSelect.setAttribute('id', 'product_name');
		productSelect.setAttribute('class', 'product_name');
		productSelect.style.width 	= "225px";
		productSelect.setAttribute('style', 'display = inline-block');
		productSelect.style.border	= "0px solid black";
		product_row.appendChild(productSelect);
		
	var select						= document.createElement('select');
		select.id					= "products";
	var option						= document.createElement('option');
		option.innerHTML = "Product Selection";
		connect_ajax();
		xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			
			for (i=0; i < xmlhttp.responseText.length; i++)
				{
				option.innerHTML = xmlhttp.responseText[i]['options'];
				} 
			}
		}
			select.appendChild(option);
	var file   = "http://" + u + "/members/accounting";
	var values = "show=insert_options";	
	var uandp = values;
	xmlhttp.open("POST", file, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(uandp);
			
		productSelect.appendChild(select);
		
	var description 				= document.createElement('div');
		description.setAttribute('id', 'description');
		description.setAttribute('class', 'description');
		description.setAttribute('style', 'width=430px');
		description.style.display 	= "inline-block";
		description.style.fontFamily= "metro";
		description.innerHTML 		= "Description";
		product_row.appendChild(description);
}

Open in new window



Here is the php code that pulls the info from the database to send to the javascript function

if ($show == 'insert_options')
	{
	$statement = "SELECT id, product_name FROM products_services";
	$database	= new explore('accounting');
	$products = $database->fetch($statement);
	$encode = array();
	for ($i=0; $i < count($products); $i++)
		{
			$encode[$i] = '<option value=' . $products[$i]['id'] . '>' . $products[$i]['product_name'] . '</option>';
		}
		echo $encode;
}				

Open in new window


This is all the code I have to make this function work so if im missing something please let me know! Thanks again
0
Easyrider43
Asked:
Easyrider43
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Read this : https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

You need to do a synchronous ajax call or use a different logic

Using jQuery : http://jsfiddle.net/YPaRY/
function add_row(newid) {
	var html = "";
	html += "<div id='product_row3'>"
	html += "<div id='product_name' class='product_name' style='width:225px;display:inline-block;0px solid black'><select id='products></select></div>";
	html += "<div id='description' class='description' style='width=430px;inline-block;font-family:metro'>Description</div>";
	html += "</div>";

    $('#newProductRow').append(html).find("select#product").load("/echo/html/", {"html":"<option>1</option><option>2</option>"}).prepend("<option>Product Selection</option>");
}

Open in new window

0
 
Easyrider43Author Commented:
I see where your going with that but all your inserting and such is done inside the javascript, what im trying to do is pull info out of the database and loop through each line using php to give it a Id and then send it over to javascript, also can you not use Jquery? I need it to be strictly javascript. Thanks :)
0
 
leakim971PluritechnicianCommented:
but all your inserting and such is done inside the javascript, what im trying to do is pull info out of the database and loop through each line using php to give it a Id and then send it over to javascript

Open in new window

No, I can't do a true ajax call to get data from a database with jsfiddle
SO for the test page you see the data
Line 8, just remove {"html":"<option>1</option><option>2</option>"}
and replace "/echo/html/" by "http://" + u + "/members/accounting"

The true information in my post come in the first line, you need to understand the difference between sync and async ajax call
0
 
Easyrider43Author Commented:
okay... I understand what your saying now, except how can I do this with just regular javascript and not jquery??
0
 
leakim971PluritechnicianCommented:
what is the problem with jquery?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now