Jump to content

Running an onclick() command from a PHP array?


woolyg

Recommended Posts

Hi,

 

I hope I explain this one clearly.

 

I am running a PHP script that outputs the lines of a mySQL table to an HTML table. Kinda like:

 

<table>
while($matches = mysql_fetch_assoc($sqlrun)){

$match_id = $matches['match_id'];
$team1_id = $matches['team1_id'];
$team2_id = $matches['team2_id'];

echo "<tr><td><input type="text" id="match_id" value="$match_id"><td><td>$team1_id</td><td>$team2_id</td></tr>\n";
}
</table>

 

On each line, I'd like to add the following:

 

 

<td onclick='getScorers()'><u>Add Scorers</u></td>

 

 

 

Now, 'getScorers()' basically uses AJAX to open information in another DIV, using $match_id as its getElementById. As follows:

 



function getScorers(){
	var match_id = document.getElementById('match_id');
	getData("../../codebin/soccer_get_scorers.php?match_id=" + match_id.value, 'scorersDiv')

}

 

 

OK - my problem here is, that when there are multiple lines within the main output table, there will be links to add scorers on the right hand side of each line, calling the JS. Each time the JS is called, though, it always calls the same Element ('match_id'), as you can see from my coding above.

 

Is there a way to populate my output table and getScorers(), so that each line can call getScorers(), using a unique getElementById string?

 

My current way only calls one element ('match_id'), but in reality there could be tens or hundreds of lines, each wanting to get info to that match....

 

 

 

All help appreciated,

Woolyg.

 

<table>
while($matches = mysql_fetch_assoc($sqlrun)){

$match_id = $matches['match_id'];
$team1_id = $matches['team1_id'];
$team2_id = $matches['team2_id'];

echo "<tr><td><input type="text" id="match_id" value="$match_id"><td><td>$team1_id</td><td>$team2_id</td><td onclick='getScorers($match_id)'><u>Add Scorers</u></td></tr>\n";
}
</table>

 



function getScorers(match_id){

	getData("../../codebin/soccer_get_scorers.php?match_id=" + match_id, 'scorersDiv')

}

 

that should work.  Now each row of the table is passing a unique value into the getScorers array. 

mbeals,

 

Thanks - that worked perfectly. You rock! Now I have an add-on Q:

 

In JS, is there a way to pass a variable into another variable?

 

 

For example, I'd like to pass variable match_id1 into variable team1_score, below, taking inputs from form items:

 

$match_id = $matches['match_id'];

<input type='text' id='$match_id.team1_score' name='team1_score' size='2' maxlength='2' />

 

 

	var match_id1 = document.getElementById('match_id')
	var team1_score = document.getElementById([b]'match_id1[/b].team1_score');

 

Is it possible to do this?

The reason I'm trying to do it is to continue passing unique information in through the javascript, so that whenever anyone clicks on the onclick() text, the site displays the result info for each line of the populated table.

 

Any ideas? Thanks for your earlier help.

Woolyg.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.