Placing selected value in its' input box and accessing it with php

Hi everyone,
I’m trying to create a “from - to” selection area where the user selects his “from” number to “to” number.
The numbe choise is derived from a mysql table “hourshifts” and a column named “counter” .
This is my code:

<?php // select_list_from_query1.php
require_once 'myInitial.php';
require_once 'myLogin.php';
MYSQLI_SET_CHARSET($myConnection,'UTF8');
?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>select_list_from_query1</title>
    <link rel="stylesheet" href="css/table2.css">
  </head>
<body>
  <table>
    <td>
      <form method = "post" action = "first_form.php">
        <input type="list" class = "extraction"
          required aria-required="true" placeholder="from"/>
          <?php
            $myQue = "select counter from hourshifts";
            $myResult = $myConnection->query($myQue);
            if (!$myResult) die ("Database access failed: " . $myConnection->error);
            $numOfRows = $myResult->num_rows;
          ?>
          <select name='counters'>
          <?php
            for ($j = 0 ; $j < $numOfRows ; $j++)
            {
              $myResult->data_seek($j);
              $row=$myResult->fetch_array(MYSQLI_ASSOC);
          ?>
           <option value = "<?php echo $row['counter'] ?>"><?php echo $row['counter'] ?></option>
          <?php
            }
          ?>
          <input type="list" class = "extraction"
            required aria-required="true" placeholder="to"/>
            <?php
               $myQue = "select counter from hourshifts";
               $myResult = $myConnection->query($myQue);
               if (!$myResult) die ("Database access failed: " . $myConnection->error);
               $numOfRows = $myResult->num_rows;
            ?>
            <select name='counters'>
            <?php
              for ($j = 0 ; $j < $numOfRows ; $j++)
              {
                $myResult->data_seek($j);
                $row=$myResult->fetch_array(MYSQLI_ASSOC);
              ?>
              <option value = "<?php echo $row['counter'] ?>"><?php echo $row['counter'] ?></option>
              <?php
             }
          ?>
      </form>
    </td>
</table>
</body>
</html>

This is what the display looks like after yhe user selected the “from” number.


The number selected, 19, is placed outside the input box.
Can anyone help me with placing the selected numbers inside their input boxes?
My second question is: How can I access the number selected with php? It is shown on the screen whereby en “echo” command
but is it stored inside a variable or an array such as $_post?
Thanks

I’m a bit confused at your html. You have an input type="list" which I hadn’t heard of, then you open a select but don’t close it, then repeat it for the “to” field. Traditionally I’d have done:

<form method="post" action="whatever.php">
<select name="to">
<option value="1">1</option> // obv. fill out with your PHP loop
</select>
<select name="from">
// list of options and values from your db again
</select>
<input type="submit">
</form>

So I’m a bit unsure at your extra input types, and whether not closing the select tags will cause any trouble.

In the code called by your action parameter, the selections will be available in the $_POST array using the index of the name for each select field.

1 Like

Thank you droopsnoot,
Sorry about the confusion. I’m very confused at this stage of my internet programming right now .

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.