Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placing a selected value inside input box and accessing it 3

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
0
0
IL
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:
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><?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><?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.
list_i1ageo.gif


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
 
Couple of things:
1. Close select tags - you are missing </select> to wrap your <option>...</option>
2. Why do you need <input ...> if you already have two <select ...> drop boxes?

It is best to ask your questions individually so that proper attention is given and you benefit from getting the help you seek. I do not think anyone here will suggest you ask fewer questions .... [glasses]

As far as your 2nd question, accessing the selected value off a <select ...> drop box is no different than the value entered in an <input ....> field. So, $_GET, $_REQUEST, and $_POST will be standard. Of course, this is dependent on how you process the form and if using AJAX calls GET/POST ...

Finally, look at what @feherke points out, make sure your type="" is valid so that you can get expected behavior. Some frameworks attempt to change these standards as if to say they are ahead of the game and if you are using a framework that suggests you set this property in such way, you may want to include this piece of information in future questions so that we learn alone side you.


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Inputs and selects are 2 different types of elements. They do not relate to each other nor would they exist inside one another.

Since you have a select element, where you select the number, why do you need it to be placed inside another form element (the input box)?

Also as has been pointed out, "list" is not a valid type for an input. Valid types are: text, checkbox, radio and button. Any other value you place there will default to text. So essentially all you are creating is a textbox.


Beyond that, accessing a select item in PHP is the same as any other form element with a value. You access it from the $_POST or $_GET superglobals once the form has been submitted, using its name. In the case of your selects: $_POST['counters'].

Now an interesting thing will happen, since both of your selects are named the same, the second one will overwrite the value of the first when the form is submitted. So you will only ever get 1 value.

Name your selects differently to get both values.





----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Thanks southbeach,
I'm very sorry about the disorder.
1. I didn't put closing tags to select because I didn't know how to handle it.
2. I mixed up input types such as "checkbox", "radio" etc.. I didn'y know select was different.
"list", I think I found through google search and I might have not fully understood it so I illused it.
Thanks for your assistence. I thing I better handle "select" now.
 
Thanks a lot Vacunita.
Thing are much more clear now and I should thank you all for the assistence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top