Php - Ajax - eval()

  • Zaphe
  • Born
  • Born
  • Zaphe
  • Posts: 2

Post 3+ Months Ago

I've been using a lot of ajax on my website, so that the whole page doesn't have to load everytime someone clicks a link, or have loads of different pages, it updates with ajax [I'm using prototype].

Now for some links i just need to update a section of the site, so i was doing this:


Code: [ Select ]
function showContent(action, var1, var2, var3){
 
if(ajaxRequests <= 8){
 
ajaxRequests++;
 
new Ajax.Request('http://www.***.co.uk/scripts/mainContentScript.php',
  {
    method:'post',
    parameters: {action: action, var1: var1, var2: var2, var3: var3},
    onSuccess: function( i) {
            get('centreContents').innerHTML = i.responseText;
ajaxRequests--;
        },
    onFailure: function(){ popUp("Oops", "Sorry but an error has occured. Please wait a few moments and then try again. [Error Code: AxRq]");
ajaxRequests--; }
  });
}
else { popUp('Connection Problem', 'It seems you have too many open connections to the site, please wait a little while beore trying anything again, thank you.<br><br>Current Connections: ' + ajaxRequests); }
}
  1. function showContent(action, var1, var2, var3){
  2.  
  3. if(ajaxRequests <= 8){
  4.  
  5. ajaxRequests++;
  6.  
  7. new Ajax.Request('http://www.***.co.uk/scripts/mainContentScript.php',
  8.   {
  9.     method:'post',
  10.     parameters: {action: action, var1: var1, var2: var2, var3: var3},
  11.     onSuccess: function( i) {
  12.             get('centreContents').innerHTML = i.responseText;
  13. ajaxRequests--;
  14.         },
  15.     onFailure: function(){ popUp("Oops", "Sorry but an error has occured. Please wait a few moments and then try again. [Error Code: AxRq]");
  16. ajaxRequests--; }
  17.   });
  18. }
  19. else { popUp('Connection Problem', 'It seems you have too many open connections to the site, please wait a little while beore trying anything again, thank you.<br><br>Current Connections: ' + ajaxRequests); }
  20. }


But sometimes it needs to just run a php script and then do something else like run another javascript function, so in that case i was doing:


Code: [ Select ]
eval(i.responseText);

In place of the bolded part in the function above.

However i have realised that due to certain aspects of the site i need to have them all as:


Code: [ Select ]
eval(i.responseText);


So that i can run functions and/or redirect the page on certain errors/occasions, etc...

And i previously tried putting something like:

Code: [ Select ]
<script type="text/javascript">
myfunction();
alert('test');
</script>
  1. <script type="text/javascript">
  2. myfunction();
  3. alert('test');
  4. </script>


Into the file that was being included through a require_once() in the php script. when i was doing the InnerHTML method, but that didn't work.

So how can i do the same updating of sections of the site using eval() instead?

I have tried something like this [In the php script]:

Code: [ Select ]
...blah
 
if(...blah)
{
?>
get('centreContents').innerHTML = '<?php require_once('path/to/file.inc.php'); ?>';
<?php
}
 
..blah
  1. ...blah
  2.  
  3. if(...blah)
  4. {
  5. ?>
  6. get('centreContents').innerHTML = '<?php require_once('path/to/file.inc.php'); ?>';
  7. <?php
  8. }
  9.  
  10. ..blah

But that just breaks the function and never reaches the onSuccess stage.

Hopefully i explained that well enough.

Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

eval() would not know what to do with the <script> elements in that short section of code you posted.

First thing I'd try if I were you is whatever you had, but without the HTML elements.

Code: [ Select ]
myfunction(); alert('test');
  • Zaphe
  • Born
  • Born
  • Zaphe
  • Posts: 2

Post 3+ Months Ago

Yes i know about that, i've used eval() quite a bit, and i know you just have to echo/print out the things like alert('test'); without the <script>.

Obviously i didn't explain it as well as i was aiming for.

The problem i'm having now is updating a section of the site using eval() instead of document.getElementById('blah').innerHTML in the onSuccess section of the ajax function.

Previously it was easy because i just used the innerhtml bit and the response text was placed into the div id i told it to. But i need to use eval() now instead because i need to run other functions as well as updating the divs.

So i've tried putting:

Code: [ Select ]
get('centreContents').innerHTML = '<?php require_once('path/to/file.inc.php'); ?>';


In the php script, but like i said, that just breaks the function.
  • cjxxi
  • Expert
  • Expert
  • cjxxi
  • Posts: 566
  • Loc: Fort Worth, TX

Post 3+ Months Ago

You can do that with CJAX.

it would go like this..

This is on the fly...
$CJAX->update('ElementID','DATA');


the awesome part about using CJAX is that you can execute javascript function anyway you want. for example, when the page first loads or within an ajax call as you specified in your post.

also when you make an ajax call to execute some javascript..

You can just create any function with javascript..
and to execute it with AJAX you would just do like..

$CJAX->[function_name]();

I know you are calling it with PHP.. but it actually executes a javascript function with the function name in javascript .. with help of the magic function __call in php 5.

example....
http://cjax.net/examples/update_content.php

Note that each box that is being updated, is a separate command (its the same command but different events).

It goes into a loop to execute the JavaScript for each box, there are more than 80 commands built-in , and its easy to add custom commands. So all the stuff that you need to do probably is already built-in CJAX.

If you are curious look at the source code in the page and you will see that all JavaScript is execute from the server side, you won't see any JavaScript in the page, except for the core JavaScript of CJAX

if YOU need help to set it up, i can help you set it up all the way.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 95 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.