Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Sri Lanka
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to capture the checked primary key in the HTML table view of a database?

    My problem is that how to identify which row of the HTML table, a certain web server user have checked in order to perform edit or delete function. Table is printed to HTML using 'echo' inside a 'while()' loop, which loops till all the data in 'data1' table of the database is printed. I have used a field called 'sid' which is a var-char as my primary key and i'm trying to assign it's value to a check box which is in front of each row of the table. When i box is checked a java script execute and try to assign value of 'sid' to check box value so that later in my code i will be able to extract that value to run a MySQL query in order to perform the intended task. The problem is that though the js executes as a box being checked, an js error message appears saying that 'unable to get the value of property 'value'. object is undefined or null'. What happen to 'X'? I have tried several methods to correct and i have seen that when im doing this using only HTML and java script (no echo) check box value is properly assigned though that is without connecting to the db and without using php. May be i'm missing some concept here
    ???
    Please understand that i have only recently started self learing php, mysql, html & js its not yet even week. what im telling is im sure there are many mistakes in my code and ill be most grateful if you can show them to me as well. I would like to do this simply as possible since i may not be able to understand very complex code at this moment. So thanx in advance everybody..

    <head>
    <script type="text/javascript">
    function go_java()
    {
    var x=document.getElementById(index).value;
    alert(x);
    }
    </script>
    </head>
    <h1>Database view</h1>
    <body>
    <form method="post" action="handle_update.php">
    <center><strong>Edit row(s):</strong><input type="radio" name="edt" value="1">
    <strong>Delete row(s):</strong><input type="radio" name="edt" value="2">
    <input type="submit" value="go">
    </form>
    <footer>
    © nick.jay.r
    </footer>
    </body>
    </html>



    <?php
    $user = 'root';
    $pswrd = '';
    $dbase = 'siteinfo';

    mysql_connect('localhost', $user, $pswrd) or die('Connection failed'. mysql_error());
    mysql_select_db($dbase) or die('Connection to db failed'. mysql_error());
    $query="select * from data1";
    $q_result=mysql_query($query) or die('query failed'.mysql_error());
    echo "<table border=2>"."<tr><th><th>SITE ID</th><th>SITE NAME</th><th>EQUIPMENT TYPE</th><th>VLAN</th><th>MANAGEMENT IP</th><th>SUBNET</th></tr>";

    while($row = mysql_fetch_array($q_result)){

    $id = $row['sid'];
    $na = $row['sname'];
    $qe = $row['eqtype'];
    $vl = $row['vlan'];
    $ip = $row['mgip'];
    $su = $row['subnet'];

    //echo "$id<br>";
    echo "<tr><td><input type='checkbox' name='box' id='index' value='$id' onclick=go_java(this)></td><td>".$id."</td><td>".$na."</td><td>".$qe."</td><td>".$vl."</td><td>".$ip."</td><td>".$su."</td><br/>";//fetch arry 1row at a time
    }
    echo "</table>";
    mysql_close() or die('Connection close failed'. mysql_error());


    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    When you have the current object referrer this in your code,
    Code:
    onclick=go_java(this)
    you may change your javascript to
    Code:
    function go_java(elm)
    {
    alert(elm.id);
    }
    BUT, you can't have the same value of id applied to multiple elements, which makes the document invalid. So, you may need to append the auto-increment id to the value, like
    PHP Code:
    echo '<tr><td><input type="checkbox" name="box" id="index'.$row['sid'].'" value="'.$id.'" onclick="go_java(this)">'
    Last edited by abduraooft; 06-22-2012 at 06:49 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    nickjay (06-21-2012)

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Sri Lanka
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much for the valuable support..now back to some searching & thinking


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •