Forum Moderators: open

Message Too Old, No Replies

Evaluating dynamic javascript ajax response

         

dungareez

5:04 am on Aug 7, 2007 (gmt 0)

10+ Year Member



Hi all,

I am having a difficult time to get the inline text effect from scriptalicious to work when the script is returned in an ajax response. I want to add that I am not a programmer by trade or education, but like to play around, so please forgive my hideous code.

Here is what is going on server side in the php file (pertinent info):

$returnValue .="</><br /><form id=\"poForm\" name =\"poForm\"><p id =\"invoice\" name =\"invoice\"><b>Invoice#(for multiple invoices separate with commas) ".$invoiceNums."</p>";
$returnValue .="<script type=\"text/javascript\">

inline('invoice', 'inline_editor.php');
</script></form>";

The $returnValue variable is echo'd out and the document goes through some parsing for proper div/s to update(there is obviously more to it then what I have shown)

I have this function on the main page to make sure the scriptalicious function loads ( Ithought that may have been my problem). It works with a test inline that is loaded immediately (not via ajax)

function inline(elementID, url, options){
new Ajax.InPlaceEditor(elementID, url);
}

ultimately what I get is the following script in the page:
<script type="text/javascript">
1
2 inline('invoice', 'inline_editor.php');
3
</script>

But no inline editing, although in the div below it the original still works just fine.

Looking through the forum I think somehow eval() may help solve this issue, but I can't figure out how to get it to work.

Any help would be very greatly appreciated.

mehh

9:01 pm on Aug 8, 2007 (gmt 0)

10+ Year Member



there is an error in your php code. try this:

$returnValue .="</><br /><form id=\"poForm\" name =\"poForm\"><p id =\"invoice\" name =\"invoice\"><b>Invoice#(for multiple invoices separate with commas) ".$invoiceNums."</p>";
$returnValue .="<script type=\"text/javascript\">".
inline('invoice', 'inline_editor.php')."
</script></form>";

dungareez

2:42 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Thanks for your suggestion but it looks like you just added concatenation in for the inline function which is not a php function, but a javascript function that is echoed out, so it needs to be within the quotes.

$returnValue .="<script type=\"text/javascript\">

inline('invoice', 'inline_editor.php');
</script></form>";

could be rewritten to be

$returnValue .="<script type=\"text/javascript\">inline('invoice', 'inline_editor.php');</script></form>";

so you can see it better, the white space shouldn't count in php (I had it written that way because I was changing it so much it made it easier for me to know what I had changed). The interior semicolon is for the end of the javascript statement.

Again thanks for the try. Everything I read seems to lead me back to haw a function needs to be presented to prototype to be evaluated (I also tried eval()). Can eval() be used on a string that is intermixed with code or does the string have to be entirely code? Anyhow thanks again for the attempt.

mehh

12:14 pm on Aug 9, 2007 (gmt 0)

10+ Year Member



Sorry. I miss read the question. Try seting the forms innerHTML to itself at the end of your callback function. eg:

function AJAXCallBack(){
//ajax code etc.
var form=document.getElementById('poForm');
form.innerHTML=form.innerHTML;
}

eval will only work if the returned stuff is all JS. I supose you could try picking out the code with RegEx but the above works usualy.

dungareez

3:43 am on Aug 10, 2007 (gmt 0)

10+ Year Member



Thanks again mehh. It did not work for me but I do appreciate the input. Part of my problem is that I am using protoype, and in hindsight I think I would not until I am a good deal more experienced in JS. It is too complex for me to go through their code and it adds too much confusion for me, not to mention size to my code base for what I actually used it for.

For now I am going to skip the scriptalicious effect and use a simpler solution. I may try your regex suggestion one day when I am bored though. Thanks again.