0

I have a file named index.php in which My Ajax is-->

    <script>
    $(document).ready(function() {  
        $("#tokensubmit").submit(function(e){
            var id = $("#id").val();
                $.ajax({
                    type: "POST",
                    url: "requests/abcd.php",
                    data: {login:true,id: id},
                    success: function(response){
                                alert(response);
                            }
                        }
                }); 
            e.preventDefault();
        }); 
    }); 
</script>

& OTHER FILE I HAVE WHICH IS "abcd.php" where i am sending ajax Request. abcd.php is like this ->

<?php  include 'xyz.php'; ?>

In xyz.php if i type

<?php echo "abcd ";?>

My ajax is not working ! What i want to say is PHP INCLUDE is NOT working with Ajax Request.

But without including xyz.php in abcd.php If i write the code directly which is in xyz.php without including .. Everything is working fine..

But i want to include the file. How can i do that ? please help..

4
  • 2
    Hi and welcome to Stack Overflow. Please provide a little more information. Your xyz.php file, where is it located? Commented Nov 6, 2018 at 16:33
  • in your abcd.php file, is INCLUDE the first instruction? Commented Nov 6, 2018 at 16:34
  • There are multiple possible explanations. Maybe the PHP script fails with some error and you have your environment set to supress those errors. Maybe the include path isn't correct. Maybe something else, but with correct paths your code is working on my machine Commented Nov 6, 2018 at 16:35
  • The problem is solved ! if you are calling abcd.php through ajax then abcd.php will require an absolute path . Commented Nov 6, 2018 at 17:32

1 Answer 1

6

If you are calling a file using ajax, then to include any other files on that file (abcd.php) will require an absolute path. For example :

require $_SERVER['DOCUMENT_ROOT'].'/file_folder/xyz.php';
Sign up to request clarification or add additional context in comments.

7 Comments

Whether it's called with ajax or via the address bar in the browser has no impact on the path needed in the backend.
@Kevin B So what is your answer?
There is no answer, the OP hasn't included enough information for one to be provided.
This downvoted answer was the solve from a Google search, so upvote it gets!
@SulungNugroho Your answer solved my problem, so 1 up-vote. Thank you very much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.