How to use ID from a table and send it to php file for query [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So I've this admin-dashboard where I want to click on the options link which will take me to another page or show a modal of that particular person's details.
So basically, I want to get the ID of one particular person and send it to backend for query and display the details.
My doctor-details.php file
<?php
require('config.php');
$sql = "SELECT * FROM doctor";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while($rows = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['id'];?> </td>
<td><?php echo $rows['f_name'];?></td>
<td><?php echo $rows['l_name'];?></td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['contact_number'];?></td>
<td><?php echo $rows['gender'];?></td>
<td> Options</td>
</tr>
<?php
}
}
?>
my ajax code for doctor's details
//For Doctor
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$.ajax({
url: 'views/admin/doctor-details.php',
type: 'POST',
success: function(result){
$("#response-doctor").html(result);
}
});
});
});
//Hide table on login
$("#show-doctor-details").hide();
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$("#show-doctor-details").show();
$("#show-patient-details").hide();
});
});

window.addEventListener('load', function() {
var trs = document.getElementById('response-doctor').querySelectorAll('tr');
for (var i = 0; i < trs.length; i++) {
var tds = trs[i].querySelectorAll('td');
tds[tds.length-1].querySelector('a').setAttribute('href', '/user/'+tds[0].innerHTML);
}
});
My bad, this sloution more easier (inside doctor-details.php):
<td> Options</td>

You can make entire table row as link
For example:
<tr a="profile.php?id=<?php echo $rows['id'?];">
...
<tr>
This is how you can add link on every row which leads you to profile.php file where you can show personal information. When you are in profile.php you can access person's id with $_GET['id'] and fetch data from database using this id. If you are looking for more detailed answer don't hesitate to ask.

Related

reload my webiste one table only when database is updated || or reload table only 2 second to get new updated values from database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
when the database in phpmyadmin get updated then i want to update the table values live to get new values from database.
so when the values are updated in database i can get new values.
i have done the below code it is working but thing is that the full web page will be reload after 2 second and data will be updated but i want to load data only not full webpage so please give me solution for this.
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<?php
require './connect.php';
$sql = "SELECT * FROM labours";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<div id='div1'><table border='2' width='100%' height='20%' style='text-align:center;font-size:50px; id='table'>
<tr><th>ID</th><th>NAME</th><th>WORK</th><th>SALARY</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["work"]. "</td><td>". $row["salary"] . "</td></tr>";
}
echo "</table></div>";
} else {
echo "0 results";
}
$conn->close();
?>
<script>
setTimeout(function ()
{
window.location.reload(1);
}, 2000);
</script>
You have to seperate your code into two files
index.php:
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<div id="results">
</div>
<script>
setInterval(function ()
{
$.ajax({
type: "GET",
url: 'http://localhost/Stackoverflow/reload_data/get_data.php', // CHANGE THAT
success: function(res){
$('#results').html(res);
},
dataType:'html',
});
}, 2000);
</script>
get_data.php:
<?php
require './connect.php';
$sql = "SELECT * FROM labours";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<div id='div1'><table border='2' width='100%' height='20%' style='text-align:center;font-size:50px; id='table'>
<tr><th>ID</th><th>NAME</th><th>WORK</th><th>SALARY</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["work"]. "</td><td>". $row["salary"] . "</td></tr>";
}
echo "</table></div>";
} else {
echo "0 results";
}
$conn->close();
?>
I hope it helps.

Using ID from table to show details about user in php

I've a link in admin-dashboard page which on click shows "doctor-details". Now I want the admin must be able to click on the options link in the table and see full details of the doctor in the same page(I'll probably use modals for this).
So my question is how do I get the ID from the table and send it to another php file for database query?
my code for generating details about doctor(doctor-details.php)
<?php
require('config.php');
$sql = "SELECT * FROM doctor";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while($rows = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['id'];?> </td>
<td><?php echo $rows['f_name'];?></td>
<td><?php echo $rows['l_name'];?></td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['contact_number'];?></td>
<td><?php echo $rows['gender'];?></td>
<td> Options</td>
</tr>
<?php
}
}
?>
and finally my ajax:
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$.ajax({
url: 'views/admin/doctor-details.php',
type: 'POST',
success: function(result){
$("#response-doctor").html(result);
}
});
});
});
//Hide table on login
$("#show-doctor-details").hide();
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$("#show-doctor-details").show();
$("#show-patient-details").hide();
});
});
So, the gist is I want to click on options and show full details of John Doe.
Data attributes are a pretty easy to pass data. So when first appending make sure you have the id in the options field like this:
<td> Options</td>
Now you can get this id in your click event handler like this:
$(document).on('click','.options', function(){
var currentId = $(this).data('id');
...
});
Also you don't need two document readys. You can wrap both event handlers in one document ready.

Selecting multiple items to perform a task [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to select multiple items to delete the records in the database. But i am not being able to. My delete.php is fine. How can i use it to delete multiple selected items. I just created a checkbox. How can i make that checkbox work.
echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
while ($myrow = $result->fetch_assoc())
{
echo "<tr><td><input type='checkbox'name='mycheck'onclick='toggleform('document.myform','mycheck','ptext')'></td>";
echo "<TD>".$myrow['id']."</TD>";
echo "<TD>".$myrow['name']." </TD>";
echo "<TD>View ";
echo "Delete ";
echo "Edit";
}
<input type="button" name= "ptext" value="Delete selected">
You have to create checkbox name as array type i.e. mycheck[]. Then, In delete_all.php page, find total checked checkbox and delete it accordingly.
<form method='POST' action="delete_all.php">
<?php
echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
while ($myrow = $result->fetch_assoc())
{
echo "<tr><td><input type='checkbox' name='mycheck[]' value=".$myrow['id']."></td>";
echo "<TD>".$myrow['id']."</TD>";
echo "<TD>".$myrow['name']." </TD>";
echo "<TD>View ";
echo "Delete ";
echo "Edit";
}
?>
<input type="submit" name= "ptext" value="Delete selected">
</form>
delete_all.php
<?
extract($_POST);
$totalCheckboxChecked = sizeof($mycheck);
for($i=0;$i<$totalCheckboxChecked;$i++)
{
$idToDelete = $mycheck[$i];
echo "DELETE FROM table_Name WHERE id_ColumnName = '$idToDelete'";
// Execute Your Querys
}
?>
Are you getting all ids on the delete.php page? if yes then simple use
DELETE FROM tableName
WHERE id IN ($id1 , $id2 , $id3 , etc);
if not, The way i would do it is, i would use form, and then post it in the delete.php page using method post, and then simply take the values from super global
$_POST[]
and use the above query, hope this helps.

Get var from a PHP page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm having a problem with my PHP. I would like to send data from my page to another page. But it's all in PHP. I need to take an input value and a combobox value. And I don't know how to do it.
Here's my PHP :
<?php
session_start();
$q = intval($_GET['q']);
$db = mysql_connect('localhost', 'root', 'root');
mysql_select_db('Projet',$db);
$sql2 = "select IDControle, NomControle from Controle WHERE IDUser='".$_SESSION['id']."'";
$req2 = mysql_query($sql2) or die('Erreur SQL !<br>'.$sql2.'<br>'.mysql_error());
echo'<select id="controleChoix" name="controleChoix">';
while ($data2 = mysql_fetch_array($req2)){
echo '<option value="'.$data2['IDControle'].'">'.$data2['NomControle'].'</option>';
}
echo '</select>';
echo '<input type="hidden" name="selected_text" id="selected_text" value="" />';
$sql = "select ID, Nom, Prenom from User where Groupe ='".$q."'";
$req = mysql_query($sql) or die('Erreur SQL 2!<br>'.$sql.'<br>'.mysql_error());
echo '<ul class="list-group">';
while ($data = mysql_fetch_array($req)){
echo '<li class="list-group-item"><strong>Nom</strong> : '.$data['Nom'].' <br> <strong>Prénom</strong> : '.$data['Prenom'].'<br>';
echo '<input type="text" name="note" id="note"/> ';
echo '<a class="label label-success" href="ajouterNote.php?var1='.$data2['IDControle'].'&var2='.$data['ID'].'&var3='.$test.'"><i class="fa fa-fw fa-check"></i></a></li>';
}
echo '</ul>';
echo '<a class="btn btn-primary" href="#">Ajouter toutes les notes</i></a></li>';
?>
I need to send the input note.value, the select controleChoix.value and the intval q that I've received from another page.
As you already seem to use SESSIONS, you could easily transfer your data from one page to another using these $_SESSION variables.
https://secure.php.net/manual/en/book.session.php
edit: Please keep in mind that this isn't secure at all. The values can easily be changed by the user. Using this you might want to validate the values first, before using.

value "ID" for the selected row is not parsing on AJAX JQUERY [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an HTML table where the data was retrieved from a database using PHP hence I used a loop to echo the rows. Which created my initial problem of trying to retrieve the correct ID using AJAX. I was recommended using class to create my JQUERY Code I don't knoe what I am doing wrong.
My objective:
I am trying to parse the selected row's ID into my jquery code which then parses into a second php script.
This is my code of the HTML table:
<form id="mainform" >
<table id="table1">
<?php
$result = mysql_query("SELECT * FROM test");
while ($row = mysql_fetch_array($result))
{
$ID = $row['ID'];
echo "<tr data-rows='$ID'><td>$ID</td>";
echo "<td><input type='hidden' class='hf' value='$ID' /></td>";
echo "<td><input type='button' class='abc' value='select' /></td></tr>";
}
?>
</table>
</form>
This is my JAVASCRIPT code:
<script>
$( document ).ready(function() {
$(".abc").each(function(){
var btn = $(this);
btn.on("click",function(){
$("#newdiv").empty();
var rowId = $(this).closest('tr').attr('data-row');
$('.hf').each(function(){
$.get("boom.php", { ID: rowId } ,function(data) {
$("#newdiv").append(data);
});
});
});
});
});
</script>
I don't know where I went wrong?
I edted the CODE to my latest version
Convert this line
echo "<tr><td>".$ID."</td></tr>";
To this:
echo "<tr data-row='$ID'><td>$ID</td>";
Then, in your javascript, use the power of jQuery to find that attribute in your onClick handler.
btn.on("click",function() {
var rowId = $(this).closest('<tr>').attr('data-row');
}
The variable rowId should now match which row you are on. From there you can pass that in your AJAX calls.
EDIT: You also have mismatched selectors on your jQuery each() function. You have
$("#abc").each(function(){
}
But in the HTML, the button has no ID selector, only a class. Change that line to this:
$('.abc').each(function () {
}

Categories

Resources