Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-05-30
7
Medium Priority
?
268 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

636 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