0

I have a requirement to display contents of a file after clicking it. I am using javascript to call the php function in same file using Ajax. I know we cannot call php function using Ajax but i am trying to call the same file and in php file i am checking the isset() for the parameter which i am sending through ajax. I dont know whats wrong but the code is not working. Can someone please assist me>=?

PHP code am using...

    if(isset($_POST['param3'])){
           echo "I am in displaying data successfull ajax call";
    }

My ajax:

$.ajax({
    type:'POST',
    url : 'surveypanel.php',
    data : {param3:"asdf"},
    success : function(data){
        //
    }
});

Thanks, Kshan

8
  • I dont see you trying to make any function call in your php code, also your js code is invalid Commented Mar 30, 2018 at 2:42
  • You never do anything with data in success : function(data){ Commented Mar 30, 2018 at 2:43
  • Patrick, As far as i know we cannot call php function from ajax. So am calling the same file and checking isset. Yeah may be i kept a wrong title. But my intenestion is to execute some fraction of php code after the click. So i mentioned it as function. Commented Mar 30, 2018 at 2:45
  • in your php code you can call any function your php code has access to, ie user defined, built-in, functions from included files, etc Commented Mar 30, 2018 at 2:48
  • 1
    You would call it in your php file that you are requesting in your ajax request ie, if(isset(..)){ somefunction(); } Commented Mar 30, 2018 at 2:53

1 Answer 1

2

try this:

$.ajax({
    type:'POST',
    url : 'surveypanel.php',
    data : {param3:"asdf"},
    success : function(data){
        // use data for get response from surveypanel.php
        alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. But i have some code fragment to be executed after calling the same file. I cannot excutet that.
@kshan, you didn't mention about that code fragment ? Share detail , although this is the answer of your question.
Apologies for the confusion. if(isset($_POST['param3'])){ echo "Successfully called same file"; } I have to exectute this after the click. This code is in same php file where i am writing the click. I am checking the parameter "param3" which is calling from ajax call. Let me know if still this is not clear...

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.