Using select / option to display data

I am attempting to use … to load data onto a web page, so when an option is selected, the relevant data appears on the page. So far, I have one piece of data displayed, but none of selected options change this.

This is the PHP:

<form name="form" method="post" action="#">

      <?php
        $select_tag = "<select onchange='reload(this.form)'>";
        $select_tag .= "<option>test one</option>";
          foreach ($data as $output)
          {
          	$select_tag .= "<option>" . $output['header'] . "</option>";
          }
        $select_tag .= "</select>";
      ?>
      <?php echo $select_tag; ?>
      <br>
      <button type="submit" value="submit">Submit</button>
    </form>

    <?php echo $output['pages']; ?>

The database contains for test data items, but when the page is loaded, the fourth item is shown. I guess this has to do with the code I am using.

I am connecting to the database using PDO if that helps.

So, how do I get the data for page 1, 2, 3, and 4 to show when selected.

Thanks in advance for any help.

Well, first of all you are trying to do a selection using a form then taking what the user selected to display the corresponding data? I believe I am understand you correctly…I think.

Tackle one thing at a time would be the way to go about doing it.

For example

        <form id="myForm" action="" method="post">
            <select id="selectCategory" class="select-css" name="category" tabindex="1">
                <?php 
                   foreach ($data as $record) {
                       echo '<option value="'. $record['header'] . '">' . $record['header'] . '</option>'; // The header or category 
                   }
                ?>
            </select>
           <button class="form-button" type="submit" name="submit" value="enter">submit</button>
        </form>

Then in JavaScript taking the value of select HTML element attribute value that the user has selected or using PHP. Using JavaScript is a little more involved initially and when using PHP leave the action="" attribute empty (I believe you can leave it out). Then pull the data from what the user has selection to output it and I would suggest doing it on a separate page (file) at least at first. I hope I was understanding you properly and that this helps a little?

The work-flow for this would be -

  1. Display a list (select/option menu or links) of the choices. The page id would be the value that’s used in the select/option menu or link.
  2. When a choice is made, test for and get the submitted page id, retrieve the matching data, and display it.

You need specific sections of code for each of these activities.

Also, because you are determining what will be displayed on a page, the convention used for the www is to use a get parameter in a url to carry the page id, i.e. the form method would be get, not post. This will allow someone to bookmark the page and return to the same page or share the url of the page with someone. If using a select/option menu, you would make the selected option ‘sticky’ so that if someone reloads the page, the choice will not be forgotten.

Thanks for the code. It’s a bit easy for me to understand. May I ask if you could show show me how to display the text on the page when an option is selected.

Thank you for the advice. Although I have asked for advice on using id with Strider 64, I would appreciate advice on how to use id as you suggested, also can you explain and/or show what you mean by ‘sticky’.

Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service