0

I am using PHP to change the value of a dropdownlist menu in my database. The status I am using are: New - progress - Wait - Done - Close Suppose that all the new entry are with status "NEW"

When I change the value I can still see a double option.

Here my code:

<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value=" . $row['status'] . " >" . $row['status'] . "</option>
        <option value='new'>New</option>
        <option value='progress'>Progress</option>
        <option  value='wait'>Wait</option>
        <option value='done'>Done</option>
        <option value='close'>Close</option>
    </select>
</td>

And a picture: enter image description here

--------------------- FULL CODE --------------

<?php

include("../includes/connection.php");

if ($link->connect_errno > 0) {
    die('Unable to connect to database [' . $link->connect_error . ']');
}

if (isset($_POST['update'])) {
    $results = $link->query("UPDATE job SET status='$_POST[status]', priority='$_POST[priority]' WHERE id='$_POST[hidden]'");
    $results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");
}

$sql = "SELECT * from job";
if (!$result = $link->query($sql)) {
    die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
    <thead>
        <tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
    echo "
        <th>" . $finfo->name . "</th>";
}
echo "
        </tr>
    </thead>
    <tbody>";


while ($row = $result->fetch_assoc()) {

      $job_id = $row['id'];
    echo "<form action='' method=post>";

    echo "<tr class='info'>

                <input type=hidden name=hidden value=" . $row['id'] . ">
                <td>" . $row['id'] . "</td> 
                <td>" . $row['device'] . "</td>
                  <td>" . $row['model'] . "</td> 
                <td>" . $row['problem'] . "</td>

                <td><select class='form-control col-sm-10' id='status' name='status'>
                                  <option value=" . $row['status'] . " >" . $row['status'] . "</option>
                  <option value='new'  >New</option>
                  <option value='progress'>Progress</option>
                  <option  value='wait'>Wait</option>
                  <option value='done'>Done</option>
                  <option value='close'>Close</option>
                       </select></td>

                <td><select class='form-control col-sm-10' id='priority' name='priority'>
                        <option value=" . $row['priority'] . "  >" . $row['priority'] . "</option>
                        <option value='high'>High</option>
                        <option value='medium'>Medium</option>
                        <option  value='low'>Low</option>
                       </select></td>

                <td> <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>

                <td> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'   name='[$job_id]' value='[$job_id]'  >  Info</a></td>


            </tr>";
    echo "</form>";
}
echo "
    </tbody>

</table>";

?>
8
  • 1
    Going to need to see a lot more code to offer any help - but presumably $row['status'] contains the selected option Commented Dec 3, 2015 at 10:41
  • $row['status'] contains the current status. Status can change thats why I put it as a first option, in the way that everytime you can see in which status you are, but still able to change it throught a button. Which other code do you need? Commented Dec 3, 2015 at 11:17
  • I am using $row['status'] to be able to change the status with another one of the dropdownlist. The problem is that I can choose in the dropdownmenu also the status that is already selected and thats not correct. I should diplay only all the other status which are not currently selected. Commented Dec 3, 2015 at 11:32
  • 1
    both the upvoted questions do exactly that Commented Dec 3, 2015 at 11:33
  • 1
    Hm, i think i can guess whats happening, but could you edit your question to show more code - eg the bit where you echo the string currently shown Commented Dec 3, 2015 at 11:48

4 Answers 4

2
<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value='new' <?if($row['status']=='new'){echo "selected";}?>>New</option>
        <option value='progress' <?if($row['status']=='progress'){echo "selected";}?>>Progress</option>
        <option value='wait' <?if($row['status']=='wait'){echo "selected";}?>>Wait</option>
        <option value='done' <?if($row['status']=='done'){echo "selected";}?>>Done</option>
        <option value='close' <?if($row['status']=='close'){echo "selected";}?>>Close</option>
    </select>
</td>
Sign up to request clarification or add additional context in comments.

Comments

2

check PHP variable with option value and if it is matched then add atttribute selected='selected' for that.

<option value='new' <?php if($row['status'] == 'new') { echo "selected='selected'" ; } ?> >New</option>
<option value='progress' <?php if($row['status'] == 'progress') { echo "selected='selected'" ; } ?>>Progress</option>
<option  value='wait' <?php if($row['status'] == 'wait') { echo "selected='selected'" ; } ?> >Wait</option>
<option value='done' <?php if($row['status'] == 'done') { echo "selected='selected'" ; } ?>>Done</option>
<option value='close' <?php if($row['status'] == 'close') { echo "selected='selected'" ; } ?> >Close</option>

Comments

1

As mentioned, you can check the value of $row['status'] and selectivly add the selected attribute to the correct option:

"<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value='new' ". ($row['status'] == 'new'? 'selected ': '') .">New</option>
        <option value='progress' ". ($row['status'] == 'progress'? 'selected ': '') .">Progress</option>
        <option  value='wait' ". ($row['status'] == 'wait'? 'selected ': '') .">Wait</option>
        <option value='done' ". ($row['status'] == 'done'? 'selected ': '') .">Done</option>
        <option value='close' ". ($row['status'] == 'close'? 'selected ': '') .">Close</option>
    </select>
</td>"

Comments

0

If you need to select specific option, you need to set selected attribute to option tag:

<option value='new' <?if($row['status']=='new'){echo "selected";}?>>New</option>

1 Comment

I need to select the otion which is stored in the database and I do it with <option value=" . $row['status'] . " >" . $row['status'] . "</option>. The problem is that I can choose in the dropdownmenu also the voice that is already selected.

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.