1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: javascript problem in php

Discussion in 'Web Design & Development' started by twmprys, Apr 26, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. twmprys

    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>
     
  2. IT-Support

    IT-Support

    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>';
    }
    ?> 
    
     
  3. twmprys

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

    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?
     
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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/919296

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice