dtng5978 2019-04-13 13:45
浏览 80

如何使用php ajax javascript根据选定的下拉项更改表的颜色

I am fairly new to php and even newer to PDO! what I am trying to do is change the color of the row based on selection of a drop down in my form, but I do not have a clue how to do this!

I have only been googling around for a bit about this, have not found anything that is close to what I am trying to do

here is the main table

 <div class="row">
    <div class="large-4 columns">
      <label>Current Status
        <select name="current_status">
            <option>Select Status</option>
            <?php foreach($statuses as $key => $value) : ?>
                <option value="<?php echo $key; ?>"><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>

here is the "key" code

'InProgress'=>'In Progress',
'Completed'=>'Completed',
'Unstatused'=>'Unstatused'

here is the contoroller page code

  <div class="large-4 columns">
      <label>Select Current Status
          <select name="current_status">
            <?php foreach($statuses as $key => $value) : ?>
                <?php 
                    if($key == $task->current_status) {
                        $selected ='selected';
                    }else{
                        $selected='';
                    }
                ?>
                <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>
    </div>

     <input type="hidden" name="id" value="<?php echo $task->id; ?>">
     <input name="submit" type="submit" class="add_btn button right small" value="submit"> 
     <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</form>
</div>

so what I am hoping can be done here is; if "completed" was selected in the forms dropdown - then I want to change the row color that holds that task to red, and if they select "unstatused" i want to have that table row change to blue..but i do not have a clue as to how to do this!

    <table><!--start table-->
            <thead><!--create the headers-->
                <tr><!--the table row for the table headers-->
                    <th width="100">task date</th>
                    <th width="450">task title</th>
                    <th width="150">assigned to</th>
                    <th width="100">current status</th>
                    <th width="200">action</th>
                </tr><!--close the table header table row-->
             </thead><!--close the headers-->
                <tbody><!--begin the table body-->
                <?php foreach($tasks as $task) : ?>
                    <tr>
                        <td><?php echo $task->task_date; ?></td>
                        <td><?php echo $task->task_title; ?></td>
                        <td><?php echo $task->assigned_to; ?></td>
                        <td><?php echo $task->current_status; ?></td>

                       <td>
                        <ul class="button-group radius">
<li>
<a href="#" class="button tiny" data-reveal-id="editModal<?php echo $task->id; ?>" data-task-id="<?php echo $task->id; ?>">Edit</a>
<div id= "editModal<?php echo $task->id; ?>" data-cid= "<?php echo $task->id; ?>"  class="reveal-modal editModal" data-reveal>

the row I want to change is the "current_status" row - based on if they selected "completed" or "In Progress" in the form that is used to populate the table.

JEFF- here is the full code page (the controller page)

<?php
//create database object
$db = new Database;
//bring in the contacts
$db->query("SELECT * FROM multytasklist");
//assign the resultset
$tasks = $db->resultset();
?>

 <div class="row"><!--begin the row for table data-->
    <div class="large-12 columns"><!--begin columns-->
        <table><!--start table-->
            <thead><!--create the headers-->
                <tr><!--the table row for the table headers-->
                    <th width="100">task date</th>
                    <th width="450">task title</th>
                    <th width="150">assigned to</th>
                    <th width="100">current status</th>
                    <th width="200">action</th>
                </tr><!--close the table header table row-->
             </thead><!--close the headers-->
                <tbody><!--begin the table body-->
                <?php foreach($tasks as $task) : ?>
                    <tr>
                        <td><?php echo $task->task_date; ?></td>
                        <td><?php echo $task->task_title; ?></td>
                        <td><?php echo $task->assigned_to; ?></td>
                        <td><?php echo $task->current_status; ?></td>

                       <td>
                        <ul class="button-group radius">
<li>
<a href="#" class="button tiny" data-reveal-id="editModal<?php echo $task->id; ?>" data-task-id="<?php echo $task->id; ?>">Edit</a>
<div id= "editModal<?php echo $task->id; ?>" data-cid= "<?php echo $task->id; ?>"  class="reveal-modal editModal" data-reveal>

    <h2>EDIT TASK</h2>

   <form id="edit_multytask" action="#" method="POST">

  <div class="row">
    <div class="large-6 columns">
      <label>Task Date
        <input name="task_date" type="text" placeholder="task date.." value="<?php echo $task->task_date; ?>">
      </label>
    </div>

     <div class="large-6 columns">
      <label>Task Title
        <input name="task_title" type="text" placeholder="task title.." value="<?php echo $task->task_title; ?>" >
      </label>
    </div>
  </div>

  <div class="row">
    <div class="large-6 columns">
      <label>Task Description
        <input name="task_description" type="text" placeholder="task description.." value="<?php echo $task->task_description; ?>" >
      </label>
    </div>
    </div>


     <div class="row">
     <div class="large-12 columns">
      <label>assigned to
        <input name="assigned_to" type="text" placeholder="assigned to.." value="<?php echo $task->assigned_to; ?>" >
      </label>
     </div>
     </div> 


   <div class="large-4 columns">
      <label>Select Current Status
          <select name="current_status">
            <?php foreach($statuses as $key => $value) : ?>
                <?php 
                    if($key == $task->current_status) {
                        $selected ='selected';
                    }else{
                        $selected='';
                    }
                ?>
                <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>
    </div>

     <input type="hidden" name="id" value="<?php echo $task->id; ?>">
     <input name="submit" type="submit" class="add_btn button right small" value="submit"> 
     <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</form>
</div>
</li>

<li>
    <form id="delete_multytask" action="#" method="post">
        <input type="hidden" name="id" value="<?php echo $task->id; ?>">
        <input type="submit" class="delete_btn button tiny secondary alert" value="delete">
    </form>
</li>
                        </ul><!--close unordered list-->

                       </td><!--close table data-->
                    </tr><!--close table row-->
                   <?php endforeach; ?>
                 </tbody><!--close tbody-->
             </table><!--close table-->
         </div><!--close 12 column class-->
        </div><!--close row-->
  • 写回答

1条回答 默认 最新

  • doumei1203 2019-04-13 14:14
    关注

    This is happening dynamically so php and pdo are inappropriate in this situation. Your css needs to be:

    <style>
    .InProgress { background-color: red; }
    .Completed { background-color: orange; }
    .Unstatused { background-color: blue; }
    </style>
    

    You need to change the <select> item to:

    <select name="current_status" onchange='changeColors(this.value)'>
    

    Add some JavaScript:

    <script>
    function changeColors( value ) {
      var element = document.getElementById('myElementToChange');
      element.className = value;
    }
    

    Please note that this is a rough guideline as you have not provided the full html so I can only guess at what you want to change.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记