lordterrin
lordterrin

Reputation: 171

How do I keep the value selected from a select field from disappearing?

I have the following code:

<form method="post" enctype="multipart/form-data">
<table id="box-table-a" summary="PAGE HEADER" style='width:10%'>
<tr>
<th>
<?php 
include('./db.php');
$PM = mysqli_query($con, "SELECT DISTINCT Name FROM report ORDER BY Name ASC" ); 
echo "<b>Sort by Name:</b>&nbsp;\n"; 
echo " <select name='Name' onChange='submit(this.form)'>\n"; 
while( $row = mysqli_fetch_row( $PM )) { 
$sel = ( $table === $row[0] ) ? "id='sel' selected" : ""; 
printf( " <option %s value='%s'>%s</option>\n", $sel, $row[0], $row[0] ); 
    } 
    echo " </select>\n"; 
    ?> 
</th>
</tr>
</form>

Once the selection is made, the select box defaults back to whatever value is at the TOP of the list (from the SELECT SQL query...). How do I stop that from happening and keep the value that was selected in the select box until another selection is made?

Upvotes: 0

Views: 1272

Answers (2)

Marc
Marc

Reputation: 514

The reason the form resets when you change the drop down is because you have an onChange event on the select box. So any time it changes it runs submit(this.form). If this is intended functionality then below code will insure the item from the dropdown that was selected when it was submitted is reset as the selected item.

<form method="post" enctype="multipart/form-data">
    <table id="box-table-a" summary="PAGE HEADER" style='width:10%'>
    <tr>
        <th>
            <?php 
                include('./db.php');
                $PM = mysqli_query($con, "SELECT DISTINCT Name FROM report ORDER BY Name ASC" ); 
                echo "<b>Sort by Name:</b>&nbsp;\n"; 
                echo " <select name='Name' onChange='submit(this.form)'>\n"; 
                while( $row = mysqli_fetch_row( $PM )) { 
                    // We should check if this value matches what they sent in the post
                    // Doing a nice litter ternary operation to put selected in the $selected variable if it matches.
                    $selected = array_key_exists('Name', $_POST) && $_POST['Name'] == $row[0] ? ' selected' : '';

                    // Now down here I added another %s which maps to the 3rd 
                    // parameter in printf, its the $selected variable from above.
                    printf( " <option value='%s' %s>%s</option>\n", $row[0], $selected, $row[0] ); 
                } 
                echo " </select>\n"; 
            ?> 
        </th>
    </tr>
</form>

I am not sure given the context if the form being submitted every time the dropdown changes is intended.

If it is not intended then simply remove the attribute onChange and its contents from the select element and it will stop resetting.

You will need to add a submit button somewhere inside the form if you wish for it to be up to the user when the forms submits.

Upvotes: 1

brunoroc
brunoroc

Reputation: 21

Well, first of all, you should really stop mixing PHP and HTML codes, this is creep and confusing. And you may also like something called indent. It's more simple and clear. Your code should look like this:

<form method="post" enctype="multipart/form-data">
<table id="box-table-a" summary="PAGE HEADER" style='width:10%'>
<tr>
<th>
<?php 
include('./db.php');
    $PM = mysqli_query($con, "SELECT DISTINCT Name FROM report ORDER BY Name ASC" );
?>
    <b>Sort by Name:</b>
    <br> 
<select name='Name'>
<?php
while( $row = mysqli_fetch_row( $PM )) { 
    $sel = ( $table === $row[0] ) ? "id='sel' selected" : "";
    printf( " <option %s value='%s'>%s</option>\n", $sel, $row[0], $row[0] ); 
    }
?>
</select> 

</th>
</tr>
    <input type="submit" value="Submit">
</form>

And if you really want your form to stop doing this value remove, you should remove this onChange='submit(this.form)' and use a submit button

Upvotes: 0

Related Questions