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.

 

Link to comment
Share on other sites

<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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.