vaka
vaka

Reputation: 61

Php call itself many times through ajax call

I have a php file which makes calls to itself with ajax through a click event. Due to these calls the jquery code (all the code that i have inside the script tags) is repeated, as a consequence the jquery code to be executed many times. I want to execute the script only the first time that i call the php file.

Please help me with this. I can also post part of my code.

Thanks in advance!

Sorry for my omission. I use jquery tabs which are created like this

<div class='tabs'>
<ul>
 <li><a href="functions.php?id=1&table=1&tab=First">First</a></li>
 <li><a href="functions.php?id=2&table=2&tab=Second">Second</a></li>
 <li><a href="functions.php?id=3&table=1&tab=Third">Third</a></li>
</ul>
<ul>
  <li><a href="functions.php?id=11&table=1&tab=First">First</a></li>
  <li><a href="functions.php?id=12&table=2&tab=Second">Second</a></li>
  <li><a href="functions.php?id=13&table=1&tab=Third">Third</a></li>
 </ul>
 </div>

The php file functions.php has jquery ajax call

$( ".decision_button > input" ).click(function() { 
var responseSubmit = $.ajax({
url: 'functions.php',
type: 'POST',
data:'comments='+comment_select,
async: false
}).responseText;});

So every time click on href the file is called and the jquery code is repeated. I hope this was clear.

Thank you

Upvotes: 0

Views: 845

Answers (2)

TheWolf
TheWolf

Reputation: 1385

You could add a GET parameter to your Ajax call:

if (!isset($_GET['repeat']) || $_GET['repeat'] != 'false') {
   echo '$.ajax("test.php?repeat=false", ...)';
}

(assuming you use jQuery). Of course, you could use a POST parameter as well if you like. This way, php will print the jQuery Ajax call only if it hasn't been called via Ajax.

Upvotes: 0

Jaroslav
Jaroslav

Reputation: 1389

When calling the page from JavaScript (AJAX) add some parameters to you url, so PHP script can distinguish is it called by user or by "itself". For example if your page is your-domain.com/index.php from JavaScript you open it as your-domain.com/index.php?ajax=1.

Then in PHP find all places where you echo JavaScript code and put it in if statement:

if(!isset($_GET['ajax'])){
  echo "JAVASCRIPT code goes here";
}

Upvotes: 1

Related Questions