Solved

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

Posted on 2013-05-30
7
262 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now