Hello and welcome to our community! Is this your first visit?
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
    Sri Lanka
    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..

    <script type="text/javascript">
    function go_java()
    var x=document.getElementById(index).value;
    <h1>Database view</h1>
    <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">

    $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. #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Thanked 2,250 Times in 2,237 Posts
    When you have the current object referrer this in your code,
    you may change your javascript to
    function go_java(elm)
    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.

  3. Users who have thanked abduraooft for this post:

    nickjay (06-21-2012)

  4. #3
    New to the CF scene
    Join Date
    Jun 2012
    Sri Lanka
    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