We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 2 of Article: struct issue
Hi I was following a tutorial on sitepoint for creating structs. I must be missing something since I followed the code exactly, but keep getting an error! Here is the code: <cfset myBook= structNew()> <cfset a = StructureInsert(myBook, \\"title\\", \\"All About Coldfusion\\", 1)> <cfset a = StructureInsert(myBook, "author", "Teed Younger",…

Your choice. I think you're totally misunderstanding how the example's supposed to work, lol. Forget about the code for a sec and think about what the 2 pieces of code are doing conceptually.

You're trying to do it old style
page1.cfm => go to => page2.cfm to see preview

The whole point of ajax is to avoid leaving the page. So users can see preview changes quickly, and you don't have to worry about maintaining the state form page to page. Technically you still send data to page2.cfm. But you display the results on the original page. It's no more complex than updating the content of an element with javascript.

document.getElement('myDiv').innerHTML = "See I just changed the content!";
arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

Well I know ajax does just that, it changes page data or whatever without making a new request to the server.


So with that said, I went back to the original example he gave me, changed the action.cfm to my builderAction.cfm and ran the script from there. Works just fine like that. It "adds" the list items like it should and "loads" my builderAction.cfm without a page refresh.

So, why then couldnt I take the code...jquery/ajax...out of that page, paste it into my template, using the same builderAction.cfm "action" within the ajax function, and display it that way.

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

lol. I didn't mean to sound like you don't know how ajax works. Just that you weren't using the form as it was intended to work. ie You were using a submit button to do a plain form post the old way. Instead of a plain button to do an ajax call (method=get) and display the content on the same page.

So, why then couldnt I take the code...jquery/ajax...out of that page, paste it into my template, using the same builderAction.cfm "action" within the ajax function, and display it that way.

Sorry .. I'm brain dead today. Not sure what you mean by template. ie Are you talking about having everything (jquery and action) on the same page? That should work as long as you structure it correctly.

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

lol no I wasn't trying to sound like you were trying to sound like I didnt know what ajax was for...lol

Sorry .. I'm brain dead today. Not sure what you mean by template. ie Are you talking about having everything (jquery and action) on the same page? That should work as long as you structure it correctly.

I think maybe therein lies the problem. My form submit button.

let me post the relavant parts of my template.cfm page. I guess when I said template, that was confusing. I should probably change that name.

Here is how I thought this would work with my CURRENT page and action page.

Here is the jQuery code. Notice the ajax function and the action page file its calling.

<script src="http://www.google.com/jsapi"></script>
<script> 
// Load jQuery
google.load("jquery", "1.4.2");
 
google.setOnLoadCallback(function(){
	$(document).ready(function(){
		$('.add-product').live('click', function(){
			var productContainer = $(this).parents('.product');
			var newProduct = $(productContainer).clone();
			$(newProduct).find('.product-input').val('');
			
			var vendorProductContainer = $(productContainer).parents('.vendor-products');
			$(vendorProductContainer).append(newProduct);
			$(productContainer).find('.add-product').hide();
						
			return false;
		});
		$('.add-vendor').live('click', function(){
			var vendorContainer = $(this).parents('.vendor');						
			//clone the vendor
			var newVendor = $(vendorContainer).clone();
			$(newVendor).find(':input').val('');
			
			//remove all but the first products
			$(newVendor).find('.product:gt(0)').remove();
			
			$(vendorContainer).after(newVendor);
			$(newVendor).find('.add-product').show();
			$(vendorContainer).find('.add-vendor').hide();
			return false;
		});
		
		$('#submit').click(function(){
			$('#results').html('Loading...');
			
			var items = {};
			
			$('.product-grouping').each(function(i,e){
				var title = $(e).find('legend').html();
				items[title] = {};
				items[title]['category'] = $(e).find('.category').val();
				items[title]['vendors'] = {};
				$(e).find('.vendor').each(function(vi,ve){
					var products = [];
					var v = $(ve).find('.vendor-input').val() || 'vendor ' + (vi+1);
					
					$(ve).find('.product-input').each(function(pi,pe){
						products.push($(pe).val());
					});
					items[title]['vendors'][v] = products;
				});
				
			});
			
			$.ajax({
				type: 'GET',
				url: 'pageBuilder/builderAction.cfm?data=' + JSON.stringify(items),
				dataType: 'html',
				complete: function(data){
					$('#results').html(data.responseText);
				}
			});
							
		});
		
		$('#add-item').click(function(){
			var item = $('.product-grouping:last').clone();
			$(item).find('.vendor:gt(0)').remove();
			$(item).find('.product:gt(0)').remove();
			$(item).find(':input').val('');
			$(item).find('.add-product').show();
			$(item).find('.add-vendor').show();
			var itemIdx = $('.product-grouping').size() + 1;
			$(item).find('legend').html('Item ' + itemIdx);
			$('#items').append(item);
		});
		
	});
});
</script>

here is the form code on the same page template.cfm:

<form method="POST">
		<div id="items">
			<fieldset class="product-grouping">
				<legend>Item 1</legend>
											
				<div class="vendor" style="padding-left: 30px;">
					<span>Vendor: </span>
					<input type="text" class="vendor-input" name="vendor" />
					<a href="#" class="add-vendor">add vendor</a>
					
					<div class="vendor-products" style="padding-left: 60px;">
						<div class="product">
							<span>Product: </span>
							<input type="text" class="product-input" name="product" />
							<a href="#" class="add-product">add product</a>
						</div>
					</div>
				</div>
				
			</fieldset>
		</div>
		
		<input type="button" id="add-item" value="add item" />
		<input type="button" id="submit" value="submit" />
	</form>

Now one thing I will mention is that I have other form elements on this page. Therefore, I think as you suggested, if I am trying to submit SOME form input elements as a regular form action post, and I have this ajax function that gets the data and displays it via a javascript button, I guess thats going to be in conflict.

Then finally here is my cfloop displaying the data on builderAction.cfm:

<cfoutput>
          <cfset results = deserializeJSON(url.data)>
          <!--- <cfdump var="#results#"> --->
          <cfloop collection="#results#" item="itemName">
            <cfset item = results[itemName]>
            <cfdump var="#item#" label="Item Name #itemName#">
            <cfloop collection="#item.vendors#" item="vendorName">
              <ul>
                <li>
                  <h2>#vendorName#</h2>
                </li>
                <cfset productArray = item.vendors[vendorName]>
                <!--- <cfdump var="#productArray#" label="Products for vendor: #vendorName#"> --->
                <cfloop array="#productArray#" index="productName">
                  <li>#productName#</li>
                </cfloop>
              </ul>
            </cfloop>
          </cfloop>
        </cfoutput>

Again, this whole thing works as long as I dont try to do it the old way as you said. When I use the original two files he gave me....objectTest.cfm and action.cfm....then it works.

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

Now one thing I will mention is that I have other form elements on this page.

Yeah, but they're not inside the same <form> right? So you could process that form data separately, like have a "preview" and "submit" button for just that form.

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

Well I tried that.,...I think...lol. I clicked the ajax function button which outputted the "list" as needed, but then when I click my page submit button, I still got a data undefined error.

What I did though was I have this code/form inside my old form. That might make the difference. Still though, if my action page is looking for a vaque for the element of data, not sure that would work either. Gonna try anyway.

Nope....same error..lol

Pretty much the whole page is a form to submit cfinputs and pass the form values to my action page. I closed the cfform right before this jQuery form for the lists. So then I go down, populate my dynamic list, click submit, the values are displayed, then I lcick my page submit button.

hmmm...both submits action is to the builderAction.cfm. Maybe I need another "go between" action page?

No scratch that. The jQuery form action is the original action.cfm which just displays the results without a refresh. Then the page form action os to builderAction.cfm. I guess I need to somehow capture the values of the list, which I just thought would be straight html.

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

Well I tried that.,...I think...lol. I clicked the ajax function button which outputted the "list" as needed, but then when I click my page submit button, I still got a data undefined error.

Right. Because that variable doesn't exist... The ajax function and your <form> *don't* send the same data the same way

Ajax uses-> URL scope
Your <form> uses-> FORM scope

Ajax sends a complex structure -> URL.data[ItemName].vendorName ...etc...
Your <form> sends simple strings -> FORM.vendor, FORM.product, etc..

ie exactly what's in your <form>
<input type="text" class="vendor-input" name="vendor" />
<input type="text" class="product-input" name="product" />

You can't just plug the ajax into a larger <form>, POST it have it work right out of the box. You won't get an error, but your code isn't set up to handle it. It's something you have to add.

At this point I'm totally lost ;-/ ... I thought you were trying to do a simple preview of JUST the vendor/product stuff. But it's starting to sound like much more than that ... There's a lotta stuff on that page you linked. If you're trying to incorporate this section (vendors/products) with that whole big form .. you've got your work cut out for you. I'm sure it's possible, but it's beyond my skills. And with my brain mainly focused on my work, I've kinda lost track of what you're trying to do at this point ;-)

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

I really do appreciate how much you've helped me! lol I guess what I know in my head of how I want something to work....how I want one thing to work with another thing...here two different pieces and types of code, is one thing. Translating it to a forum like this so that someone would understand perfectly is another thing.

Ajax sends a complex structure -> URL.data[ItemName].vendorName ...etc...Your <form> sends simple strings -> FORM.vendor, FORM.product, etc..

Yes that makes sense and now that you explained it, I do understand the difference. I'll keep working on it for now. I may not know alot of code like this, but one thing I have learned is that almost nothing is impossible in coding of one form or another...lol There has to be a way.

Thanks again for all your patience and help!

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

If I knew more jquery I'd say change it make the field names unique. Then you could process it easily as product1,product2, etc.. But that's beyond my skills for now ;)

Yeah, there's an art to asking a "good" question. I'm fine with small stuff. But I really suck asking about big conceptual stuff. lol. Usually people have no clue what I mean. Some people are amazing at it. Their descriptions simple and concise. They get great answers ... when I couldn't even form the question...let alone the answer. lol

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

Btw ... if you're going to open another question on this, I'd suggest including *exactly* what you happen, with specific variable and field names, etc.. Don't make the reader guess ;-)

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

lol will do. I actually went to houseoffusion.com and posted a thread with link to the application. I have a reply, but I hadnt uploaded my newest files, so did that and waiting to see what they say..lol

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

I actually went to houseoffusion.com

Smarter brains than mine live there. So hopefully you'll have better luck. lol

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

lol well if they are smarter than you and you are smarter than me...guess that makes me fairly dumb...hahaha

Yeah the guy that replied is actually a friend of a friend of mine....who was both our instructor at our local community college.

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

... or it makes you "smart", me "smarter" and them "friggin geniuses". It's all in the spin ;-)

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

Ok I like the sound of that much better...lol

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0
Question Answered as of 2 Years Ago by arrgh

Ok I like the sound of that much better...lol

Since I'm often on the bottom rung, I've learned to "adjust" my perception of things ;-)

arrgh
Posting Pro in Training
405 posts since Dec 2008
Reputation Points: 32
Solved Threads: 50
Skill Endorsements: 0

Ahh well maybe one day I can be somewhere in the middle...lol I dont set unreachable goals!

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

So finally you guys have a solution here .. lol

sharma.rahul375
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

yes, the thread was marked solved. I didnt post my final code since it has sensitive material in it.

teedoff
Practically a Master Poster
605 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5731 seconds using 2.75MB