skip to Main Content

Main.php

<script>
$(document).ready(function(){
    searchUser();
});

function searchUser() {
    alert("aaaaaaaa");
    var data = $("#user-search-form").serialize();
    $.ajax({
            type: "POST",
            url: "test.php",
            data: data,
            success: function(response) {
                alert("bbbbbbbb");
            }
    });

    alert("cccccccc");

    return false;
}

test.php

<?php echo "testing 1234" ?>

Directory:

  • phpturtorial/admin/main.php
  • phptutorial/admin/test.php

I am calling php function using ajax but not working. My code able to alert “aaaaaaaa” and “cccccccc” but cannot alert “bbbbbbbb”. Any idea ? is it related to my incorrect path ?

2

Answers


  1. phpturtorial/admin/main.php and phptutorial/admin/test.php are in two different directories.
    Hence why it is not able to locate test.php.

    Change url: "test.php" to url: "/phptutorial/admin/test.php"

    Login or Signup to reply.
  2. With the Given Information:

    Case 1:

    You need to modify the ajax request url slightly.

    $(document).ready(function(){
        searchUser();
    });
    
    function searchUser() {
        alert("aaaaaaaa");
        var data = $("#user-search-form").serialize();
        $.ajax({
                type: "POST",
                url: "phptutorial/admin/test.php", //or the path to test.php
                data: data,
                success: function(response) {
                    alert("bbbbbbbb");
                }
        });
    
        alert("cccccccc");
    
        return false;
    }
    

    Case 2:

    Provided path could be wrong as they are identical.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search