Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-21-2012, 12:17 PM   PM User | #1
nickjay
New to the CF scene

 
Join Date: Jun 2012
Location: Sri Lanka
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nickjay is an unknown quantity at this point
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());


?>
nickjay is offline   Reply With Quote
Old 06-21-2012, 12:25 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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)">'
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 06-22-2012 at 05:49 AM..
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
nickjay (06-21-2012)
Old 06-21-2012, 05:38 PM   PM User | #3
nickjay
New to the CF scene

 
Join Date: Jun 2012
Location: Sri Lanka
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nickjay is an unknown quantity at this point
Thank you very much for the valuable support..now back to some searching & thinking
nickjay is offline   Reply With Quote
Reply

Bookmarks

Tags
database, event handling, java script, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 PM.


Advertisement
Log in to turn off these ads.