Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Can't send XMLHttpRequest

I am trying to delete data with vanilla javascript ajax. After sending POST request with an id value to php file. Every time it prints "error" in the console.

HTML

<div class="deleteButton" onclick="showAllert(this)" id="1">
  <i class="fa-solid fa-trash-can fa-2x" style="color:#A81E22" title="delete"></i>
</div>

JAVASCRIPT

 <script>     
        function showAllert(a) {

            // sent delete request
            var dataId = a.getAttribute("id");
            var dataId = parseInt(dataId);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'includes/vehicles/del-veh.php', true);
            // xhr.responseType = 'json';
            xhr.onload = () => {
                if(xhr.status === 200) {
                    console.log(xhr.response);
                    location.reload();
                }else {
                    console.log("There was a problem");
                }
            }
            var data = JSON.stringify({id: dataId});
            xhr.send(data);
                    
        }
    </script>

PHP

<?php
//Connection statement
require_once('../../Connections/ukcd.php');
$data = json_decode(file_get_contents('php://input'), true);

if(!empty($data["id"])) {
    // Required variables
    $vehid = $data['id'];
    echo "success";
}else {
    echo "error";
}

//AUTOEXECUTE METHOD
$delveh = $ukcd->prepare('DELETE FROM a_ukcd_client_vehicles WHERE clientveh_id = ?');
$result = $ukcd->execute($delveh, $vehid);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Send POST data in urlencoded format:

 <script>     
        function showAllert(a) {

            // sent delete request
            var dataId = a.getAttribute("id");
            var dataId = parseInt(dataId);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'includes/vehicles/del-veh.php', true);
            // xhr.responseType = 'json';
            xhr.onload = () => {
                if(xhr.status === 200) {
                    console.log(xhr.response);
                    location.reload();
                }else {
                    console.log("There was a problem");
                }
            }
            // var data = JSON.stringify({id: dataId});
            xhr.send(`id=${dataId}`);
                    
        }
    </script>

And in your PHP script:

<?php
//Connection statement
require_once('../../Connections/ukcd.php');
$id= $_POST['id'];

if(!empty($id)) {
    // Required variables
    $vehid = $id;
    echo "success";
}else {
    echo "error";
}

//AUTOEXECUTE METHOD
$delveh = $ukcd->prepare('DELETE FROM a_ukcd_client_vehicles WHERE clientveh_id = ?');
$result = $ukcd->execute($delveh, $vehid);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!