decremnting a db value with a form button?

Discussion in 'Programming' started by ldexterldesign, Dec 11, 2007.

  1. #1
    i have a page with two form buttons. when one button is pressed i need it to decrement a value in a field of my db and move to a new page. when the other button is pressed i need to simple move to the next page.

    any tips?

    cheers,
    lewis
     
    ldexterldesign, Dec 11, 2007 IP
  2. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    would i be better off using two radio buttons (decrement and do nothing respectively) and a submit button here?
     
    ldexterldesign, Dec 11, 2007 IP
  3. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <cfform action="<!--- page to load --->" method="post" id="decisionButtons">
          <cfinput type="radio" value="0" name="doNothing">
          Leave image
          <cfinput type="radio" value="1" name="decrement">
          Remove image
          <cfinput type="submit" name="submitDecision">
        </cfform>
    Code (markup):
    depending on the form result (either '0' or '1') i'd like to direct the user to a new page. how can i achieve this?

    also how do i delete threads/posts on this forum?! you'd think the 'edit/delete' button would suffice.

    thanks,
    lewis
     
    ldexterldesign, Dec 11, 2007 IP
  4. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    updated xhtml:

        <cfform action="<!--- page to load --->" method="post" id="decisionButtons">
          <cfinput type="radio" value="0" name="decisionBt" id="doNothing">
          <cfinput type="radio" value="1" name="decisionBt" id="decrement">
          <cfinput type="submit" name="submitDecision" value="Decide" id="decideBt">
        </cfform>
    Code (markup):
     
    ldexterldesign, Dec 12, 2007 IP
  5. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorted this with the following pieces of code in case it helps someone in the future.

    above the DOC type:

    <cfset currentPage=GetFileFromPath(GetTemplatePath())>
    <cfif isdefined("form.decisionGroup") and #form.decisionGroup# eq "doNothing">
      <cflocation url="index3.cfm" addtoken="no">
      <cfelseif isdefined("form.decisionGroup") and #form.decisionGroup# eq "decrement">
      <cflocation url="index4.cfm" addtoken="no">
    </cfif>
    Code (markup):
    in the body:

        <form action="<cfoutput>#currentPage#</cfoutput>" method="post" id="decisionButtons">
          <input type="radio" name="decisionGroup" id="doNothing" value="doNothing">
          <input type="radio" name="decisionGroup" id="decrement" value="decrement">
          <input type="submit" name="submitDecision" id="decideBt" value="Decide">
        </form>
    Code (markup):
    any idea why the above (non cfform/cfinput) works, yet this...

        <cfform action="<cfoutput>#currentPage#</cfoutput>" method="post" id="decisionButtons">
          <cfinput type="radio" name="decisionGroup" id="doNothing" value="doNothing">
          <cfinput type="radio" name="decisionGroup" id="decrement" value="decrement">
          <cfinput type="submit" name="submitDecision" id="decideBt" value="Decide">
        </cfform>
    Code (markup):
    ... cf'd-up code throws a 404 error?

    404

    /newMediaArts/<cfoutput>index2.cfm</cfoutput>


    java.io.FileNotFoundException: /newMediaArts/<cfoutput>index2.cfm</cfoutput>
    at jrun.servlet.file.FileServlet.service(FileServlet.java:349)
    at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
    at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
    at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:284)
    at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
    at jrun.servlet.http.WebService.invokeRunnable(WebService.java:172)
    at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:320)
    at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
    at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:266)
    at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

    cheers,
    lewis
     
    ldexterldesign, Dec 12, 2007 IP
  6. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    on the first page
    <input type="submit" name="decrement">
    <input type="submit" name="next">
    Code (markup):
    on the second page
    <cfif isdefined("decrement")>
      <cfquery datasource="dsource">
        UPDATE table
        SET field = field - 1
      </cfquery>
    </cfif>
    Code (markup):
    i'm sure you can figure the rest out from there
     
    Jamie18, Dec 12, 2007 IP
  7. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Jamie18's suggestion looks simpler :)

    If you were going to do this, try using cfparam. Similar results but the final code is simpler, and don't forget a default else case.

    
    <cfset currentPage=GetFileFromPath(GetTemplatePath())>
    <cfparam name="form.decisionGroup" default="doNothing">
    <cfif form.decisionGroup eq "decrement">
      <cflocation url="index4.cfm" addtoken="no">
    <cfelse>
      <cflocation url="index3.cfm" addtoken="no">
    </cfif>
    
    Code (markup):
    I don't think you can use CF tags inside other CF tags. Try moving it outside the cfform.

    
    <cfoutput>
         <cfform .... >
    </cfoutput>
    
    Code (markup):
     
    cfStarlight, Dec 12, 2007 IP
  8. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hi guys,

    thanks, yea, all that is helpful. another learning curve here. two points:

    1) with regard to your comment Jamie18, and i'm throwing this out to anyone: is it best to process form input on the same page, like i have been doing:

    
    <cfset currentPage=GetFileFromPath(GetTemplatePath())>
    <cfform action="#currentPage#" method="post" id="decisionButtons">
    
    Code (markup):
    or submit it to another page, or doesn't it really matter? could someone shed some light on the pro's and con's of doing it both ways?

    2) cfStarlight you are correct about not using <cfoutput></output> tags inside <cfform><cfinput></cfinput></cfform>. this now works fine:

      <cfoutput>
          <!--- TODO: improve form validation [throw error for no radio input] --->
          <cfform action="#currentPage#" method="post" id="decisionButtons">
            <cfinput type="radio" name="decisionGroup" id="doNothing" value="doNothing">
            <cfinput type="radio" name="decisionGroup" id="decrement" value="decrement">
            <cfinput type="submit" name="submitDecision" id="decideBt" value="Decide">
          </cfform>
        </cfoutput> 
    Code (markup):
    regarding your comment about use of default cfparam. here's a screen shot: http://www.personal.leeds.ac.uk/~scs4ll/Untitled-1.jpg

    in my code where i've commented:

      <!--- HOW WOULD I CODE IN A DEFAULT ELSE [NO VALUE SELECTED] CASE? --->
    
    Code (markup):
    ... would you be kind enough to help me out here?

    cheers guys,
    lewis
     
    ldexterldesign, Dec 12, 2007 IP
  9. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If the form code is short and sweet, I have nothing against either method. But large self posting forms can get real ugly an unmaintainable. So I often use separate pages for that reason, or because it fits in better with my current framework.

    It depends on what should logically happen if nothing was selected. With two radio buttons I'm thinking something is wrong if nothing was selected. So you could a) ignore it and perform a default action b) throw an error or c) or do nothing and just redisplay the form. But that's not very user friendly.

    If you want to ignore it, you could simply to treat one of the options as a "default" by using a cfif/cfelse.

    <cfif ButtonOneWasChecked>
    Do this ..
    <!--- for everything else --->
    <cfelse>
    Do something else
    </cfif>

    
    <cfif isdefined("form.decisionGroup") and [COLOR="Red"]#[/COLOR]form.decisionGroup[COLOR="Red"]#[/COLOR] eq "doNothing">
    
    Code (markup):
    Btw, you can get rid of those pound signs. They're not needed.
     
    cfStarlight, Dec 12, 2007 IP
  10. ldexterldesign

    ldexterldesign Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks mate, greatly appreciated as always. sorry for the delay in getting back to this thread. deadlines, deadlines...

    have a great festive period everyone,
    lewis
     
    ldexterldesign, Dec 17, 2007 IP