Solved

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

Posted on 2013-05-30
7
266 Views
Last Modified: 2013-10-10
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
Comment
Question by:Easyrider43
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39209648
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
 
LVL 1

Author Comment

by:Easyrider43
ID: 39209698
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39209765
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
 
LVL 1

Author Comment

by:Easyrider43
ID: 39209862
okay... I understand what your saying now, except how can I do this with just regular javascript and not jquery??
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39210389
what is the problem with jquery?
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question