-3
$.ajax({ url: '/my/site',
     data: {action: 'test'},
     type: 'post',
     **success: function(output) {
                  alert(output);**
              }
});

When I use the above script, It shows me innerHTML(of example.php) only. I want the above code to run my PHP function which will redirect to another php url (example.php). Should I remove the success function? Or is there any possible solution?

L7117
  • 3
  • 2
  • 2
    are u trying to have the ajax response redirect the browser? that won't work. e,g http://stackoverflow.com/questions/2927044/redirect-on-ajax-jquery-call – Joel Blum Jan 11 '17 at 20:11
  • @Joel_Blum if I am trying to deal with database query? I believe it will work. Just hoping my assumption is correct. – L7117 Jan 11 '17 at 20:16
  • 2
    The ajax call will not redirect. You need to have the php function respond and based on that response have a js location in your success function – bos570 Jan 11 '17 at 20:38
  • You mix different things together. Ajax does not "run a script" or "call a PHP function" or "redirect" - it just does send a request to a webserver, just like you do when typing an URL into your browser. – Honk der Hase Jan 11 '17 at 22:52

2 Answers2

0

That is basically just what Ajax does, to stay simple, the ajax call gets the content of the file. So if your php file is in a server that is able to run php, the code will be executed. But only the content that you "display" on the page will be retreived by the ajax call, example:

file1.php -> nothing is diplayed, code will run and variables will take the values, but ajax call will return an empty string:

<?php $x=1; $y=$x+1; /* -> y: 2 */ ?>

file2.php -> an echo is made, code will run and variables will take the values, the ajax call will return a string with only what was diplayed: 2

<?php $x=1; $y=$x+1; echo $y; ?>

file3.php -> all text outside of php will be returned by the ajax call too: y = 2

y = <?php $x=1; $y=$x+1; echo $y; ?>

Usually you will use data formatting if you want to pass data, like JSON.

It means that Ajax is not made to redirect the page, but execute an outside code or retreive data. At least you could retreive the new URL to redirect, then redirect in JS, if this is what you want, but not redirect just from the PHP code you call. It is a different page.

Kaddath
  • 5,933
  • 1
  • 9
  • 23
  • Thank you. But what I want is to run php function with javascript. That's it. If js is launched->triggers php function. No data necessarily be passed. – L7117 Jan 11 '17 at 20:51
  • I found this. function someFunction() { echo 'Answer'; } if ($_GET['do'] === "someFunction") { someFunction(); } I believe it will refresh the page, the worst thing for me. – L7117 Jan 11 '17 at 20:54
  • it depends on what you want to do, this is not clear. If you want to redirect a web page on user input, a simple link to the URL, or javascript (look for window.location) will do. If you want to refresh a part of a page, you still need JS to do it. You build the page with the page you call with Ajax, and retreive the result. then on return use JS to inject it in the page. – Kaddath Jan 11 '17 at 20:58
  • i should add: don't use Ajax calls to pass function code, this is unsure. First, the page you call will be "open" to show its results if you don't protect it. Second: anybody will be able to see what is being send or received by the Ajax calls. This can lead to security breaches easily. – Kaddath Jan 11 '17 at 21:03
  • Absolutely correct.. To be clear, What should I use to run php function from client's side? JSON? I may have to use the ugly method, triggering php function by reloading php file (including the function). – L7117 Jan 11 '17 at 21:08
  • you need to be more specific about what you try to achieve. The Ajax call will actually make the php file run, in my first example you could have made sensitive operations and stored stuff in database, as long as nothing is displayed, nothing will return throught the response. You usually display only the final result if there is some. It could be only `true` or `false` for the page to know how the operations went, or the final html already built to inject in the page.. – Kaddath Jan 11 '17 at 21:18
0

You can echo your php function in a page by itself and use ajax to open that page on success.

Televimsa
  • 1
  • 2