Search code examples
phpjqueryjquery-post

PHP + jQuery - undefined index: , but it still posts successfully?


When inserting a record to a database, the server returns an 'undefined index: category', error, but still posts the successfully.

   $('#button').click(function(){
    var newvendor    = $('#input_vendor').val();
        newplantcode = $('#input_plantcode').val();
        newcategory  = $('#input_category').val();

$.post("php/addSite.php",
    {vendor: newvendor, 
     plant_code: newplantcode, 
     category: newcategory}, // <--- Error on this line, for some reason...


     function(result){
            console.log("server returned : " + result);
            [ RELOAD THE PAGE ]
     }

Solution

  • You having missing quote in almost all your code:

       $('#button').click(function(){
    var newvendor    = $('#input_vendor').val();
    var newplantcode = $('#input_plantcode').val();
    var newcategory  = $('#input_category').val();
    
    $.post("php/addSite.php",
    {vendor: newvendor, 
     plant_code: newplantcode, 
    category: newcategory}, // <--- Error on this line, for some reason...
    
    
     function(result){
            console.log("server returned : " + result);
            [ RELOAD THE PAGE ]
     }
     //closing the post function
     )
     //closing the click event 
     });
    

    Now try that again