-2

I want to do some modifications on a table with AJAX.

In localhost all is allright but after transfer on production server, 500 Error.

I saw on other topics the problem should be on my .php or with my Apache server.

It's my first time that I use AJAX then I'm not comfortable with this technology.

For example, I want to delete an offer with the Delete button.

My .js :

//Action supprimer
$(".suppr").on("click", function(e){
    e.preventDefault();
    var conf=confirm("Cette offre sera supprimée, valider ?");

    if(conf==true){
        var id_promo=$(this).parent().parent().attr('id');
        console.log(id_promo);
        var ligne=$(this).parent().parent();
        var data="op=suppr&id_promo="+id_promo;
        var td=($(this).parent());

        td.html(ajax_loader);



        var request=$.ajax({
            url:"ajax_promo.php",
            method:"POST",
            data:data                
        });

        request.done(function(){
           console.log(request);
           ligne.empty(); 
        });

        request.fail(function(){
            console.log(request);

            console.log("Fail AJAX");
        });


    }

});

My .php

/************************ Action bouton "Supprimer" ***************************/

if($_POST["op"]=="suppr" && !empty($_POST['id_promo'])){
       $query="UPDATE professionnel_promo"
               . " SET online=2"
               . " WHERE id=".$_POST['id_promo'];

    if(sql_query($query)){
        echo "Delete OK";
    }
    else{
        echo "Fail Delete";
    }

}

Thanks for help :)

pnuts
  • 58,317
  • 11
  • 87
  • 139
AHAV
  • 31
  • 1
  • 8
  • 2
    `500` means error on your server. Debug your `PHP` script – Tushar Jun 18 '15 at 13:44
  • 1
    I try to do this since 1 hour. My queries are ok but it seems AJAX can't access to the PHP script... – AHAV Jun 18 '15 at 13:53
  • Check your `console` for error – Tushar Jun 18 '15 at 13:57
  • When i make a die() before request, code 200 => OK. Strange, I test my request and I don't see mistakes. – AHAV Jun 18 '15 at 13:58
  • I think `sql_query($query)`, check if function is valid – Tushar Jun 18 '15 at 13:59
  • It works in all of other scripts ;) – AHAV Jun 18 '15 at 14:04
  • Have you got a tip for good debugging a php script called by AJAX ? – AHAV Jun 18 '15 at 14:08
  • Is `sql_query` a custom function? If so, is the PHP file that declares it pushed out to your production site? – Shotgun Ninja Jun 18 '15 at 14:21
  • Yes it's a "custom function" and yes this file is on the prod server ;) – AHAV Jun 18 '15 at 14:25
  • I think it can't connect to my sql – AHAV Jun 18 '15 at 14:26
  • Server log : File(/lib/mysql.php) is not within the allowed path(s): (/var/www/vhosts/ns306815.ip-188-165-207.eu/:/tmp/) in /var/www/vhosts/ns306815.ip-188-165-207.eu/httpdocs/ajax_promo.php on line 11, referer: http://www.website.com/backoffice.php?op=professionnel_promo – AHAV Jun 18 '15 at 14:29
  • Debug a php page called with ajax in the same way you debug any other php page. An ajax request is not very different from visiting the page directly with your browser. – Kevin B Jun 18 '15 at 14:36

2 Answers2

-1

Solved problem ! It was a problem with SQL connection, I just changed permissions and it's ok.

Ty for all your replies :)

AHAV
  • 31
  • 1
  • 8
-2

I would rewrite

 var data="op=suppr&id_promo="+id_promo;

with:

 var data='{op: "suppr", id_promo : "' + id_promo '"}';

Simply because the former is passed throught _GET superglobal, the latter with _POST

Suat Hyusein
  • 495
  • 3
  • 14
  • I changed but the 500 Error is here again. But I think you're right, then I will replace my vars like you tell me. – AHAV Jun 18 '15 at 14:15
  • Never build json manually, and, the first version will still send POST params, if the method is POST. objects sent to the data property will still be parameterized down to the query string format, even for post requests, unless they are already in string format (for example, when posting JSON) – Kevin B Jun 18 '15 at 14:37