I have a dropdown box to choose topics from a discussiob board so i can then view all the replies for deletion etc.

At the minute when I choose a topic i then press a submit button and get all the answers. But is there any wayt i could have it so that the answers just get displayeds depending what is shown in the dropdown box?

This is my code to populate the dro[pdown.

 <?php
          '../inc/connect.php';
   //populate form dropdown box
   $op = '';
   $r = "SELECT id, topic FROM forum_question ORDER BY id";
   $result = $link->query($r);
   if (mysqli_num_rows($result)){
      $comment = 0;
if(isset($_POST['comment'])) $comment = (int) $_POST['comment'];
 while ($d = mysqli_fetch_assoc($result)){
     $sel = ($comment == $d['id']) ? " selected='selected' " : '';
     $op .= "\n\t<option value='{$d['id']}'$sel>{$d['topic']}</option>";
  }
   }
?>

And this is my form that contains the dropdown and shows the answers

<form class='form2' action='forumadminpage.php' method='post' enctype='multipart/form-data'>

                  <b><span class='formheading'>Choose A Topic</span><br />To Remove Comments</b><br />
                   <select name="comments">
                      <?php echo $op;?>
                   </select>
                   <input type='submit' name='submit' value='Get Comments' />
                   <?php
                   include '../inc/connect.php';
                   if(isset($_POST['submit'])){
                   $comments= intval($_POST['comments']);
                   $data = mysqli_query($link, "SELECT * FROM forum_answer WHERE question_id IN ($comments) ")
                     or die(mysql_error());
                     while($info = mysqli_fetch_array( $data )) { 
                        echo "<p>";
                        echo "<input type='checkbox' name='remove[{$info['id']}]' value='Remove' />";
                        echo $info['id'];
                        echo ':   ';
                        echo $info['a_title'];
                        echo "</p>";
                     }
                     echo"<input type='submit' name='submit' value='Delete Comments' />";
                     }



                   ?>

                   </form>

Thanks for looking........

Member Avatar for diafol

Are you talking about an 'ajax' solution so that you don't need to submit the value from the dropdown - so that it displays automatically?

If so, I suggest using jQuery, although it could be done with vanilla js if req'd.

Unless you're uploading a file, you don't need this: enctype='multipart/form-data'

AFAICS, there's little point in using the posted comment with ajax:

if(isset($_POST['comment'])) $comment = (int) $_POST['comment'];
 while ($d = mysqli_fetch_assoc($result)){
     $sel = ($comment == $d['id']) ? " selected='selected' " : '';
     $op .= "\n\t<option value='{$d['id']}'$sel>{$d['topic']}</option>";
  }
}

So this would suffice...

while ($d = mysqli_fetch_assoc($result)){
     $op .= "\n\t<option value='{$d['id']}'>{$d['topic']}</option>";
  }
}

Making the first item in the dropdown selected on initial page load.

Your form could be simplified to...

<form class='form2' action='forumadminpage.php' method='post'>
    <strong><span class='formheading'>Choose A Topic</span><br />To Remove Comments</strong><br />
    <select id="topics" name="topics">
        <?php echo $op;?>
    </select>
    <p>Remove the following comments:</p>
    <div id="comments">
    </div>
    <input type='submit' name='submit' value='Delete Comments' />
</form>

The jQuery ajax script, to be placed at the bottom of the page, just before the close </body> tag...

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    $('#topics').change(function(){
        runCommentGetter();
    });

    function runCommentGetter(){
        var op = $('#topics').val(); 
        $.get( "includes/get_delete_comments.php", { ddoption: op }, function(data){
            $('#comments').html(data);
        });
    }

    runCommentsGetter();
</script>

The last line ensures that the comments are displayed on page load.

Your php file that gets the comments (includes/get_delete_comments.php) can then be something like this...

<?php
    include '../inc/connect.php';
    if(isset($_POST['ddoption'])){
        $topic = intval($_POST['ddoption']);
        $result = mysqli_query($link, "SELECT * FROM forum_answer WHERE question_id IN ($topic) ") or die(mysql_error());
        while($data = mysqli_fetch_array( $result )) { 
            echo "<input type='checkbox' name = 'removecomment[{$data['id']}]' />";
            echo $data['id'] . ':   ' . $data['a_title'] . "<br />";
        }
    }
?>    

Off top of my head, so expect a bug or two.

I used your code, but no comments get displayed, either on page load or when a choice is made for the dropdown

Oldschool

<select name="comments" onchange='this.form.submit();'>
<?php echo $op;?>

but it has to have

<input type='submit' name='mysubmit' value='Delete Comments' />

just in case there is a name conflict

Member Avatar for diafol

I used your code, but no comments get displayed, either on page load or when a choice is made for the dropdown

Like I said...

Off top of my head, so expect a bug or two.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.