2014-02-08 124 views
0

如何將標記< a >的USER_ID傳遞給設置隱藏字段的Javascript函數Javascript?使用Javascript設置隱藏字段值

我試了Javascript函數showConfirm(),但隱藏的字段沒有被填充。它保持爲空......我認爲字段deleteid的onClick屬性寫錯了。

我想用javascript填充此字段。我怎麼能夠?

錯誤的消息稱

您的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 「」附近的1號線

可能是因爲隱藏字段"id_action"不接收功能的DATAS。

無論如何,主要的問題是設置功能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> 

這裏,我從查詢使用$id_action = $_POST["id_action"]

檢查如果傳遞的ID在這個階段產生

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>"; 
    } 

} 

回答

0

做在PHP中的SQL:

變更

   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; 
      } 
+0

是的,我已經做了 – AlexDoria

+0

告訴我們,你是從查詢產生,因爲它不是在上面的代碼中的SQL。 – malta

+0

對不起,我是新來的。有... – AlexDoria

相關問題