1

When I press a button with some functionalities(in php code) I don't understand why I receive sometimes error, sometimes succes from ajax. I have one more button which have almost same structure of code to the bottom. So, I have 2 ajax codes with same structure(but different names of variables or files). Is this important ?

Ajax:

function plan_tarifar_submit_anulare() {
    var id = get_user_id_anulare();
    var myURL = "./UpdateAnularePlanTarifar.php";
    $.ajax({
        timeout: 5000,
        type: "POST",
        url: myURL,
        data: {id:id},
        error: function(xhr) {
            alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' -> ' + xhr.responseText);
            parent.location.reload();

        },            
        success: function (xhr) {
            alert('IT WORKS !!' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
            parent.location.reload();
        }
    });
    return false;
}

php:

     <?php
    /**
     * Description of updateAnularePlanTarifar (...)
     */
    require_once("../includes.inc.php");
    $cookie_value = assure_login_check();

    if (!isset($_POST['id'])) {
        return;
    }
    $user_id = htmlspecialchars($_POST['id']);
function getOldIstoric($id){
   (.....)   
    return $istoric;
}
function anularePlanTarifar($id) {
   (.....) 
}
anularePlanTarifar($user_id);
?>

Also, after error the page can't reload: unloaded page

I think that the id is missing at the end of url.

  • If the problem is only intermittent then it's most likely a server issue. Have you checked the response when the request fails to see if it contains any error messages? Also note that reloading the page after making an AJAX request pretty much makes the request redundant. – Rory McCrossan Feb 13 '17 at 13:40
  • This is not a valid url: "./UpdateAnularePlanTarifar.php" this is a file path but not something you would use as URL. a url would look like: {host}/{path}/UpdateAnularePlanTarifar.php or just "/UpdateAnularePlanTarifar.php" if that file is in the web root – Auris Feb 13 '17 at 13:41
  • 2
    @Auris that's not true at all. `./` is a perfectly valid path construct: http://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location – Rory McCrossan Feb 13 '17 at 13:41
  • @RoryMcCrossan the error message doesn't contain anything. – George Liviu Neculita Feb 13 '17 at 13:44
  • In which case I'm afraid there's not really enough information to help you. You need to try and reproduce the error and then diagnose the state of the logic when it occurs. – Rory McCrossan Feb 13 '17 at 13:46
  • @ Rory McCrossan even if some browsers are able to handle that, this kind of pathing is reserved for file system and not for url. It should never ever ever be used. As for OP, 1) turn on your error reporting 2) echo something in your destination page and see if that shows up. 3) go to destination page manually (via browser and see if you get errors there) – Auris Feb 13 '17 at 13:56
  • @Auris Is destination page a php file? how can i access from browser the php code from my destination file? – George Liviu Neculita Feb 14 '17 at 08:46
  • I added **async:false,** in ajax function and now it works. I don't have errors anymore. I think that was a problem from the server. Now if I wait the response (synchronous) it's succes in ajax. But the problem with the blank page still remains. It' s missing the parameter at the end: http://localhost/abcd/asd-asd-asd/Updates/**Updates.php?** . – George Liviu Neculita Feb 14 '17 at 08:55

1 Answers1

0

Depends on how your html code looks like. But I imagine there is something in a form tag.

Check if you have those attributes for the form tag:

<form action="#" method="post" onclick='plan_tarifar_submit_anulare();' id='yourID'> <!--something--> </form>

It should look like this, or similar.

Let me know if it works. If you still have problems, please show your html code.

Newbie
  • 38
  • 1
  • 11