Question

How can I pass USER_ID of the tag < a > for a Javascript function Javascript that set a hidden field?

I tried the Javascript function showConfirm(), but the hidden field isn't being filled. It stays null... I think the onClick attribute, of the field deleteid, is wrote wrong.

I want fill this field by javascript. How can I?

The message of error says

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

probably because the hidden field "id_action" isn't receiving datas of the function.

Whatever, the main problem is set the function showConfirm by paramters 'show', $row['id'] for the hidden field 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>

Here the SQL that I am generating from the query

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

}
Was it helpful?

Solution

Do it in php by using $id_action = $_POST["id_action"]

Check if you are passing the id at this stage:

change

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

to

             if (isset($_POST['id_action'])) {
                $del_id   = $_POST['id_action'];
                    echo $del_id;
             }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top