通过Javascript设置隐藏字段值

时间:2014-02-08 23:14:20

标签: javascript hidden-field bootstrap-modal

如何为Javascript函数传递标记< a >的USER_ID?设置隐藏字段的Javascript?

我尝试了Javascript函数showConfirm(),但隐藏字段未填充。它保持为null ...我认为字段deleteid的onClick属性写错了。

我想通过javascript填写此字段。我怎么能够?

错误信息说

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近''第1行

可能是因为隐藏字段"id_action"没有接收到函数的数据。

无论如何,主要问题是通过参数showConfirm为隐藏字段'show', $row['id']设置函数id_action

           <script>

            function showConfirm (action, delID) {
              if (action == 'show') {
                document.confirm.id_action.value = delID;
              }
            }
            </script>


             <tbody>
               <?php
                   if (!empty($_POST)) {
               if (isset($_POST['id_action'])) {
                $del_id   = $_POST['id_action'];
                    $delUser  = new User;
                    $delUser->deletarUsuario($del_id);
                    unset($delUser);
                }
            }
                 $listUser    = new User;
                 $result     = $listUser->listarUsers();
                 if (is_array($result)) {
                  foreach ($result as $row) {
                    echo "
                     <tr>
                       <td align='right'>" . $row['id'] . "</td>
                          <td>". $row['name'] . " ".$row['sobrename']."</td>
                          <td>" . $row['email'] . "</td>
                          <td>" . $row['login'] . "</td>
                          <td>
                              <a data-toggle='modal' id='deleteid' data-target='#modal_delUser' onclick=\"showConfirm('show'," . $row['id'] . ")\">
                              Remove
                              </a>
                          </td>
                     </tr>"; 
                  }
                } 
                unset($listUser);
              ?>
             </tbody>


                     <!-- Button trigger modal -->
              <div class='modal fade' id='modal_delUser' tabindex='-1' role='dialog' aria-labelledby='modal_delUserLabel' aria-hidden='true'>
              <div class='modal-dialog'>
                <div class='modal-content panel-danger'>
                  <div class='modal-header panel-heading'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                    <h4 class='modal-title' id='modal_delUserLabel'>The user will be deleted</h4>
                  </div>
                  <div class='modal-body'>
                    Are you sure continue?
                  </div>
                   <div class="modal-footer">
                    <form role="form" id="confirm" action="users.php" method="post">

                      <input type="hidden" name="id_action">  

                      <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                      <button type="submit" class="btn btn-danger">Yes</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>

这是我从查询中生成的SQL

public function deletarUsuario($id) {
    $del_id     = $this->db->real_escape_string(trim($id));
    if ($update = $this->db->query("DELETE FROM usuario WHERE id = $del_id")) {
        if ($this->db->affected_rows) {
            echo "<div><p>Deleted user!</p></div>";
        }
        else {
            echo "<div><p>Failed to delete user.</p></div>";
        }

    }
    else {
        echo "<div><p>". $this->db->error."</p></div>";
                echo "<script>$('#modal_erroBD').modal('show');</script>";
    }

}

1 个答案:

答案 0 :(得分:0)

使用$id_action = $_POST["id_action"]

在php中执行此操作

检查您是否在此阶段传递了ID:

变化

             if (isset($_POST['id_action'])) {
                $del_id   = $_POST['id_action'];
                    $delUser  = new User;
                    $delUser->deletarUsuario($del_id);
                    unset($delUser);
             }

             if (isset($_POST['id_action'])) {
                $del_id   = $_POST['id_action'];
                    echo $del_id;
             }