click row to fill textboxes in php - array vs ajax - php

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>

Related

Php show users data from mysql with dropdown list

i want to display users data in a table with the dropdown list , the first option shows all the users but when i click a name it still shows all users, i think i have some trouble in my ajax script because it deosn't seem to work
this is the page where i display data :
<?php
//load_data_select.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
function fill_emp($connect)
{
$output = '';
$sql = "SELECT * FROM employés";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$output = $output."'<option value="'.$row["IdEmp"].'">'.$row["NomEmp"].'</option>'";
}
return $output;
}
function fill_Abs($connect)
{
$output = '';
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
$result = mysqli_query($connect, $sql);
echo "<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
$output="</table>";
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Multiple Image Upload</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
/>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
</script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
<script>
$(document).ready(function(){
$('#nom').change(function(){
var NomEmp = $(this).val();
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
});
});
</script>
</head>
<body>
<br /><br />
<div class="container">
<h3>
<label for="nom">Selectionnez le nom de l'employé </label>
</br></br>
<select name="nom" id="nom">
<option value="">Tout les employés</option>
<?php echo fill_emp($connect); ?>
</select>
<br /><br /> <br />
<div class="row" id="show_Abs">
<?php echo fill_Abs($connect);?>
</div>
</h3>
</div>
</body>
</html>
and this is the usr.php :
<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$output = '';
if(isset($_POST["NomEmp"]))
{
if($_POST["NomEmp"] != '')
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW()) WHERE NomEmp = '".$_POST["NomEmp"]."'";
}
else
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
}
$result = mysqli_query($connect, $sql);
$output="<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
echo $output;
}
?>
can anyone help me to find the problem in this ? any help would be appreciated.
Thanks.
We could use a little more info to help with this, but I'll try to get the ball rolling.
I would guess that $_POST["NomEmp"] in usr.php is === "" so the else is running, which shows the entire table again.
I can't tell what you are setting the "value" attribute to in the fill_emp function. What is the value of $row[1] in that function?
Just change the ajax call like mentioned below :
$(document).ready(function(){
NomEmp = "";
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
$('#nom').change(function(){
NomEmp = $(this).val();
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
});
});
Also in your html cannot able to find a tag having id show_Abs. So just modify the div into
<div class="row" id="show_Abs">
</div>
Now I think the php function <?php echo fill_Abs($connect);?> is not necessary.
When page loads the first time, ajax call will be generated and on changing the select tag call will be generated with passing the selected parameters getting appropriate results.

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.

checkbox in table in 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>

Deleting Checked box data in Mysql Table from form

I am working on an inventory program and I need help on the following:
I have a table that works through selecting data from my Database and showing this in a table.
I now need to make it possible for a user to be able to delete bad/faulty records from the form ( Ofcourse u don't want to everytime have to go in the database itself to remove a record, would be stupid)
My code:
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Inventory Grid.html</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost", "root", "admin", "inventarisdb");
// Check connection
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query( $con, "SELECT * FROM BCD" );
echo "<table border='0'>
<tr>
<th>Categorie</th>
<th>SerieNummer</th>
<th>MacAdress</th>
<th>ProductCode</th>
<th>Prijs</th>
<th>RekNummer</th>
<th>PaletNummer</th>
<th>Hoeveelheid</th>
<th>Aantekeningen/th>
</tr>";
while( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row['Categorie'] . "</td>";
echo "<td>" . $row['SerieNummer'] . "</td>";
echo "<td>" . $row['MacAdress'] . "</td>";
echo "<td>" . $row['ProductCode'] . "</td>";
echo "<td>" . $row['Prijs'] . "</td>";
echo "<td>" . $row['RekNummer'] . "</td>";
echo "<td>" . $row['PaletNummer'] . "</td>";
echo "<td>" . $row['Hoeveelheid'] . "</td>";
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</article>
</fieldset>
</tbody>
This code works perfectly!
What i want to achieve is to select Records from this table with a checkbos and delete them by clicking on a delete button in the bottom of the page programmed like this:
<Input type="submit" id="submit" name="submit" value="Verwijderen" />
Or at least with this principle!
(This is the second ever application I wrote, 1st was a webpage, and I have no idea how to research this. I keep finding questions and posts that are just not what i need :S Hope U can help me )
I suggest you to use jQuery ajax so you will be able to delete entry one by one without page reload.
At first include jQuery library in HTML HEADER and the Javascript script file
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>
script.js:
$(document).ready(function() {
$(".delete").click(function(event) {
var entryid = $(this).closest('tr').find('.entryid').val();
alert('This entry will be deleted: ' + entryid); // this will alert you the value what you get. After finisgin to get correct value you can delete this
$.ajax({
type: "GET",
url: "delete.php",
data: "id=" + entryid
});
$(this).closest('tr').remove();
});
});
in MySQL database must be uniqe ID for every entry named 'id'
Grid.html:
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo '<td><input type="hidden" class="entryid" name="id" value='.$row['id'].' />delete</td>';
delete.php:
// connect to database
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$id'";
First of all you need to wrap the table into a form
<div id="artikel-container">
<form name="form1" method="post" action="...">
<table class="table 1">
...
echo "</table></form>";
mysqli_close($con);
Next you need the checkboxs on the form $row['ID'] is the primary key of the table
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "<td><input type='checkbox' name='del[]' value='".$row['ID'] . "></td>";
echo "</tr>";
In the php file called in the form's action attribute:
<?php
if( isset( $_POST['del'] ) {
$del = $_POST['del'];
for(x=0; x<count(del); x++ ) {
$sql = 'DELETE FROM BCD WHERE ID='.$del[$x];
//---execuste the query
}
}
?>

Categories