0

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
        }

    ?>
1
  • What have you tried to resolve the problem? Where are you stuck? Also, be warned that your SELECT query is widely open for SQL injection - please have a look at prepared statements to avoid getting hacked Commented Jan 18, 2022 at 7:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thank you sir for your time answering this question. I've change var userid = $(this).data('userid'); TO var userid = $(this).data('id'); . but it still gives me an error :((
@teachmelearning I also update at the end of my answer about your $_GET stuff. Please check.
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');

3 Comments

my englist is a little bit hard to understand too
its okay :D . i've try to change it but it still gives me an error. I'm pretty much out of ideas now
i've got it. thank you so much for your time :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.