Jump to content

Using <select><option>?</option></select> to display text.


Steve_Berry
 Share

Go to solution Solved by ginerjm,

Recommended Posts

I am trying to use a <select> <option> to display text on a web page.  The data comes from a database.

This is what I am attempting:  When an option is selected and submit button clicked them some text will be displayed.

At the moment I do have text on the web page, but none of the selected options change this.  I have four pieces of text: Page 1, Page 2 etc. At the moment Page 4 text is displayed.  I would like each piece of text to be displayed.

This is the php:

<?php
//  Connect to the database
    $pdo = new PDO("mysql:host=localhost;dbname=###", "###", "");
   
    $sql = "SELECT * FROM testdb ORDER BY id";
    try {
      $stmt = $pdo->prepare($sql);
      $stmt->execute();
      $data = $stmt->fetchAll();
    }
  catch(Exception $ex){
    echo ($ex -> getMessage());
  }

?>

This is the html:

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

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

I would appreciate help with this.  However, from previous attempts at adding data to a page I used isset() and I think $_POST(), possibly together.  If these are the things I need to use, then please could you include their usage within any examples you feel would help.  Thank you.

Link to comment
Share on other sites

I have tried a few things, as a beginner I guessed what I needed to do.  The following code is what I have tried - not working obviously.  Am I on the right track.  If not I would appreciate an example.

Thanks.

<select onchange="reload(this.form)">
  <option>test one</option>
  	<option>
    	<?php foreach ($data as $output) {

     		echo $output['header'];

        } ?>
  </option>
</select>

 

Link to comment
Share on other sites

  • Solution

Try this instead

$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>";

Now - in your html area simply place the $select_tag var where you want this html to show up.

  • Like 1
Link to comment
Share on other sites

Hello, after some search, testing etc.  I found something online to help with what I have been trying to do. I displays the options fine, but I still don't get the required data to display.  For example, <option value="Page 1">Page 1</option> does not show Page 1 text.

I also have an if else statement where the else part does not appear to work - no message.

This is the code:

<div class="container">
    <form name="myForm" method="post" action="">
      <div class="select-block">
        <select name="Pages">
          <option value="" disabled selected>Testin 123</option>
          <option value="Page 1">Page 1</option>
          <option value="Page 2">Page 2</option>
          <option value="Page 3">Page 3</option>
          <option value="Page 4">Page 3</option>
        </select>
      </div>

      <input type="submit" name="submit" value="Submit">
    </form>

    <?php
      if(isset($_POST['myForm'])){
        if(!empty($_POST['Pages'])) {

          //Connect to the database
          $pdo = new PDO("mysql:host=localhost;dbname=timeline", "root", "");
          $sql = "SELECT * FROM testdb ORDER BY id";
          $stmt = $pdo->prepare($sql);
          $stmt->execute();
          $data = $stmt->fetchAll();
          echo $data['pages'];

        } else {
          echo 'Please select an option.';
        }
      }

    ?>
  </div>

Please, could you help with the code.   Thanks

Link to comment
Share on other sites

4 hours ago, Steve_Berry said:

there should be some text, but the page just resets.

That's because all you are doing is resetting the page

<select onchange="reload(this.form)">
  

You need to send the form data. Try...

<select name="page" onchange="this.form.submit()">
  

and change

8 hours ago, Steve_Berry said:
if(isset($_POST['myForm'])){

to

if ($_SERVER['REQUEST_METHOD']=='POST') {

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.