Solved

Checkbox problem

Posted on 2006-10-20
12
179 Views
Last Modified: 2013-12-24
Hello!

I have couple of images with checkbox. There have 5 images and user can choose his favourite image, then user press submit button and that checkbox data go to the action page. But now the problem is that action page insert all pictures to the database (not that which is user favourite image)

Here is my inset page code
<cfquery name="getdetails" datasource="#dsn#">
select imgid, picture, text
from pictures
</cfquery>
<html>
<head>
<title>pictures</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.picture#" border=0 width=100 alt="">
            <input name="picture" type="checkbox" value="#imgid#">
            <INPUT type="hidden" name="nimi" value="#kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
      
      
      </tr>
       <cfset tc=0>
      </cfloop>  </table>
 <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


...and here is my action page code (where the magic happens, I want that only those pictures goes to the database which user have chosen using checkbox)

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<cfif isDefined("form.picture")>
 <cfloop list="#form.picture#" index="i">
   <cfquery name="syota" datasource="#dsn#">
INSERT INTO            kilpi      
                        (imgid, picture)
VALUES
                        ('#imgid#', '#form.picture#')
   </cfquery>
 </cfloop>
</cfif>



</body>
</html>

0
Comment
Question by:mattimeikalainen
  • 6
  • 6
12 Comments
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17774290
Change to this and see if it helps...


Change Image to this...

<img src="#dir##picture#" border=0 width=100 alt="">

Change checkbox to this....

<input name="picture" type="checkbox" value="#imgid#|#picture#">




Change if statement on action page to this...

<cfif isDefined("form.picture")>
   <cfloop list="#form.picture#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17774390
when you loop over a list ie:

as you have here....

 <cfloop list="#form.picture#" index="i">
   <cfquery name="syota" datasource="#dsn#">
       INSERT INTO  kilpi (imgid, picture)
       VALUES ('#imgid#', '#form.picture#')
   </cfquery>
 </cfloop>

the index "i" is the actual list item for each iteration through the loop...
so you can see why each time you looped, you weren't even referencing the list, you just had variables in there...

so lets say you have a list like so...

Try this simple example to see what I mean......

<cfset myList = "Honda, Ford, Buick, Chevy">
<cfoutput>
  <cfloop list="#myList#" index="myCar">
   #myCar#<br>
  </cfloop>
</cfoutput>

See how "myCar" is actually the item in the list? So that's why we give your check box the value "ImageId"|"Picture"

because with each one selected... you'll form a list like so...  image001|picture001,image003|picture003

then you loop over that list wit say the index of "TheItem"

which will give you each item...

1)   image001|picture001
2)   image003|picture003

then listfirst(TheItem,'|') will give you "image001"
and listlast(TheItem,'|') will give you "picture001"

it's like a list within a list...
0
 

Author Comment

by:mattimeikalainen
ID: 17775312
Sorry, that not work. It still insert all pictures to the database.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17775509
can you repost your code with the changes you made?
0
 

Author Comment

by:mattimeikalainen
ID: 17775679
this send page:

<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##kuva#" border=0 width=100 alt="">
            <input name="kuvat" type="checkbox" value="#imgid#|#picture#">
            <INPUT type="hidden" name="nimi" value="#picture#">


and this is action page:

<cfif isDefined("form.picture")>
   <cfloop list="#form.picture#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17775791
in looking at this, you changed your checkbox name to "kuvat", but you didn't change the "form.pictures" name in the action page to match that value

try this on your action page...

<cfif isDefined("form.kuvat")> <!--- this has to be the form name of your checkbox --->
   <cfloop list="#form.kuvat#" index="i"> <!--- this has to be the form name of your checkbox --->
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 

Author Comment

by:mattimeikalainen
ID: 17776220
I chance my database and cfm code little bit. Here is the new ones
 input page:
<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
            <input name="kuva" type="checkbox" value="#imgid#|#kuva#">
            <INPUT type="hidden" name="nimi" value="#kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
      
      
      </tr>
       <cfset tc=0>
      </cfloop>

action page:

<cfif isDefined("form.kuvat")>
   <cfloop list="#form.kuvat#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuva)
         VALUES ('#tilausnumero#', "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>



but now i get next error message  [Microsoft][ODBC Microsoft Access Driver] Invalid bracketing of name '[kayntikortti3.jpg]'.
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17776511
change it to this instead.... you "have" to make sure the form you're looping over in the action page has the same name as the checkbox....

<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


         <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
            <input name="kuva" type="checkbox" value="#getdetails.kuva#">
            <INPUT type="hidden" name="nimi" value="#getdetails.kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
     
     
     </tr>
      <cfset tc=0>
     </cfloop>

action page:

<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuva)
         VALUES ('#tilausnumero#', "#i#") <!--- also note here --->
      </cfquery>
   </cfloop>
</cfif>




I'm not sure what the variable "tilausnumero" is, but if it doesn't need to be specific to the item selected, you're fine... otherwise stick to what we did before with the "imgid"
0
 

Author Comment

by:mattimeikalainen
ID: 17779157
"tilausnumero" =(ordernumber)
is this:

<cfset theList = "1234567890">
<cfset tilausnumero = "">
<cfloop from="1" to="8" index="idx">
  <cfset tilausnumero = tilausnumero & mid(theList, randRange(1,len(theList)), 1)>
</cfloop>
0
 

Author Comment

by:mattimeikalainen
ID: 17779240
We are so close, now i change code little bit and now data goes to the database. But only and big problem is that when I choose example 3 pictures, that data goes three times in to database. Here is link to my print screen picture from my database.

http://www.realex.fi/coldfusion/database.jpg

and here is codes
send page:

<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">
          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2">
<img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
<input name="kuva" type="checkbox" value="#getdetails.kuva#">
            <br>
            <br>
            </font></cfoutput> </td>      
      </tr>
       <cfset tc=0>
      </cfloop

and action page:

<cfset theList = "1234567890">
<cfset tilausnumero = "">
<cfloop from="1" to="8" index="idx">
  <cfset tilausnumero = tilausnumero & mid(theList, randRange(1,len(theList)), 1)>
</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuvat)
         VALUES ('#tilausnumero#','#form.kuva#')
      </cfquery>
   </cfloop>
</cfif>
</body>
</html>
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 200 total points
ID: 17780612
change your last statement on your action page to this..... and it'll fix that problem


<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="myImg">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuvat)
         VALUES ('#tilausnumero#','#myImg#')
      </cfquery>
   </cfloop>
</cfif>

When looping over a list like above, your use the index variable to get the item in the list to insert...

~trail
0
 

Author Comment

by:mattimeikalainen
ID: 17783455
Thank you very much!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OpenLDAP set password to expire 7 635
Running Guacamole on port 80 5 214
Coldfusion Datefield problem 2 110
Website being blocked? 3 127
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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