checkbox in table in php - php

I want to have a table that contains words and their meaning from database, in another column i want to have checkbox for each row, that user will check them and show what words he/she know.
i have two question in this case:
1- how can i hide the meaning in the first and after clicking in show meaning visible them?
2- how can i set checkboxes?
i have this code until now but it doesn't work
please help me if you can
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').item(0).style.visiblility = 'visible';
}
</script>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='visibility:hidden'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>

getElementByClassName is an inexistant function. You mean getElementsByClassName, which will however return a list of the elements, so you need to select one.
document.getElementsByClassName('hiding').item(0).style.visibility = 'visible';

For hide a suggestion:
echo "class='hiding' style='display:none'>" . $row['meaninig'];
To show the meaning:
//for Jquery
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
Or
//for plain old javascript
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').style.visibility = 'visible';
}
</script>
Your code Edited:
<html><head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
</body>

Related

How to retrieve row from a html table on mouse click in PHP?

I am displaying some records in an HTML table using PHP. I am trying to make it so that when I click on a row, I can store the itemID (that has been clicked on) in a variable. For some reason, when I click on the row I want, the alert in JavaScript just doesn't work nor does it happen. Am I missing something trivial?
<?php
$con=mysqli_connect("localhost","root","mypassword","myDB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<font color = 'darkgreen'> Connected to database. </font> <br>";
$result = mysqli_query($con,"SELECT * FROM Inventory");
echo "<table border='1'>
<tr>
<th>Item ID</th>
<th>Item Name</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['itemID'] . "</td>";
echo "<td>" . $row['itemName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<script type="text/javascript">
$("tr.table").click(function() {
var tableData = $(this).children("td").map(function() {
return $(this).text();
}).get();
alert("Your data is: " + $.trim(tableData[0]) + " , " + $.trim(tableData[1]));
});
</script>
<?php
$con=mysqli_connect("localhost","root","mypassword","myDB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<h3 style='color: darkgreen;'> Connected to database. </h3> <br />";
$result = mysqli_query($con,"SELECT * FROM Inventory");
echo "<table border='1'>
<tr>
<th>Item ID</th>
<th>Item Name</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='item-id'>" . $row['itemID'] . "</td>";
echo "<td class='item-name'>" . $row['itemName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script type="text/javascript">
$("tr").click(function() {
var id = $(this).find('.item-id').text();
var name = $(this).find('.item-name').text();
alert("Your data is: " + $.trim(id) + " , " + $.trim(name));
});
</script>
echo "<tr onclick='showAlert($row['itemID'],$row['itemName']);'>";
please write in your table and create java script function to show alert
<script>
function showAlert(TxtItemid,txtItemName){
alert("Your data is: " + TxtItemid + " , " + txtItemName));
}
</script>
this is one of the way to alert.Please have try on this. This will work for you. It is working in my case.

New fetched row overwriting the previous row while writing to a table

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.css">
<script>
function submit() {
document.getElementById("myform").submit();
}
window.onload = function() {
document.getElementById("textbox").focus();
};
</script>
</head>
<body>
<form id="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1> AWB_NO</h1>
<input type=text id="textbox" name="excel" onchange="submit()">
</br>
</form>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$vars = $_COOKIE['var1'];
if(!isset($_SESSION['items'])) {
$_SESSION['items'] = array();
}
if(!isset($_SESSION['arr2'])) {
$_SESSION['arr2'] = array();
}
$var='';
if(isset($_POST['excel']))
{
$host="localhost";
$user="root";
$password="";
$db="greenmobiles";
$table="manifest";
$var=$_POST['excel'];
$con=mysqli_connect("$host","$user","$password") or die("Cannot Connect");
mysqli_select_db($con,"$db") or die("Cannot select DB!");
if (in_array($var,$_SESSION['items']))
{
echo"The new value has been scanned!</br>";
echo "<hr>";
echo "The tracking id's of the currently scanned items are given below<br><hr>";
foreach ($_SESSION['items'] as $x => $value)
{
echo "$value";
echo "<br>";
}
echo "<script>alert('oops! This item has already been scanned!')</script>";
}
else
{
$sql = "SELECT * FROM manifest WHERE awb_no = '$var'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0)
{
echo "tracking id present in the manifest</br>";
}
else
{
echo "<script>alert('Tracking id is not present in the manifest!')</script>";
}
$sqli = "SELECT * FROM manifest WHERE (awb_no = '$var') AND (document_no='$vars')";
$result1 = mysqli_query($con,$sqli);
if (mysqli_num_rows($result1))
{
echo "The tracking id matches with the document no.";
}
else
{
echo "<script>alert('Tracking id does not belong to the document number entered.')</script>";
exit(0);
}
while ($row = mysqli_fetch_assoc($result))
{
$_SESSION['data'][] = $row['awb_no'];
$_SESSION['items'] = $_SESSION['data'];
}
mysqli_free_result($result);
echo"The new value has been scanned!</br>";
echo "<hr>";
echo "The tracking id's of the currently scanned items are given below<br><hr>";
echo '<div class="col-lg-10 col-lg-offset-1 panel">'."<table class='table'>
<tr>
<th>Document No</th>
<th>AWB NO</th>
<th>Order Id</th>
<th>Forms</th>
<th>Extras</th>
</tr>";
echo "<tr>";
$sqlq= "SELECT * FROM manifest WHERE awb_no = '$var'";
$result2 = mysqli_query($con,$sqlq);
if ($result2)
{
while ($row=mysqli_fetch_row($result2))
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
echo "</table></div>";
}
}
}
else
{
session_destroy();
}
?>
</body>
</html>
Hello, i am new to php and html. I am giving an input to a text box whose value is '$var' and retrieving data one row at a time and trying to print it in a html table via php. But each time i enter a value the previous row gets overwritten by the new row and i am not able to keep the previous rows and append the new ones to my table. Can someone please tell me where i am going wrong.

Delete full row from a form into a database in php

I want to delete full row from a form into a database in php when i click on delete button. But there are "Undefined index on line 39" issues come on my page and when i click on delete button it redirect me on different page and didn't delete a row.
how can i delete a row on one click ??
Please help me.
Thanks,
Nabeel
<body>
<a href="" >delete</a>
<a href="" >create</a>
<label>Read</label>
<?php
$con=mysqli_connect("localhost","root","","firstphp");
if($con)
{
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users`");
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>passward</th>
<th>name</th>
<th>delete</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['passward'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<label>End Read</label>
<br /><br /><hr />
<label>Delete</label>
<?php
if(isset($_POST['delete']))
{
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
$id = $_POST["id"];
$sql = "DELETE FROM users WHERE id = $id";
mysql_select_db('firstphp');
$result = mysql_query($sql,$con);
if(!$result)
{
die("Could not delete data: " . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
}
else
{}
?></body>
change
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
in
echo '<th><a href="'.$_SERVER['PHP_SELF'].'?id='. $row['id'] .'&delete=true" >delete</a></td>';
and change
if(isset($_POST['delete']))
//...
$id = $_POST["id"];
in
if(isset($_GET['delete']))
//...
$id = $_GET["id"];

click row to fill textboxes in php - array vs ajax

I have a table coded in php which pulls in data from mysql database and displays it in a html table . Now when i click on a row I need certain textboxes to be filled.
What is the best approach for this: is it using an array or ajax,html and php,
<?php
$default = "<img src='http://localhost/on.png' width='24' height='24'/>";
$default1 = "<img src='http://localhost/of.png' width='24' height='24'/>";
$con = mysql_connect("*****","******","******");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db ("*****",$con);
$sql= "select act.*
from audit_activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_id and act.timestamp=a.max_ts";
$mydata = mysql_query($sql,$con);
echo "<table id='tfhover',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
mysql_close($con);
?>
<html>
<script type="text/javascript">
window.onload=function(){
var tfrow = document.getElementById('tfhover').rows.length;
var tbRow=[];
for (var i=1;i<tfrow;i++) {
tbRow[i]=document.getElementById('tfhover').rows[i];
tbRow[i].onmouseover = function(){
this.style.backgroundColor = '#ffffff';
};
tbRow[i].onmouseout = function() {
this.style.backgroundColor = '#d4e3e5';
};
}
};
</script>
<head>
</head>
<body>
Total exams taken : <br>
<input type="text" name="fname"/>
</body>
</html>

how to bring ajax sorted result into html divs

HI i have done some AJAX, PHP&MySQL Sorting and it is giving me result in tables as shown in the code below, my question is how to bring that $result in html divs.
please help
PHP code used
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>title</th>
<th>image</th>
<th>description</th>
<th>rating</th>
<th>download</th>
<th>buy</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['rating'] . "</td>";
echo "<td>" . $row['download'] . "</td>";
echo "<td>" . $row['buy'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I want Result In These HTML Div's
<div class="category-container">
<div class="category-image"></div>
<div class="category-link">#</div>
<div class="category-desc"><p>#</p> </div>
<div class="rating5" >Editors' rating: </div>
<div class="category-download-btn">Download </div><
<div class="category-buy-btn">Buy</div>
</div>
I don't know why you are creating table when returning the ajax response. I advice you to create json response as a result of ajax. Using this result JSON you can either create table or you can render them in your html.
in your php code where ajax request is sent: ajax.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
$response = array();
$i=0;
while($row = mysql_fetch_array($result))
{
$response[$i]['id'] =$row['id'];
$response[$i]['title'] = $row['title'];
$response[$i]['image'] = $row['image'];
$response[$i]['description'] = $row['description'];
$response[$i]['rating'] = $row['rating'];
$response[$i]['download'] = $row['download'];
$response[$i]['buy'] = $row['buy'];
$i++;
}
mysql_close($con);
echo json_encode($response);
?>
In your html file where you are getting this ajax response, I am giving you the hint how can u use this ajax response:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.ajax({
url: 'ajax.php',
dataType: 'json',
success: function(response){
data = '';
$.each(response,function(i,val){
data = '<div class="category-image">'+val.image+'</div>'+
'<div class="category-link">'+val.id+'</div>'+
'<div class="category-desc"><p>'+val.description+'</p> </div>'+
'<div class="rating5" >'+val.rating+'</div>'+
'<div class="category-download-btn">Download </div>'+
'<div class="category-buy-btn">Buy</div>';
$('<div>').attr('id',i).html(data).appendTo('#response');
});
});
}
});
</script>
</head>
<body>
<div id='response'></div>
</body>
</html>
I'm inclined to view this as a bit of a joke, seeing as the database is called "security_software", and you're putting a GET var directly into a database query without doing any sanitation. You're also making no attempt to clean anything coming from the database before spitting it back onto the page...
Anyway, assuming it's not a joke, the following should point you in the right direction:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<div class="category-image"><img src="' $row['image'] . '</div>';
echo '<div class="category-link">' . $row['title'] . '</div>';
echo '<div class="category-desc"><p>' . $row['description'] . '</p></div>';
echo '<div class="rating5" >Editors' rating: ' . $row['rating'] . '</div>';
echo '<div class="category-download-btn">Download</div>';
echo '<div class="category-buy-btn">Buy</div>';
}
echo "</table>";
mysql_close($con);
?>
if you wan to pull the result in these divs then use same divs instead of table/tr/tds or you can bind it by receiving json/xml or any kind of object based data

Categories