Solved: javascript problem in php

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

twmprys

Thread Starter
Joined
Jun 4, 2009
Messages
111
I wonder if anybody can help: I'm running a php WHILE statement to run out a table of users, and have a button next to each user to enable the details to be edited - using javascript the button brings up a new table row immediately below (with editable textareas - I haven't put these in the script below to keep it simple). My problem at the moment is with the javascript: doesn't matter which button you press on, it always brings up a new line under the last entry in the list. The javascript id's match up, so what's wrong?

PHP:
<?php
  mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
// Get data from table
$query = "SELECT * FROM members ORDER BY surname";
$result = mysql_query($query) or die(mysql_error()); 
?>
HTML:
<table width="300" border="1">
PHP:
<?php while($row=mysql_fetch_array($result)) {
echo '<tr><td>' . $row['firstname'] . '</td><td>' . $row['surname'] . '</td><td>' . $row['username'] . '</td><td><a href="#" onclick="displayRow()">edit</a> '. $row['id'] . '</td></tr>';
echo '<tr id="';
echo $row['id'];
echo '" style="display:none"><td>' . $row['firstname'] . '</td><td>' . $row['surname'] . '</td><td>' . $row['username'] . '</td><td></td></tr>';
echo '<script type="text/javascript">
function displayRow(){
var row = document.getElementById("';
echo $row['id'];
echo '");
if (row.style.display == \'\') row.style.display = \'none\';
else row.style.display = \'\';
 }
</script>';
}
?>
HTML:
</table>
 
Joined
Nov 30, 2009
Messages
277
Personally, I would have done this with Divs as they behave better than table rows. Anyway, you're javascript function is being overwritten on each loop. I've made a few adjustments to your supplied code:

Javascript at top of page:
Code:
<script type="text/javascript">
function displayRow(chosenRow){
    var row = document.getElementById(chosenRow);
    if (row.style.display == 'block') row.style.display = 'none';
    else row.style.display = 'block';
 }
</script>
Your PHP connection to mySQL:
Code:
<?php
  mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
// Get data from table
$query = "SELECT * FROM members ORDER BY surname";
$result = mysql_query($query) or die(mysql_error()); 
?>
Your PHP connection to mySQL:
Code:
<?php
  mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
// Get data from table
$query = "SELECT * FROM members ORDER BY surname";
$result = mysql_query($query) or die(mysql_error()); 
?>
Next bit:
Code:
<?php while($row=mysql_fetch_array($result)) {
echo '<tr><td>' . $row['firstname'] . '</td><td>' . $row['surname'] . '</td><td>' . $row['username'] . '</td><td><a href="javascript:;" onclick="displayRow(\''. $row['id'] . '\')">edit</a> '. $row['id'] . '</td></tr>';
echo '<tr id="' . $row['id'] . '" style="display:none"><td>' . $row['firstname'] . '</td><td>' . $row['surname'] . '</td><td>' . $row['username'] . '</td><td></td></tr>';
}
?>
 

twmprys

Thread Starter
Joined
Jun 4, 2009
Messages
111
Thank you, that works a treat. I know divs can be more elegant, but I was happy with the simple, clunky table row look for this.
 

twmprys

Thread Starter
Joined
Jun 4, 2009
Messages
111
apart from the fact that the javascript seems to be stuffing the new table row into the first cell of the next line instead of running it aqs a row in itself. I've stripped out the php and it works OK. Is it something to do with the way the while statement works?
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top