1

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql

$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1 

<select id="taskCompany" required name="taskCompany" class="form-control">

    <option value="" disabled selected>Select a Company</option>
    <option value="1">building</option>
    <option value="2">maintenace</option>
</select></div>

I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created. Thanks Jon

2
  • Is it me or are I have a stored value in mysql from a select box and I do not store any select option in the database values or text contradicting each other Commented Aug 20, 2015 at 10:05
  • you are not storing any value in database ? Commented Aug 20, 2015 at 10:13

3 Answers 3

1

I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list: You could do it like:

<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>

Or if you have the names coming from the database too.

<select id="taskCompany" required name="taskCompany" class="form-control">

<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>

Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.

This is for the time being, as I wanted to demonstrate but Please consider mysqli/PDO as mysql_* is deprecated.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>

<select id="taskCompany" required name="taskCompany" class="form-control">

    <option value="" disabled selected>Select a Company</option>
    <option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
    <option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>

4 Comments

Thanks for your quick reply but it does not seem to work. I have setup a fiidle take a look FIDDLE
add jquery library in your fiddle and remove the script tag from fiddle's script area
Thanks but did not want to use jquery I have answered my own question my adding and is statement <?php if ($row['taskCompany'] == '1') {$select ='selected'; }else{$select2 ='selected';}; ?> Thanks
check the modified answer
0

You can do this

<?php foreach($taskCompany as $row){ ?>
    <option value="unique id for select options">name for select options</option>
<?php } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.