user2181479
user2181479

Reputation: 3

How to stop Select drop box resetting after submit

I have created a PHP / HTML file which enables me edit invoices from a MySQL table. It works, but not as well as I would like.

In order to select the relevant invoice for editing, I have created a dropbox. Each row is composed of a number of fields which makes the invoice identifiable. When I select the relevant invoice from the dropbox and click on the ‘Submit_Invoice’ button, the fields of the relevant invoice appear in a number of table elements for editing. However, the select drop box resets i.e. reverts to the default (first) invoice in the table rather than staying on the one which has been selected and which is being edited.

Is there a simple way of having the dropbox not reset i.e. maintain focus on the selected invoice after clicking on the ‘Submit_Invoice’ button?

I have spent a lot of time searching but have been unable to find an answer to this question. I can submit code if that helps.

Upvotes: 0

Views: 3556

Answers (2)

CoBolt
CoBolt

Reputation: 442

Some code would've been great.

Lets assume your Submit_Invoice-button isnt hook'd up on some crazy java-script-magic.

In the loop generating all of your dropdown-options you need to identify the one that is "active" and set it to

selected="selected"

Like this:

//loop start
echo '<option value="'.$row['id'].'"';
if ($_POST['id'] == $row['id']) echo 'selected="selected"';
echo '>'.$row['id'].'</option>';
//loop end

Upvotes: 1

Jay Bhatt
Jay Bhatt

Reputation: 5651

See below for reference

<select name="select">
<option value="1" <?php if(!empty($_REQUEST['select'] && $_REQUEST['select'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if(!empty($_REQUEST['select'] && $_REQUEST['select'] == 2) echo 'selected'; ?>>2</option>
<option value="3" <?php if(!empty($_REQUEST['select'] && $_REQUEST['select'] == 3) echo 'selected'; ?>>3</option>
</select>

Upvotes: 1

Related Questions