Tejaswi
Tejaswi

Reputation: 33

Unable to call a PHP file using $.post() method of Ajax

I am trying to call php file located in the same directory as HTML using $.post() method of Ajax. however, i am getting a file not found(404) error.

below is the code I am using.

$(document).ready(function(){
        $("#formSubmit").on("click",function(){             

            alert("in iquery");
            $.post("./path.php",                        
                    function(){
                        alert("success1");
                        }                       
            );
        });
    });

I have tried calling other php file located in the subdirectory with "./PHP/path2.php". Its resulting in the same error.

Please help.

Upvotes: 0

Views: 260

Answers (1)

DavideR
DavideR

Reputation: 570

If the php file is in the same dir, then $.post("path.php", function() { alert("success"); }); without ./ should work. Also, handle the submit event on the form, which is better than the click event on the submit button, and make sure to disable the default behavior of the submit event with event.preventDefault() or a return false after the AJAX call.

Upvotes: 1

Related Questions