Hi!


I have problem that i fail to find solution for and im looking for some help from you as im hopelessly struggling for answers.

So i have a HTML table and some cells are filled with id-d div-s. The problem is only the first id-d cell is filled with data.

Im calling a JS function that is linked with ajax. Im calling this function in for loop, but function runs only once.


for loop:

echo "<script language='JavaScript' type='text/javascript'> ";
for($b=0; $b<10; $b++){
	echo "ajax_fill(".$b.",".$IDarr[$b].");n";
}
echo "</script>";

JS function:

function ajax_fill(nr,id){

	var hr = new XMLHttpRequest();
	var url = "table_score_filler.php";
	
	var post_vars = "nr_fill="+nr+"&id_fill="+id;
	var skoor_str1 = "skoor"+nr;
	
	hr.open("POST", url, true);
	hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	hr.onreadystatechange = function() {
	    if(hr.readyState == 4 && hr.status == 200) {
		    var return_data = hr.responseText;
			document.getElementById(skoor_str1).innerHTML = return_data;
	    }
    }
	
	hr.send(post_vars);
    document.getElementById("status_fill").innerHTML = "fill score task: processing...";

}

And table_score_filler.php content:

<?php

$id = $_POST['id_fill'];
$nr = $_POST['nr_fill'];

//echo $id." ".$nr;
//alert($nr);
$connect = mysql_connect("localhost","rstudent","student") ;
mysql_select_db("reddit", $connect) or die("Ei leidnud andmebaasi at table_processor.php code."); 

$query = mysql_query("
SELECT skoor FROM t104557_links WHERE id='$id';") or die("table score fill problem!");

//$result = mysql_query ($query);
$row = mysql_fetch_array($query);
$skoor = $row[0];


echo $nr." ".$skoor;  //."fill fail <br />"
?>

Thank you in advance!

Recommended Answers

All 3 Replies

Member Avatar for diafol

Hmmm...
You're saying that the ajax_fill(...) only runs once or is ajax_fill() overwriting the same part?

Have a look at the browser view source to see if php spits out ajax_fill(...) 10 times.

I'm no js pro, but I'm sure there's a better way to do this.
Can't you pass a php array as a parameter to the ajax_fill('$array') and then let js deal with it. Perhaps you'll need to encode (json?) the array first.

The problem what this code was: JS code could find a id-d div, becasue is was NOT added to html at the moment the JS function ran. It was at the end of php file thus it was written/ sent to browse as html after everything.


So the struggle was to understand or notice the timing of different processes.

Thank you for your relpy, the "Have a look at the browser view source to see if php spits out ajax_fill(...) 10 times." Helped me much!

The problem what this code was: JS code could find a id-d div, becasue is was NOT added to html at the moment the JS function ran. It was at the end of php file thus it was written/ sent to browse as html after everything.


So the struggle was to understand or notice the timing of different processes.

Thank you for your relpy, the "Have a look at the browser view source to see if php spits out ajax_fill(...) 10 times." Helped me much!

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.