2

I am trying to clean up a search mysql webform so that all the functions (and there are many) reside in external files, called with require(fileName.php); in the form page, wanting to make the main webform.php a lot easier to follow and manage.

I have quite a few drop down menus in a searchDb.php form page for the user to select search criteria from, which are all scripted similar to:

    <p> <!-- Technician: -->
    <label for"Technician:">Technician:</label>
      <select name="searchTech" id="searchTech">
        <option value="0">-- Select Technician --</option>
          <?php
            techDDM($searchTech, isset($_POST['submitted']))
          ?>
      </select>

When the form first loads the drop down menus are populated as expected, and in this example by the function call techDDM() that resides in: require('dropDownMenu.php');

function techDDM(&$searchTech, $sbmtd) {
  // initialize and populate techArr
  $techArr = array('First Last1', 'First Last2', 'First Last3', 'First Last4', 'First Last5', 'First Last6', 'First Last7', 'First Last8', 'First Last9');

  echo  '<br />(' . __LINE__ . ') $searchTech : ' . $searchTech . '<br />'; // output for debug
  echo  '<br />(' . __LINE__ . ') $sbmtd : ' . $sbmtd . '<br />'; // output for debug
  foreach ($techArr as $t) {
    echo "<option value='$t'";
    if ($sbmtd == 1 && $searchTech == '$t') {
      echo " selected";
    }
    echo ">$t</option>";
  }
}

But after the form is submitted and if it is determined one of the text fields in which the user typed input contains errors, and if the user additionally selected a name from the Select Technician drop down menu, for example, as additional search criteria, then the I intend for the user's drop down selections to remain selected while they are prompted to fix typed errors. Unfortunately, the selected attribute for the option value the user selected is not being included in the <option tag, yet the condition is being met to include the selected attribute as shown in the page's source for the Technician section rendered by the HTML and PHP examples pasted above:

<p> <!-- Technician: -->
        <label for"Technician:">Technician:</label>
          <select name="searchTech" id="searchTech">
            <option value="0">-- Select Technician --</option>
              <br />(8) $searchTech : First Last1<br /><br />(9) $sbmtd : 1<br /><option value='First Last1'>First Last1</option><option value='First Last2'>First Last2</option><option value='First Last3'>First Last3</option><option value='First Last4'>First Last4</option><option value='First Last5'>First Last5</option><option value='First Last6'>First Last6</option><option value='First Last7'>First Last7</option><option value='First Last8'>First Last8</option><option value='First Last9'>First Last9</option>          </select>
        <span id="spanSpace"></span>

The source shows that tech selection: First Last1 was indeed selected, and the page was also submitted: $sbmtd == 1, which should then append the selected attribute to `First Last1, but that is just not happening.

Can anyone see what I am doing wrong or leaving out trying to get this working?

scriptz
  • 515
  • 2
  • 10
  • 3
    You are comparing your $searchTech variable with the _literal_ text `$t`. – CBroe Feb 08 '17 at 09:58
  • 2
    Varaibles in single quotes `'$t'` are not parsed. – u_mulder Feb 08 '17 at 09:58
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – u_mulder Feb 08 '17 at 09:58
  • @Cbroe & u_mulder thanks for the point in the right direction. As soon as I read your comments and made the necessary change, all became good once more. – scriptz Feb 08 '17 at 10:03

1 Answers1

2

change your condition:

if ($sbmtd == 1 && $searchTech == $t) {
      echo " selected";
    }
B. Desai
  • 16,414
  • 5
  • 26
  • 47