Hi!

I have a JS ajax function that goes into PHP file and returns (by echo) javascript code (a string data). All that is echo-d is in "return_data" variable. The problem is that returned JS code wont run.

For example if i try to return simple JS function alert from PHP file i get nothing, if it is wrapped in <script> - tags. If its not wrapped in script tags, its just printed out.

I tried eval() function. Same effect.


It is line from JS ajax function that prints data to ID-ed div:

document.getElementById("ID").innerHTML = return_data;

The thing im trying to do is to have a have a function that runs for example onClick and fills in with data some PHP pre-written ID-ed div-s. I want to achieve dynamic effects without using refresh.


Thank you in advance!

Member Avatar for diafol

Could you show all the relevant code. return_data means nothing in this context.

BTW - avoid eval() wherever possible, unless you totally lock down any input.

commented: Helped me twice! +1

Sure!

The return data is sum of all echos:

<?php

		$connect = mysql_connect("xxxx","xxxx","xxxxx") ;
		mysql_select_db("xxxx", $connect) or die("Ei leidnud andmebaasi at member.php code."); 
		
		$query = mysql_query("
		SELECT * FROM xxxxxxxx order by skoor desc limit 10 ;"  // ainult 10 rida ja skoori j2rgi
		);
		
		$skoor = array();
		
	    $b=0;
		while($row = mysql_fetch_array($query)) {
		
			$skoor[$b]['url_name'] = $row['url_name'];
			$skoor[$b]['url']      = $row['url'];
			$skoor[$b]['added']    = $row['added'];
			$skoor[$b]['adder']    = $row['adder'];
			$skoor[$b]['tags']     = $row['tags'];
			$skoor[$b]['skoor']    = $row['skoor'];
	
			$b++;
		}
		//echo "JS starting:";
		echo "<script language='JavaScript' type='text/javascript'> ";
		//echo "alert('asd')";
		for($b=0; $b<10; $b++){

			$url_name = $skoor[$b]['url_name'];
			$url      = $skoor[$b]['url'];
			$added    = $skoor[$b]['added'];
			$adder    = $skoor[$b]['adder'];
			$tags     = $skoor[$b]['tags'];
			$skoor_t  = $skoor[$b]['skoor'];

			echo "ajax_fill(
			$b,
			'$url_name',
			'$url',
			'$added',
			'$adder',
			'$tags',
			'$skoor_t'
			);"; 
		}

		echo "</script>";

?>

Without the JS script tags data looks like:

ajax_fill( 0, 'Lambda', 'www.lamda.ee', '2011-12-15 16:35:35', 'vik', 'Lambda kursus', '56' );ajax_fill( 1, '9gag', 'www.9gag.com', '2011-12-15 14:24:37', 'vik', '9gag', '36' );  ...

Thank you in advance.

Member Avatar for diafol

OK, it seems that you can produce 10 ajax_fill() statements via php, but I don't know why you wish to fill a tag element with a <script> block. As a rule, you'd place a script block in the head of a page or at the very bottom (to avoid the onload problem). For ajax, the usual way to do this would be to include all the js in the actual js function and not to use php to create it. I suggest that you use php to return the data via json (using json_encode). You then use js to loop over the return value and run the ajax_fill(...) from there.

For example (using jQuery):

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script>
    function doFills(){
       /*place var myURL - your php file*/
       $.ajax({
       type: "GET",
       url: myURL,
       beforeSend: function(x) {
       if(x && x.overrideMimeType) {
         x.overrideMimeType("application/j-son;charset=UTF-8");
       }
     },
     dataType: "json",
     success: function(data){
       /* parse your json data as data.var1, etc */
       /* place your ajax_fills(...json data...) in the form of a loop */
     }
     });
    }
  </script>

</head>

<sometag onclick="doFills();return false;">...</sometag>

The php file spits out array from the DB as a mutidimensional and then you encode it:

echo json_encode($mymutliarray);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.