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
Solved

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

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP and MSSQL Arrays and Variables 3 24
Accessing specific element - DOM PHP HTML 8 35
Button on Table, name table1 not working 4 22
2 separate CSS animations 2 17
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 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