Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    6

    Unanswered: Passing Checkbox Values as Boolean Back to DB

    I wrote some C code to handle the passing of textfield values from a webform back to postgres, what I want to do is adapt that code to form a new function for handling checkbox values. Below is my code for handling textfield values, can anyone help me adopt this to handle checkbox values (t/f) and tell me how to parse that back to the database?
    Code:
    void fDisplay_EditField(PGresult *pdb_results_indiv, char *pdb_name, 
           char *pform_name, int ilen, char *pprefix, char *psuffix, 
           int bpriv_user) {
    
    /* display edit form entry with current value (if any)          */
    /* if standard user, display value as text; if privileged user, */
    /*   display current value as form object to allow editing      */
    
      char *pstr;
      char sbuf[iBUFSIZE];
    
      printf("      <td>\n");
      printf("        %s", pprefix ? pprefix : "");
    
      pstr = (pdb_results_indiv) ? 
        fDB_GetField(pdb_results_indiv, pdb_name, 0) : "";
    
      if (!pstr) {
        sprintf(sbuf, "cannot find database entry for \"%s\"\n", pdb_name);
        fErr_Msg(sbuf);
        exit(0);
      }
    
      if (bpriv_user) {
        printf("<input type=\"text\" size=\"%d\" maxlength=\"%d\" ",
          ilen, ilen);
        printf("name=\"%s\" value=\"%s\">", pform_name, pstr);
      }
      else
        printf("%s", strlen(pstr) ? pstr : "&nbsp");
    
      printf("%s\n", psuffix ? psuffix : "");
    
      printf("      </td>\n");
      return;
    }
    I need to create a separate function to handle checkboxes, something like fDisplay_CheckBox() but I don't know how to handle the values (boolean I suppose) and pass them (there can be multiple) back to the postgres database.

  2. #2
    Join Date
    Nov 2006
    Posts
    6
    Okay, here is my SQLUpdate function, how can I write a separate function that will handle boolean values, rather than strings?
    Code:
    void fAddField_SQLUpdate(Tform_rec *pparms, char *pcmd, char *pform_field, 
           char *pdb_field, int bvis_too) {
    
    /* add specified field and value to SQL command string; clean up    */
    /*   values as we go                                                */
    /* string ultimately comprises a SQL update-existing-record command */
    
      char *pstr;
      char sbuf[iBUFSIZE], sval[iBUFSIZE];
    
    /* if Web form value is not found, don't do anything                 */
    /* note: even if value is empty string, we still need to process it, */
    /*   as it may be replacing a previous non-empty value               */
    
      if ((pstr = fWeb_Form_Val(pparms, pform_field)) == NULL)
        return;
    
      fCleanUpValue(sval, sizeof(sval), pstr);
      sprintf(sbuf, ",%s='%s'", pdb_field, sval);
      strcat(pcmd, sbuf);
    
    /* if saving visibility flag, construct appropriate field names */
    /*   and add current value                                      */
    
      if (!bvis_too)
        return;
    
      sprintf(sbuf, "%s%s", pform_field, sVIS_SUFFIX);
      if (((pstr = fWeb_Form_Val(pparms, sbuf)) == NULL) || !strlen(pstr))
        return;
    
      sprintf(sbuf, ",c%s%s='%s'", (pdb_field + 1), sVIS_SUFFIX, pstr);
      strcat(pcmd, sbuf);
    
      return;
    }

  3. #3
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    I can't see any SQL statements in your posts.
    You should post this in the C language forum

  4. #4
    Join Date
    Nov 2006
    Posts
    6
    Figured it out, thread closed.

  5. #5
    Join Date
    Nov 2006
    Posts
    6
    For those interested, the solution was simple. To my function definition I added another variable, bis_check and then added simple if statement to check for a value of '1' being passed which indicates it is a check box. So for all my non check box fields I passed a '0' and for the check boxes I passed a '1'. Here's the code:
    Code:
    void fAddField_SQLUpdate(Tform_rec *pparms, char *pcmd, char *pform_field, 
           char *pdb_field, int bvis_too, int bis_check) {
    
    /* add specified field and value to SQL command string; clean up    */
    /*   values as we go                                                */
    /* string ultimately comprises a SQL update-existing-record command */
    
      char *pstr;
      char sbuf[iBUFSIZE], sval[iBUFSIZE];
    
    /* if Web form value is not found, don't do anything                 */
    /* note: even if value is empty string, we still need to process it, */
    /*   as it may be replacing a previous non-empty value               */
    
      if ((pstr = fWeb_Form_Val(pparms, pform_field)) == NULL)
        return;
    
      fCleanUpValue(sval, sizeof(sval), pstr);
    
      if (bis_check == 1)
    	  sprintf(sbuf, ",%s='t'", pdb_field);
      else
    	  sprintf(sbuf, ",%s='%s'", pdb_field, sval);
    	  strcat(pcmd, sbuf);
    
    /* if saving visibility flag, construct appropriate field names */
    /*   and add current value                                      */
    
      if (!bvis_too)
        return;
    
      sprintf(sbuf, "%s%s", pform_field, sVIS_SUFFIX);
      if (((pstr = fWeb_Form_Val(pparms, sbuf)) == NULL) || !strlen(pstr))
        return;
    
      sprintf(sbuf, ",c%s%s='%s'", (pdb_field + 1), sVIS_SUFFIX, pstr);
      strcat(pcmd, sbuf);
    
      return;
    }
    I hope this helps somebody out there in the same predicament. Take care!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •