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

JoinCompany
  • 504
  • 2
  • 11
  • I dont see you trying to make any function call in your php code, also your js code is invalid – Patrick Evans Mar 30 '18 at 02:42
  • You never do anything with `data` in `success : function(data){` – Sean Mar 30 '18 at 02: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. – kshan 2k18 Mar 30 '18 at 02: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 – Patrick Evans Mar 30 '18 at 02:48
  • Yes exacty. But how to call the php function after a js click. – kshan 2k18 Mar 30 '18 at 02:49
  • Did you check: https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript . Notice `header('Content-Type: application/json');` at the beginning of the php file – Omar Tanti Mar 30 '18 at 02:53
  • 1
    You would call it in your php file that you are requesting in your ajax request ie, `if(isset(..)){ somefunction(); }` – Patrick Evans Mar 30 '18 at 02:53
  • Just call php function inside if condition in php – Niklesh Raut Mar 30 '18 at 02:56

1 Answers1

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);
    }
});
JoinCompany
  • 504
  • 2
  • 11
  • Thanks for the response. But i have some code fragment to be executed after calling the same file. I cannot excutet that. – kshan 2k18 Mar 30 '18 at 02:48
  • @kshan, you didn't mention about that code fragment ? Share detail , although this is the answer of your question. – Niklesh Raut Mar 30 '18 at 02:54
  • 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... – kshan 2k18 Mar 30 '18 at 02:58