1

I've been struggling for a while:

This is part of my AJAX code:

<script>
        function liked(button, dataset){
            var element = button;
            element.classList.toggle("liked");

            var xhttp;
            if (dataset == '') {
                document.getElementById('like-count-' + dataset).innerHTML = "Null dataset!";
            }
            xhttp = new XMLHttpRequest();

            xhttp.onreadystatechange = function() {
                if (xhttp.readyState === 4 && xhttp.status === 200) {
                    document.getElementById('like-count-' + dataset).innerHTML = this.responseText;
                }
            };

            xhttp.open('POST', '<?php echo base_url('update_like_count')?>', true);
            xhttp.setRequestHeader('X-Requested-With', 'XMLHTTPRequest');

            //var data = 'dataset_name=' + encodeURIComponent(dataset);
            //var data = JSON.stringify({ dataset_name: dataset });
            console.log(dataset);
            xhttp.send('dataset_name='+dataset);
        }
    </script>

This is part of my PHP code:

public function update_like_count() {
        //Debug
        $dataset_name = $this->request->getPost('dataset_name'); 
        echo "dataset: ".$dataset_name; // $dataset_name is null
        
        if ($this->request->isAJAX()) {
            $dataset_name = $this->request->getPost('dataset_name');
            echo gettype($dataset_name);
            if($dataset_name) {
                $model = model('App\Models\Upload_model');
                $model->update_like_count($dataset_name);
                echo 'Success!';
            } else {
                echo 'Dataset not found!';
            }
        }
    }

It seems the $dataset_name is always null on the server side. I'm appreciated for any help.

Zhixuan Li
  • 11
  • 1
  • 1
    _"It seems the $dataset_name is always null on the server side."_ - just to be clear - you are actually getting the `echo "dataset: ".$dataset_name;` from your PHP script, only the part after the colon is empty? And you are not just talking about the error message you are already setting on the client side, before the request is even made? `[...].innerHTML = "Null dataset!";` – CBroe May 12 '23 at 09:24
  • Sorry, I should have made this clearer. Yes, it is the `$dataset_name` that is actually NULL as `echo "dataset: ".$dataset_name; ` echoes `"dataset: NULL"`. And since `$dataset_name` is null the rest bit of php code echoes `"Dataset not found!"`; I'm not talking about the error message `"Null dataset"` merely because it doesn't show up on the client side. – Zhixuan Li May 12 '23 at 10:11
  • What do you see when you inspect the request using your browser dev tools, is the parameter present there? Do you get a direct 200 OK response, or are there perhaps any redirects happening? – CBroe May 12 '23 at 10:27
  • The request status is 200 without any outstanding warning or error messages. And the console prints the correct dataset name by `console.log(dataset);`. So I reckon it might has something to do wtih the way I send the dataset name by `xhttp.send('dataset_name='+dataset);`? Or the way I retrieve the data by `$this->request->getPost('dataset_name');`? – Zhixuan Li May 13 '23 at 01:36
  • Hi CBore, thanks for your response, even though I still couldn't understand why it doesn't work, I figured out another way to send the data: `xhttp.open('GET', '?q='+dataset, true);` instead and that solves the problem. – Zhixuan Li May 13 '23 at 03:50
  • You need to set a `content-type` header, like `setRequestHeader("Content-Type","application/x-www-form-urlencoded"` – Michel May 13 '23 at 08:12

0 Answers0