Advice required on inputting data
Results 1 to 10 of 10

Thread: Advice required on inputting data

  1. #1
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816

    Advice required on inputting data

    Hi all,

    I need some advice on how best to do the following.

    I have a form which is populated with 10 rows of data, each row has a checkbox. I need to write all the rows checked or uncheck in to another table.

    example: Repeat region in form populated from table 1.

    Row 1: question1 - checkbox
    Row 2: question2 - checkbox
    Row 3: question3 - checkbox

    and so on.

    User checks the box or not, clicks on the submit button and then writes the "question" and the "checkbox" result into one record in table two.


    Any advice on the best approach would be great.
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,398
    Is "question1" (et al.) a text box where the user is supplying the question, or are those questions predetermined and you're only recording the responses?

    The reason I'm asking is that unchecked checkboxes aren't submitted at all. So, no matter how you name the checkboxes, you're going to end up with information like "boxes #2, 4, and 7 were checked" - you won't know which ones were unchecked unless you have some way of determining/storing how many (and/or which ones) were displayed in the first place.

    Without more information, my guess is that you'd want to name your checkboxes something like "answer[]" or perhaps "answer[123]" (where 123 is some primary/candidate key to identify which "question" is being answered). That way, $_POST['answer'] would be an array that you could use to insert whatever data is appropriate into your DB.

  3. #3
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816
    Hi Brad,

    The questions are predefinded, I just need to record the checked box, if not checked it would recorded as a zero in the table. But because the questions (rows) that appear in the form are pulled from table 1 and displayed in a repeat region I was not too sure how I should approach the coding. But now you have pointed me in a direction by using the checkbox name as answer[] I think I can work with that. I'll keep you posted.

    Many thanks
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

  4. #4
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816
    Hi Brad,

    I am have part success but I have hit a problem.

    In the form I now have the question named as "question[]" and the checkbox as "answer[]".

    If I select the first and last checkbox the result is not right.

    The 1st item in the answer array is answer[0]:1, checkbox checked. The 2nd item in the answer array, answer[1] is also 1, checkbox not checked.
    The 20th item is checked but answer[19] displays as 0.

    I hope this makes sense.
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,398
    Yes, that's as expected. As I said before, unchecked checkboxes aren't submitted, so you won't have any idea which one(s) was/were checked unless you add a unique ID inside of the [] brackets or give each checkbox a unique value.

  6. #6
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816
    Hi Brad,

    If the checkboxs are generated from a table row within a repeat regionof the form, the form only has one checkbox before it is run in a browser. So how do I give it a unique ID?
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,398
    I guess I'm not familiar with what a "repeat" region is. What do you mean by that?

    And if a "repeat" region doesn't allow you to distinguish the checkboxes, then the answer seems obvious: stop using a "repeat" region.

  8. #8
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816
    Hi Brad,

    A "repeat" region is Dreamweaver speak, its just a way of displaying row of data from a query result using:

    PHP Code:
     do { 
    <tr>
    <td>input name="questionid[]" type="text"  id="textfield" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
    <td><textarea name="audititem"><?php echo $row_InspectItems['AuditItem']; ?></textarea></td>
    <td>input type="hidden" name="audititemid" id="hiddenField" value="<?php echo $row_InspectItems['RecordID']; ?>"></td>
    <td>input name="answer[]" type="checkbox"  value="1"></td>
    </tr>

     } while ($row_InspectItems = mysql_fetch_assoc($InspectItems));
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

  9. #9
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,398
    Quote Originally Posted by dcjones View Post
    A "repeat" region is Dreamweaver speak
    Gross! Notepad > Dreamweaver.

    Quote Originally Posted by dcjones View Post
    using:
    PHP Code:
     do { 
    <tr>
    <td>input name="questionid[]" type="text"  id="textfield" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
    <td><textarea name="audititem"><?php echo $row_InspectItems['AuditItem']; ?></textarea></td>
    <td>input type="hidden" name="audititemid" id="hiddenField" value="<?php echo $row_InspectItems['RecordID']; ?>"></td>
    <td>input name="answer[]" type="checkbox"  value="1"></td>
    </tr>

     } while ($row_InspectItems = mysql_fetch_assoc($InspectItems));
    So why not use AuditItemNo or RecordID in the value of the checkbox rather than the meaningless value "1" ?

    Also, you should remove all of those 'id' attributes or fix their values. An "id" value has to be unique throughout the entire document, so using a static value inside of a loop doesn't make any sense.

    Likewise, the hidden input element seems useless as well unless every row in the result set contains the same value in the RecordID column (in which case it doesn't make any sense to be outputting it inside of the loop).

  10. #10
    The Englishman
    Join Date
    Sep 2002
    Location
    Warwickshire, England
    Posts
    816
    Hi Brad,

    After many hours of looking at this I have now taken a very different approach.

    Scrapped the "Dreamweaver" repeat region. I am now producing the form with all the question feilds and populating them with the content of the query.
    PHP Code:
    $questions = array();
    while (
    $row mysql_fetch_array($InspectItems)) {
      
    $questions[] = $row[3];

    This allows me to give the checkboxs unquie ID's. I can now reference the $_POST variables by name.

    Looking at it in the light of a new day I think I was unnecessarily complicating the process. It is now producing the required results.

    Many many thanks for all your input and advice. Having another pair of eyes looking at the problem is of great help.
    Kind regards, keep safe and well.

    Dereck.

    Better three hours too soon than a minute too late.

    If it's good to go, be sure to mark it resolved.
    It's at the top of the page under "Thread Tools"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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