Jimmy
Jimmy

Reputation: 137

PHP function cannot be called

I will try to explain my problem in a simple way. My code looks like these.

A web page, systemView.php, has following code

<?php require_once 'Common_Function.php'; ?>
<head>
    <script src="jF.js"></script>
</head>
<html>
     <div id="one"> <?php A(); ?> </div>
     <div id="two">  </div>
</html>

Common_Function.php is

<?php
    include 'systemView_Function.php';
    include 'database.php';
?>

systemView.php has A() as well as B()

<?php
    function A() {
        D(); // a function in database.php
        //and show a few button on the page
    }
    function B($number) {
        D(); // a function in database.php
        //some codes to show something in the page
    }
    if (isset($_POST['B'])) { 
        B($_POST['B']); 
    }
?>

A() function will show some buttons on the page when called. When one of the buttons is clicked. javascript will then call B() by ajax. This function in js is

$(".abutton").click(function(){
    $("#two").empty();
    var number = parseInt($(this).text());
    $.ajax({
        url: "./systemView_Function.php",
        type: "POST",
        data: {"B": number},
        dataType: "JSON",
        success: function(data) {   
            $("#two").append(data);
        }
    });
})

Now the problem is when D() is not included in B(), the codes run successfully and shows something in id="two" div. However, if D() is included in B(), console in browser shows errors, and nothing in second div.

Upvotes: 0

Views: 51

Answers (1)

TZHX
TZHX

Reputation: 5377

My guess is that, because you don't reference database.php in your systemView.php page, PHP cannot find the D() function and so throws an error.

A() runs because it is called from a page that has it in scope (from CommonFunctions.php), but when B() is called -- being a new request -- it doesn't know what to look for.

Upvotes: 1

Related Questions