4 Replies - 3346 Views - Last Post: 13 April 2012 - 08:25 AM Rate Topic: -----

#1 deucalion0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 27-November 10

Problem using a session to store an array which is added to by a form

Posted 13 April 2012 - 07:30 AM

I am trying to create a form that when input is entered it is stored in an array which is saved in a session. I have been working on this for a few days and I am lost. Here is the code I have for my form:

<form name="input" action="html_form_action.cfm" method="post">
id <input type="text" name="id" />
<input type="submit" value="Submit" />
</form>


Here is the ColdFusion action page code:

<cfapplication name="arrays" clientmanagement="Yes"  sessionmanagement="Yes">


<cfif !isDefined('form.id')>
	<cfset session.urlid = form.id>
	

	
<cfset session.arrData[(arrayLen(session.arrData)+1)] = #session.urlid#>
</cfif>


<cfloop array=#session.arrData# index="name">
<cfoutput>
<form name="input">
item<input type="text" name="id" placeholder=#name#>

</form>
</cfoutput>
</cfloop>


Is this along the correct lines to do this? I have been reading the documentation trying to understand the help there but its a bit too much for me. My understanding of the problem was to create a session, create an array, items added to the array are stored in the session, but I am getting errors such as this:

Object of type class coldfusion.runtime.Struct cannot be used as an array

I was thinking that I need to check if the array is defined?

I would appreciate any advice on this at all!

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem using a session to store an array which is added to by a form

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1866
  • View blog
  • Posts: 3,389
  • Joined: 13-January 08

Re: Problem using a session to store an array which is added to by a form

Posted 13 April 2012 - 07:42 AM

1./ This code here:
<cfapplication name="arrays" clientmanagement="Yes"  sessionmanagement="Yes">

...that needs to go into a file called either Application.cfm or Application.cfc (depending on what version of CF you're using...most likely the latter file). That ought not to go into a form process page.

2./ I'm not too solid on what you're trying to accomplish here. Why do you want to put the form values you have from the form submission into an array and then into a session? The only other thing you're doing is using the previously submitted form values to populate as defaults in a new form, right? Is there anything else going on or some other use you're wanting to make of the form later such that storing it in a session variable is necessary?

This post has been edited by Craig328: 13 April 2012 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 deucalion0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 27-November 10

Re: Problem using a session to store an array which is added to by a form

Posted 13 April 2012 - 07:57 AM

Hi Craig328, I basically trying to build a simple shopping cart, I felt if I could store input from a form then I can proceed to build the actual shopping cart. So I thought, store form entries in an array, and save the state of the array in a session, that was my thinking, does that make sense? My code and files are a mess becauase I do not know what I am doing and due to pessure to get things done I am probably cutting too many corners unfortunately, I am now trying to follow a tutorial here but having no luck. I shall keep on going until I get it though.

Cheers!!!
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1866
  • View blog
  • Posts: 3,389
  • Joined: 13-January 08

Re: Problem using a session to store an array which is added to by a form

Posted 13 April 2012 - 08:15 AM

Ah. I see. Well to be perfectly honest, if you're new to CF, a shopping cart is probably above your skill level. I don't mean to be insulting but it's one of those things that, if you screw it up, can expose visitor's credit card info, mischarge their cards, lose items, double up on items...all that kind of misery.

There are a ton of free CF shopping carts to be found on the web. I'd truly recommend picking one of them and then working to fit it into your existing site structure rather than trying to roll one of your own with all the attendant pitfalls and gotchas such a project would entail.
Was This Post Helpful? 0
  • +
  • -

#5 deucalion0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 27-November 10

Re: Problem using a session to store an array which is added to by a form

Posted 13 April 2012 - 08:25 AM

You are absolutely right, but I would still like to get this to work, purely due to the fact I have been at it for ages and I want to accomplish something which should be simple. Here is my latest code:

<cfparam name="session.cart" default="#arrayNew(1)#">


<!--- Append a new element to the array.  This element is a new structure. --->
<cfset session.cart = arrayAppend( session.cart, structNew() )>
<!--- Set up a variable to hold the array position that we're inserting into. --->
<cfset thisCartItem = arraylen( session.cart )>
<!--- Populate the new structure with the item information passed from the form. --->
<cfset session.cart[thisCartItem].id = form.id>



<cfloop array=#session.cart# index="name">
<cfoutput>
<form name="input">
item<input type="text" name="id" placeholder=#name#>

</form>
</cfoutput>
</cfloop>
	





I keep getting the error:

Object of type class java.lang.String cannot be used as an array

This is seriously doing my head in. :(

Can you see anything obviously wrong with my new version of the code?

I appreciate your help and I am not insulted, shopping baskets require a proper security standard, I know that I kind of made on in PHP :)

Thanks again for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1