skip to Main Content

Sorry for disturbing again with my very basic question. First of all, sorry if my English is a little bit hard to understand. My current situation is I want to do a popup modal in my drag and drop boxes. In my popup modal, I can view and edit the details of the user based on what we click in the button in the box. The problem is, I cannot SELECT the data by id. But, when I SELECT all the data, the data appear in the modal boxes. But, it appears all the data. I just want the selected id. Back to my question for past few days, I’ve redo again to get more understanding on this popup modal part. I’ve done ajax and a little bit JavaScript, also, I tried to debug my code just what I’ve been told but I got an error saying "Parameter is missing" . What is causing by that ? I’ve done some reading about parameter but I still don’t get the actual understanding about it. Can someone give an idea what is actually parameter is missing . And what I suppose to do by it?

Here what I’ve tried so far.

This is the button

<button data-id="<?php echo $row['userid'];?>" data-target="doubleClick-1" class='jobinfo' type='button' id='btnInfo' ondblclick="document.getElementById('doubleClick-1').style.display='block'">Info</button>

This is the modal popup

 <div id="doubleClick-1" class="modal">
      <label class="tabHeading">User Info</label>
        <div class="contentTechJobInfo">
        <div class="tech-details">
        <div class="techClose" onclick="document.getElementById('doubleClick-1').style.display='none'" >&times</div>

        </div>    
        </div> 

        </div>
        </div>
        
 
   <script type='text/javascript'>
    
    $(document).ready(function() {
    $('.jobinfo').click(function() {

    var userid = $(this).data('userid');

    // AJAX request
    $.ajax({
    
        url: 'ajaxhome.php',
        type: 'post',
        data: {userid: userid},
        success: function(response) {
     // Add response in Modal body
        $('.tech-details').html(response);
    // Display Modal
        $('#doubleClick-1').modal('show');
        }
        });
         });
         });

</script>

This my ajaxhome.php

<?php
    $connection = mysqli_connect("", "", "");
    $db = mysqli_select_db($connection, '');

  

        if (!isset($_GET['userid'])) {
  die("Parameter is missing!");  
}

$userid = intval($_GET['userid']);

        $query = "SELECT * FROM user WHERE userid ='$userid'";
      
        $query_run = mysqli_query($connection, $query);
        if ($query_run) {
            while ($row = mysqli_fetch_array($query_run)) {
                ?>

                 
     <div class="input-box">
            <label for="">Name</label>
            <input type="text" id="username" name="username" value="<?php echo $row['username']?>">
        </div>
        <div class="input-box">
            <label for="">Number</label>
            <input type="text" id="usernumber" name="usernumber" value="<?php echo $row['usernumber']?>">
        </div>
        <div class="input-box">
            <label for="">Class</label>
            <input type="text" id="userclass" name="userclass" value="<?php echo $row['userclass']?>">
        </div>
       
 
 <button type="submit" id="submit" name="update" class="btn btn-primary"> Update Data </button>               
      
                    <?php

                    if (isset($_POST['update'])) {
                        $username = $_POST['username'];
                        $usernumber = $_POST['usernumber'];
                        $userclass = $_POST['userclass'];
                       

                        $query = "UPDATE user SET username='$username', usernumber='$usernumber', userclass='$userclass' WHERE userid='$userid'";

                        $query_run = mysqli_query($connection, $query);

                        if ($query_run) {
                            echo '<script> alert("Data Updated"); </script>';
                            header("location:homepage.php");
                        } else {
                            echo '<script> alert("Data Not Updated"); </script>';
                        }
                    }
            } ?>

    <?php
        }

    ?>

2

Answers


  1. I don’t think the value of user has been obtained
    var userid = $(this).data('userid');
    you can try
    var userid = $(this).data('id');

    Login or Signup to reply.
  2. In short, I think the problem comes from these lines of code in your modal:

    var userid = $(this).data('userid');
    

    you should replace it with

    var userid = $(this).data('id'); // you should pass 'id' to .data() function instead of 'userid'
    

    With your current code userid variable in your modal will always be undefined. It means it wont exist in $_GET when you send ajax request to PHP. And it causes your ajaxhome.php moves to die("Parameter is missing!");.

    To get data-xxx attribute with jQuery, you should use pass ‘xxx’ to .data() function.

    var xxx = $(this).data('xxx');
    

    In your button, you are storing userid in data-id attribute

    <button data-id="<?php echo $row['userid'];?>"
    

    so if you need to get that userid you should pass 'id' into .data() function

    Update:
    In your ajax, you are using type: 'post', so in your php code you should check $_POST instead of $_GET

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search