0

I have a dropdown box on my form which is populated from a MySQL database. When a value is selected i want to open a larger form and set the value selected initially to become the selected value on the new form.

My HTML code for the Combo is as follows

<select id ="Opponents" name ="Opponents"
    <?php 
       opponent_load(Wheathill)
    ?>
    >
</select> 

The PHP function, which is used to populate the dropdown list as well as set the SELECTED value to display, is as follows ;

    function opponent_load($oppt){
      $db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
      $database = "matchmanagementdb";
      $db_found = mysqli_select_db($db_handle, $database);
      if ($db_found) {
          $SQL = "SELECT * FROM opponentsdb";
          $result = mysqli_query($db_handle, $SQL);
          while ( $db_field = mysqli_fetch_assoc($result) ) {
            $uName = $db_field['Opponents'];
            if ($uName == $oppt) {
                $selected = 'selected="selected"';
            } else {
                $selected = '';
            }
     echo "<option value='$uName' '$selected'> $uName </option>";
     }
     } else {
            print "Database NOT Found ";
     }
     mysqli_close($db_handle);
     }

The new form has the values in the dropdown box BUT the selected value is not shown as Wheathill. I intend to replace the function argument "Wheathill" with a variable that will be defined in another function. Can anyone help as to why the code does note work.

The code above is part of a larger code which includes a reference to the values required by the mysqli_connect() function

NOTE Novice at PHP.

DaveTheGolfer
  • 39
  • 1
  • 8

1 Answers1

0

Your errors here are the following.

First, you need to call opponent_load(Wheathill) after closing >. Currently, it is invalid markup, browser tries to fix it, but it is still invalid.

<select id ="Opponents" name ="Opponents">
    <?php  opponent_load(Wheathill); ?>
</select> 

Second, as you pass a string to a function, it's reasonable to enclose string in quotes.

<select id ="Opponents" name ="Opponents">
    <?php  opponent_load('Wheathill'); ?>
</select> 

And third one - is outputing <option>:

echo "<option value='$uName' '$selected'> $uName </option>";

If you put your $selected = 'selected="selected"'; here you will get:

<option value='$uName' 'selected="selected"'> $uName </option>

That's definitely an invalid markup. The solution is - remove quotes:

echo "<option value='$uName' $selected> $uName </option>";

which becomes:

<option value='$uName' selected="selected"> $uName </option>

which is correct.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • #u_mulder This works great thank you. I now want to turn the argument into a variable as follows; player_load() but this shows as an error. Any ideas. – DaveTheGolfer Feb 18 '18 at 14:08
  • I don't know what is player_load, I don't know what is the error. As a new question and explain the situation. – u_mulder Feb 18 '18 at 14:28
  • #u_mulder Sorry should have been opponent_load() I have a form that I use to get two variables that I use to search a MySQL database. Having found the data I wish to pass the data back to a data entry form containing combos by defining each combo with a specific 'Selected' value for display. That is I want to replace 'Wheathill' in the above HTML with a variable I get from another PHP script that is included in the HEAD. – DaveTheGolfer Feb 18 '18 at 14:51
  • So, you open ` – u_mulder Feb 18 '18 at 14:53
  • I was not sure opening one – DaveTheGolfer Feb 18 '18 at 15:51
  • I strongly recommend you to __ask another question__. – u_mulder Feb 18 '18 at 15:53