Patrick Lemark
Patrick Lemark

Reputation: 43

Keep drop down option

I have 3 pages, register.php, view.php and edit.php

I want to keep dropdown option in edit.php It takes the first option.

My pages:

REGISTER.PHP

<form id="base" method="post" action="insert.php">
    				<input data-validate="number" id="contract" name="contract">
                    <input data-validate="text" id="client" name="client">
    				  <select  name="step">
    					    <option value="option1">OPTION1</option>
    						<option value="option2">OPTION2</option>
    				      </select>
    			<button data-validate="submit">Register</button>
       </form>

EDIT.PHP

<form id="base" name="base" method="post" action="">
    	<input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
        <input data-validate="text"  value="<?php echo $client; ?>" id="client">
    					  <select  name="step">
    					    <option value="option1">Option1</option>
    						<option value="option2">Option2</option>
    				      </select>
    	<input type="submit" value="Change">
    </form>

Upvotes: 0

Views: 35

Answers (2)

Ahmad
Ahmad

Reputation: 5760

Try something like this, assuming $step to be the value from the database.

<form id="base" name="base" method="post" action="">
    <input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
    <input data-validate="text"  value="<?php echo $client; ?>" id="client">
                      <select  name="step">
                        <?php
                            $options = array("option1" =>  "Option1", "option2" => "Option2");
                            foreach($options as $v => $c)
                                echo "<option value='$v'" . ($step == $v ? " selected='selected'" : "") . ">$c</option>";
                        ?>
                      </select>
    <input type="submit" value="Change">
</form>

Upvotes: 1

Peter Chaula
Peter Chaula

Reputation: 3711

You could try this for each option in the selection but it could ve been easier if your select was dynamic:

...... >OPTION 1 repeat this with all you options although its much cleaner to use a function which takes the option value as an argumant

Upvotes: 0

Related Questions