i have done this ajax script so i can delete a page. it works fine the page is deleted from the database but after clicking delete the browser remains on the same page. after i refresh i get a 404. how can i redirect after success to first page?

this is the script

jConfirm('Sure you want to delete this?', '', 
    function(r) 
    {
        if(r==true)
        {
            $.ajax({
            type: "POST",
            url: "delete_ajax.php",
            data: dataString,
            cache: false,
            success: function(html){
            alert(dwjlddhjds);
            }
        });
    } 

Dani AI

Generated

The database delete happening while the client never redirects usually means the XHR reached the server but the client-side success path never completes. As already suggested, confirm whether the success handler is actually being reached. As suggested, return a clear, machine-readable response from the server — prefer JSON so the client can make a reliable decision.

Check list (in order):

  • Inspect the XHR in the browser DevTools Network tab: look at the request, the HTTP status, and the Response body. A 500 (or other non-2xx) will prevent the normal success path.
  • Add client-side error handling and logging so failures are visible in the console instead of silently ignored.
  • Ensure the server emits only the expected JSON (no PHP warnings, stray HTML, or BOM). PHP notices can corrupt the response even after the DB work runs.

Example client pattern (uses modern fetch; avoids the jQuery success ambiguity):

fetch('delete_ajax.php', {
  method: 'POST',
  body: new URLSearchParams({ groove_id: ID })
})
.then(resp => { if (!resp.ok) throw new Error('HTTP ' + resp.status); return resp.json(); })
.then(json => { if (json.status === 'ok') location.assign('/'); else console.error('delete failed:', json); })
.catch(err => console.error('AJAX error:', err));

Server-side (PHP) should explicitly return JSON and stop further output:

header('Content-Type: application/json; charset=utf-8');
// ...perform delete...
if ($deleted) echo json_encode(['status'=>'ok']);
else { http_response_code(500); echo json_encode(['status'=>'error','message'=>'delete failed']); }
exit;

Other common pitfalls: binding with deprecated .live/.die can be fragile — use delegated .on('click', selector, handler); undefined variables inside a success callback will throw and abort any redirect (the original alert(dwjlddhjds) would do that); and sending a Location header from the server on an XHR will not navigate the top window — return a status and let the client script redirect.

Recommended Answers

All 9 Replies

try...

$.ajax({
   type: "POST",
   url: "delete_ajax.php",
   data: dataString,
   cache: false,
success: function(html){
   window.location.replace("http://www.domain.com");
}

still the same, dude...

$('.delete').die('click').live("click",function() 
{
    var ID = $(this).attr("id");
    var dataString = 'groove_id='+ ID;

    jConfirm('Sure you want to delete this?', '', 
    function(r) 
    {
        if(r==true)
        {
            $.ajax
            ({
                type: "POST",
                url: "delete_ajax.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    window.location.replace("http://localhost/");
                }
            });
        } 
    });
    return false;
});




<a class="delete" href="#" id="<?php echo $groove_id;?>" rel="<?php echo $groove_id;?>" title="Delete">Delete</a>

the whole script if it helps

Ok, so if you tried window.location.replace("http://localhost/"); on its own, you'll find that it does initiate a redirect. Are you sure that in your code, you are reaching the "success" point in the ajax method? You can easily test your code by logging to the console (console.log("x")), or by using the alert method (i prefer logging to the console log).

sorry for the delay JorxeM. No you are right it doesnt reach the success. Neither the alert or (console.log("x")) work. But it deletes from database. What should i do?

Focus on your server side code and break it up into chunks so you can figure out where the failure is. Is there code after the DB part that succeeds?

Can you run the server side page directly with a browser to get error information?

Sorry, I'm an asp.net web dev..very light on php

ok thanks i;ll try to find it

in your server side script try to return an output in case of success ex. php
echo "done";
in jquery

success: function(response) {
                if (response == "done"){
                window.location.href = "";
                    }else{
                alert ("some error happened");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.