Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Multiple Forms on Single Page

    I have a web page with an Article on it.

    Beneath the Article, registered Members can post one or more Comments.

    For each Comment, other registered Members can answer a few questions and "rate" the Comment.

    My expectation is that if there is a really good or really bad Comment, then people will take the time to review it.

    As such, I don't want to wrap everything in a single Form, thus allowing people to turn this into an "assembly line".

    So, each Comment is wrapped in a Form. (They use POST.)

    In addition, there is a Form at the top of the Comments Section which is used for sorting of the Comments. (It uses GET.)


    Here is what I have so far...
    PHP Code:
        // HANDLE FORM.
        
    if (($_SERVER['REQUEST_METHOD']=='POST') && ($_POST['surveySubmit']=="Submit")){
            
            
    // Initialize Errors Array.
            
    $errors = array();
            
            
    // Trim all form data.
            
    $trimmed array_map('trim'$_POST);
            
            
    // Validate Form Data.
            // Check Comment-Helpful.
            
    if (empty($trimmed['commentHelpful'])){
                
    // Comment-Helpful does Not Exist.
                
    $errors['commentSurvey'] = 'Did this help?';

            }else{
                
    // Comment-Helpful Exists.
                // Check Value.
                
    if (($trimmed['commentHelpful'] == 1) || ($trimmed['commentHelpful'] == 2)){
                    
    // Valid Value.
                    
    $commentHelpful $trimmed['commentHelpful'];

                    
    // Continue processing...

                
    }else{
                    
    // Invalid Value.
                    
    $errors['commentSurvey'] = 'Did this help? (Yes/No)';
                }
                
            }
    //End of CHECK COMMENT-HELPFUL

        
    }//End of HANDLE FORM


        // CODE IN HTML SECTION
        
    while (mysqli_stmt_fetch($stmt3)){
            if ((!empty(
    $errors['commentSurvey'])) && ($trimmed['commentID'] == $commentID)){
                
    $commentSurveyError '<span class="errorCommentSurvey">' $errors['commentSurvey'] . '</span>';
            }
            
            <
    form id=commentSurvey_$commentID action='' method='post'>
                <
    fieldset>
                    <
    legend>Comment Survey$commentSurveyError</legend>

                    <!-- 
    Comment Helpful -->
                    <
    label for='commentHelpful'>Helpful?</label>
                    <
    select id='commentHelpful' name='commentHelpful'>
                        <
    option value=''>--</option>
                        <
    option value='1'>Yes</option>
                        <
    option value='0'>No</option>
                    </
    select>

                    <
    input type='hidden' name='commentID' value='$commentID' />

                    <!-- 
    Submit Button -->
                    <
    input name='surveySubmit' type='submit' value='Submit' />
                                                                        
                </
    fieldset>
            </
    form>
        }
    //End of LOOP THROUGH COMMENTS 

    Questions:
    1.) How does this design look so far?

    2.) Should I be using an array?

    3.) When I submit a review of a given Comment, it seems like the $_POST array is being correctly populated, but $commentSurveyError isn't working correctly, because every Comment beneath the one being reviewed also gets the same error-message!! (Again, maybe an array would help here?)


    Help and suggestions welcome!

    Sincerely,


    Debbie

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    How and where does $commentID get set? It doesn't look like it's changing.

    Do you really want to refresh the whole page for this? Why not use an ajax callback?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Absolutely agree with Daniel. *WHY* aren't you using AJAX to do this?

    And if you are NOT requiring users to be registered before they are allowed to vote, then prepare to see some articles (or whatever the voting is for) get thousands and thousands of votes.

    But I said all this before and you completely ignored me.

    I guess you want a magic answer and you aren't getting one.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Quote Originally Posted by doubledee View Post
    1.) How does this design look so far?
    Overcomplicated. But no reason it shouldn't work.

    2.) Should I be using an array?
    ??? Where? Why would you think so?

    3.) When I submit a review of a given Comment, it seems like the $_POST array is being correctly populated, but $commentSurveyError isn't working correctly, because every Comment beneath the one being reviewed also gets the same error-message!! (Again, maybe an array would help here?)
    Just use <legend>Comment Survey: $commentSurveyError_$commentID</legend> or similar.

    Note what Daniel said: You will be forcing a refresh of the entire page just because somebody clicked on a comment.
    That means that the page will be loaded from the server and the user will be placed at the *TOP* of the page, even though the comment he/she clicked on is 2 or 3 or more window heights down on the page. In other words, context will be lost. VERY annoying to users. Very un-modern. But you don't want to hear me say that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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