Solved

Checkbox problem

Posted on 2006-10-20
12
177 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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

15 Experts available now in Live!

Get 1:1 Help Now