Jump to content

Execute JS received from AJAX request


ShadowMage

Recommended Posts

I decided to split this thread out from the original since it uses completely different code. It's the same issue just different code.Anyway, in the original thread I was given the suggestion by wirehopper to create a JavaScript array that would hold the arguments for a function that I needed to call. I modified my code to create that array but ran into a problem. I realized that the JavaScript is not being executed when it's received and placed into the container div. My code simply tacks the script to the end of the HTML string that is being generated by PHP which is then placed into the innerHTML of a div on my page. When I try to access the array created by the JavaScript, I get an error saying that the array is undefined.Is there a good way to make sure that the code I have in the script tags gets executed when the page receives it?Here's the code that generates the HTML and the java script:

//This snippet is within a loop....$required = "false";$rqdSpan = '';$class = '';if (strtolower($arrColumns[$colIndex]['Null']) != 'yes') { //Field is required (Can't be NULL)    $required = "true";    $rqdSpan = "<span class='rqdText'>Required</span>";    $class = " class='required'";}echo "<div><span id='".$name."'".$class.">".$name.$rqdSpan."</span><br />\n";$type = $arrColumns[$colIndex]['Type'];$regex = '/([a-z]+)(?:[(](\d{1,})(?:[,](\d{1,}))?[)])?/';preg_match($regex, $type, $matches);//Set each parameter, skipping $matches[0] since it is the full string match$type = $matches[1];$length = '';if (!empty($matches[2])) {    $length = $matches[2];}$decimals = '';if (!empty($matches[3])) {    $decimals = $matches[3];}echo "<input type='text' id='".$name."_input' name='".$name."' value='".$value."'     onchange=\"ValueChanged(this, ".$required.", '".$type.";".$length.";".$decimals."');\" />";echo "<input type='hidden' id='".$name."_orig' value='".$value."' /></div>\n";if (!empty($JSON_array)) {    $JSON_array.=", ";}$JSON_array.=("\"".$name."\": [".$required.", \"".$type.";".$length.";".$decimals."\"]");//End of loop.....if (!empty($JSON_array)) {    echo "\n<script type='text/javascript'>var arrInputs = {".$JSON_array."};\n</script>\n";}

Link to comment
Share on other sites

If you return nothing but Javascript code, without HTML tags in it, you can use eval() to execute it.
Problem is I need to have the HTML along with it. Is there any way short of using a regex to extract the script so I can execute it? Or is a regex the best way?
Link to comment
Share on other sites

You need to separate the two, however you want to do that. You could return a JSON object where one property is the HTML code and another is the Javascript code, or you could use a regular expression, or you could write some token between the HTML and Javascript and then split the text up based on the token, or you could send 2 requests to get each. There are several ways to separate the two, but the point is that you need to send only the Javascript to eval, and you can do whatever you want with the HTML.

Link to comment
Share on other sites

You need to separate the two, however you want to do that. You could return a JSON object where one property is the HTML code and another is the Javascript code, or you could use a regular expression, or you could write some token between the HTML and Javascript and then split the text up based on the token, or you could send 2 requests to get each. There are several ways to separate the two, but the point is that you need to send only the Javascript to eval, and you can do whatever you want with the HTML.
Ah yes, of course. JSON! I think I'll do it that way. Thanks guys!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...