i need to highlight the entire row using jquery or php. is there any way to do this? or please suggest me the alternative?
Here is the code i'm working on:
<table id ="gdRows" class="table table-bordered">
<tr class="info">
<td><input type='checkbox' id="selectall" onclick = "selectAll(this)"></td>
<td><strong>Username</strong></td>
<td><strong>Password</strong></td>
</tr>
<?php
for($i=0; $row = $qry->fetch(); $i++){
$username = $row['username'];
$password = $row['password'];
?>
<tr class="success">
<td><input type="checkbox" name="chkDelete" ></td>
<td><?php echo $username; ?></td>
<td><?php echo $password; ?></td>
</tr>
<?php
}
?>
</table>
You could do it with this jQuery and a CSS class:
$('input[name="chkDelete"]').click(function () {
$(this).closest('tr').removeClass('foo');
if ($(this).is(':checked')) $(this).closest('tr').addClass('foo');
})
jsFiddle example
// jQuery
$('tr').find('input').on('click', function() {
if ($(this).prop('checked') === true) {
$(this).closest('tr').addClass('highlighted');
} else {
$(this).closest('tr').removeClass('highlighted');
}
});
// CSS
.highlighted td {
background: yellow;
}
Here's a Fiddle
You have to use jquery to highlit the row it is on jquery-10.1.2
$(function () {
$('#selectall').on('click',function(){
$('yourdivwhichyouwanttohighilit').css('background-color','red');
})
});
Try this,
<script src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$(".Row").click(function(){
if($(this).is(":checked")){
$(this).closest('tr').css({backgroundColor: 'red'});
}else{
$(this).closest('tr').css({backgroundColor: ''});
}
});
});
</script>
HTML:
<tr class="success">
<td><input type="checkbox" name="chkDelete" class="Row"></td>
<td><?php echo $username; ?></td>
<td><?php echo $password; ?></td>
</tr>
Related
I have a foreach loop on a tale row. Each row has an "edit" button to edit that row only. Using JS, I want to get a hidden open and edit the row.
Bellow is my code that I have tried but it works on one element only.
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction()" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction() {
<?php
foreach($sofas as $sofa)
{
?>
var x = document.getElementById("<?php echo $sofa["id"];?>");
<?php
}
?>
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
I'm still learning, so please help me resolve my issue. I'm expecting the code to work on each individual table row.
Why don't you use something like this:
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction('<?php echo $sofa["id"];?>')" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Code is very simple, you only need to send the element id to the js function.
I am new in php and ajax.
When I load data without pagination code it will display properly, but when I merge two data it will not work. As per my understanding my show data function is not working.
This is my code.
index.php
<?php
include 'dbconfig.php';
if(isset($_POST['deletes'])) {
$sql=mysql_query("delete from tbl_employees where emp_id='{$_POST['id']}'");
if($sql)
{
echo 'success delete';
}
}
if(isset($_POST['update'])) {
$sql="update tbl_employees SET emp_name='{$_POST['upfname']}',
emp_lname='{$_POST['uplname']}',
emp_email='{$_POST['upemail']}',
emp_phno= '{$_POST['upphno']}' where emp_id='{$_POST['upid']}'";
$ures= mysql_query($sql);
if($ures)
{
echo "sucessupdate";
}
}
if(isset($_POST['editvalue'])) {
$sql="select * from tbl_employees where emp_id='{$_POST['id']}'";
$eres=mysql_query($sql);
$erow= mysql_fetch_array($eres);
header("content-type:text/x-json");
echo json_encode($erow);
exit();
}
if(isset($_POST['buttonsave'])) {
$query="INSERT INTO tbl_employees( emp_name, emp_lname, emp_email, emp_phno) VALUES ('{$_POST['fname']}','{$_POST['lname']}','{$_POST['email']}','{$_POST['phno']}')";
$res= mysql_query($query);
if($res)
{
echo 'success';
}
exit();
}
if(isset($_POST['showtabledata'])) {
$sql="select * from tbl_employees";
$rsd=mysql_query($sql);
while($row = mysql_fetch_array($rsd)) {
$eid = $row['emp_id'];
$fname = $row['emp_name'];
$lname = $row['emp_lname'];
$email = $row['emp_email'];
$phno=$row['emp_phno'];
?>
<table border="1">
<tr>
<!--<td><?php //echo $eid; ?></td>-->
<td><?php echo $fname; ?></td>
<td><?php echo $lname; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $phno; ?></td>
<td><a ide=<?php echo $eid; ?> class='edit' href='#?ide=<?php echo $eid; ?>'>EDIT</a></td>
<td> <a idd=<?php echo $eid; ?> class='delete' href='#?idd=<?php echo $eid; ?>'>DELETE</a></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
<script type="text/javascript" src="newajax.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<!--<script type="text/javascript" src="pagination.js"></script>-->
<div id="container">
<form method="post" id="reg-form">
<table border="0">
<tr>
<td><input type="hidden" name="id" id="id" /></td>
</tr>
<tr>
<td><input type="text" name="fname" id="fname" placeholder="First Name" value="<?php echo $erow[1] ?>" required/></td>
</tr>
<tr>
<td><input type="text" name="lname" id="lname" placeholder="Last Name" value="<?php echo $erow[2] ?>"required/></td>
</tr>
<tr>
<td><input type="text" name="email" id="email" placeholder="Your Mail" value="<?php echo $erow[3] ?>" /></td>
</tr>
<tr>
<td><input type="text" name="phno" id="phno" placeholder="Contact No" value="<?php echo $erow[4] ?>" /></td>
</tr>
<tr>
<td><hr /></td>
</tr>
<tr>
<td align="center"><button type="submit" id="save" name="save">Register</button></td>
</tr>
<tr>
<td align="center"><button type="submit" id="update" name="update">Update</button></td>
</tr>
</table>
</form>
</div>
<div>
<table border="1">
<thead>
<!--<th>id</th>-->
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phno</th>
<th colspan="2">Action</th>
</thead>
<tbody id="showdata">
</tbody>
</table>
</div>
data.php
<?php
include('dbconfig.php');
$shw=$_POST['showdata'];
$per_page = 3;
if($_GET) {
$page=$_GET['page'];
}
//$page=1;
//getting table contents
if(isset($_POST['showdata'])) {
$start = ($page-1)*$per_page;
$sql = "select * from tbl_employees order by emp_id limit $start,$per_page";
$rsd = mysql_query($sql);
?>
<center>
<table id="tbl" align="center">
<!--<th><b>Id</b></th>-->
<th><b>FirstName</b></th>
<th><b>LastName</b></th>
<th><b>Email</b></th>
<th><b>Phno.</b></th>
<th colspan="2">Action</th>
<?php
while($row = mysql_fetch_array($rsd))
{
$id = $row['emp_id'];
$fname = $row['emp_name'];
$mname = $row['emp_lname'];
$lname = $row['emp_email'];
$phno=$row['emp_phno'];
?>
<tr>
<!--<td><?php// echo $id; ?></td>-->
<td><?php echo $fname; ?></td>
<td><?php echo $mname; ?></td>
<td><?php echo $lname; ?></td>
<td><?php echo $phno; ?></td>
<!--<td><?php //echo $lname; ?></td>-->
<td><a ide=<?php echo $id; ?> class='edit' href='#?ide=<?php echo $id; ?>'>EDIT</a></td>
<td> <a idd=<?php echo $id; ?> class='delete' href='#?idd=<?php echo $id; ?>'>DELETE</a></td>
</tr>
<?php
} //End while
?>
</table>
</center>
<style>
#tbl
{
width:800px;
border:1px solid #98bf21;
margin-top:50px;
}
/*#tbl tr:nth-child(odd) {
background: #00a2d1
}*/
#tbl td{
border:1px solid #00a2d1
}
#tbl th
{
background: #00a2d1;
border:1px solid #00a2d1
}
</style>
<?php
}
ajax.js
$(document).ready(function(){
function Display_Load() {
//$("#loading").fadeIn(100);
//$("#loading").html("<img src='ajax-loader.gif' />");
}
//Hide Loading Image
function Hide_Load() {
//$("#loading").fadeOut('slow');
};
//Default Starting Page Results
$("#pagination li:first").css({'color' : '#FF0084','border' : 'none'});
$("#content").load("data.php?page= 1");
$('#update').hide();
$("#reg-form").validate({
rules: {
fname:{
required:true
},
lname:{
required:true
},
email:{
required:true,
email:true
},
phno:{
required:true,
number:true,
minlength: 10,
maxlength: 10
},
},
messages: {
fname:"please enter first name",
lname:"plese enter your surname",
email:"please enter valid email address",
phno:{
required:"please enter your phno",
minlength:"ph no should be min 10 digit",
maxlength:"ph no should be max 10 digit"
}
},
// //submitHandler: submit
});
// $('#update').click(function(){
$(document).on('click','#update',function(){
$('#save').show();
$('#update').hide();
var eid=$('#id').val();
var ename=$('#fname').val();
var elname=$('#lname').val();
var eemail=$('#email').val();
var ephno=$('#phno').val();
$.ajax({
url:"index.php",
type:"POST",
async:false,
data:{
update:1,
upid:eid,
upfname:ename,
uplname:elname,
upemail:eemail,
upphno:ephno
},
success:function(up){
alert("success");
$('input[type=text]').val('');
//window.location.reload();
//document.getElementById("reg-form").reset();
showtabledata();
}
});
});
$('body').delegate('.delete','click',function(){
var IDdelete=$(this).attr('idd');
var test= window.confirm("are you sure you want to delete")
if(test)
{
{
$.ajax({
url:"index.php",
type:"POST",
async:false,
data:{
deletes:1,
id:IDdelete
},
success:function(){
alert("delete success");
alert(deletes);
//window.location.reload();
showtabledata();
}
});
}
}
});
// $('body').delegate('.edit','click',function(){
$(document).on('click','.edit',function(){
$('#save').hide();
$('#update').show();
var IDedit=$(this).attr('ide');
$.ajax({
url:"index.php",
type:"POST",
datatype:"JSON",
data:{
editvalue:1,
id :IDedit
},
success:function(show)
{
$('#id').val(show.emp_id),
$('#fname').val(show.emp_name);
$('#lname').val(show.emp_lname);
$('#email').val(show.emp_email);
$('#phno').val(show.emp_phno);
}
});
});
// $('#save').click(function(){
$(document).on('submit', '#reg-form', function(){
var firstname=$('#fname').val();
var lastname=$('#lname').val();
var emailadd=$('#email').val();
var phnum=$('#phno').val();
$.ajax({
url:"index.php",
type:"POST",
async:false,
data:{
buttonsave:1,
fname:firstname,
lname:lastname,
email:emailadd,
phno:phnum
},
success:function(result)
{
alert("success");
alert(result);
showtabledata();
}
});
});
$("#pagination li").click(function(){
//Display_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
//Loading Data
var pageNum = this.id;
$("#content").load("data.php?page=" + pageNum);
});
});
function showtabledata()
{
//var showdata=1;
$.ajax({
url:"data.php",
type:"POST",
async:false,
data:{
showdata:1
},
success:function(re)
{
//alert(re);
// $('#showdata').html(re);
}
});
//$("#load").load("/load.php") ;
}
Data doesn't display without load.
Please some give suggestion.
i have a search input field in my page. currently i am displaying data from the database successfully in a table(id="table_userinfo"). Now i want to add the live search functionality to my table(id="table_userinfo") as well. i am using php codeigniter.
in my user_model i have:
function search_userInfo($keyword)
{
$this->db->like('DeviceName',$keyword);
$this->db->or_like('RegistrationDateTime',$keyword);
$this->db->or_like('LastUpdateDateTime',$keyword);
$this->db->or_like('AppVersion ',$keyword);
$this->db->or_like('iOSVersion',$keyword);
$this->db->or_like('DeviceDID',$keyword);
$this->db->or_like('DeviceToken',$keyword);
$query = $this->db->get('userinfo');
if($query)
{
return $query->result();
}
else
{
return NULL;
}
}
in my login_controller:
function displayDatabase()
{
$data['tableInfo']=$this->user_model->fetchData();
$this->load->view('adminPanel_view',$data);
}
function search()
{
$search=$this->input->post('searchString');
$data['tableInfo']=$this->user_model->search_userInfo($search);
}
in my adminPanel_view page, i have:
<script>
$('#search').keyup(function(){
var input_data = {searchString: $('#search').val() };
//console.log(input_data);
$.ajax({
type:"POST",
url:base_url+'index.php/login_controller/search',
data:input_data,
success:function(data){
if(data.length>0)
{
console.log(data);
}
}
});
});
</script>
<table class="table table-bordered table-hover table-full-width" id="table_userinfo">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php $no=0;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $r->DeviceName ?></td>
<td><?php echo $r->DeviceType ?></td>
<td><?php echo $r->RegistrationDateTime ?></td>
<td><?php echo $r->LastUpdateDateTime ?></td>
<td><?php echo $r->LastPushNotificationSent ?></td>
<td><?php echo "Actions" ?></td>
</tr>
<?php $no+=1;?>
<?php endforeach; ?>
</tbody>
</table>
this is the search bar that i am using in addition to this table
<div id="" class="dataTables_filter">
<label>
<input placeholder="Search" class="form-control input-sm" aria-controls="sample_1" type="text" id="search" name="search_userinfo">
</label>
</div>
how do i display the data that i am getting through the Ajax call and show it in the table(id="search_userinfo).
inside of function search() return it as:
echo json_encode($this->user_model->search_userInfo($search));
and inside of javascript do this
<script>
$('#search').keyup(function(){
var input_data = {searchString: $('#search').val() };
//console.log(input_data);
$.ajax({
type:"POST",
url:base_url+'index.php/login_controller/search',
data:input_data,
dataType: "json",
success:function(data){
$("#table_userinfo").html(" "); //clear everything from table
data.forEach(function(entry) {
$("#table_userinfo").append("<td>"+entry.DeviceName+"</td><td>");
//just add everything here
});
}
});
});
</script>
Your Controller
function search()
{
$search=$this->input->post('searchString');
$data['tableInfo']=$this->user_model->search_userInfo($search);
print_r(json_encode($data['tableInfo']));exit
}
In your ajax add this line
dataType:'json';
Now In Your Success function You will get an json for that data ,You can append like this,
$.each(data, function(i, item) {
alert(item.DeviceName); // append data where you need to append
});
But Please be sure about amount of data you get in success function, with keyup
I have add_person.ctp file, and there I have Inputs and function, but this function is not working, My question is why its not working, I dont see any mistake in this code. If I click button, then this doing nothing.
Here is my code
<h2>People</h2>
<table id="mytable">
<tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th><th>Gender</th></tr>
<tr id="person0" style="display:none;">
<td><?php echo $this->Form->button(' - ',array('type'=>'button','title'=>'Click Here to remove this person')); ?></td>
<td><?php echo $this->Form->input('unused.lastName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.firstName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.email',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.gender',array('label'=>'','type'=>'select','options'=>array('M'=>'M','F'=>'F','T'=>'T'))); ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another person','onclick'=>'addPerson()')); ?> </td><td></td><td></td><td></td><td></td></tr>
</table>
<?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
<script type='text/javascript'>
var lastRow=0;
function addPerson() {
lastRow++;
$("#mytable tbody>tr:#person0").clone(true).attr('id','person'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr:#trAdd");
$("#person"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
$("#person"+lastRow+" input:first").attr('name','data[Person]['+lastRow+'][lastName]').attr('id','personLastName'+lastRow);
$("#person"+lastRow+" input:eq(1)").attr('name','data[Person]['+lastRow+'][firstName]').attr('id','personFirstName'+lastRow);
$("#person"+lastRow+" input:eq(2)").attr('name','data[Person]['+lastRow+'][email]').attr('id','personEmail'+lastRow);
$("#person"+lastRow+" select").attr('name','data[Person]['+lastRow+'][gender]').attr('id','personGender'+lastRow);
}
function removePerson(x) {
$("#person"+x).remove();
}
</script>
Thank you for helping !
You have wrong selector here #mytable tbody>tr:#person0 and here #mytable tbody>tr:#trAdd. Replace with #mytable tbody>tr#person0 and #mytable tbody>tr#trAdd respectively. Keep in mind there is no need to use colon symbol in selectors.
I found a tutorial for this with single ID. enter link description here
i have modified as per my knowledge for multiple id, i cannot able to get the id using ajax. i am not good with ajax. I have attached both code. can anyone tell how i can fix it for multiple id
i also attached a screen shot screenshot
index.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
include("connection1.php");
$result = mysql_query("SELECT * FROM mytable ORDER BY id");
?>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<div>
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["text"]; ?></td>
<td><script type="text/javascript">
$(document).ready(function(){
$('#myonoffswitch<?php echo $row["id"]; ?>').click(function(){
var myonoffswitch<?php echo $row["id"]; ?>=$('#myonoffswitch<?php echo $row["id"]; ?>').val();
if ($("#myonoffswitch<?php echo $row["id"]; ?>:checked").length == 0)
{
var a<?php echo $row["id"]; ?>=myonoffswitch<?php echo $row["id"]; ?>;
}
else
{
var a<?php echo $row["id"]; ?>="off";
}
$.ajax({
type: "POST",
url: "ajax.php",
data: "value="+a<?php echo $row["id"]; ?>,
success: function(html){
$("#display").html(html).show();
}
});
});
});
</script><div class="onoffswitch">
<input type="hidden" value="<?php echo $row["id"]; ?>" name="ids" />
<input type="checkbox" name="onoffswitch<?php echo $row["id"]; ?>" class="onoffswitch-checkbox" id="myonoffswitch<?php echo $row["id"]; ?>"
<?php
$query3=mysql_query("select * from mytable where id=$row[id]");
$query4=mysql_fetch_array($query3);
if($query4['text']=="off")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<div id="display"></div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Ajax.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
$id=$_POST['ids'];
mysql_query("update mytable set text='$value' where id=2");
echo "<h2>You have Chosen the button status as:" .$value."</h2>";
}
?>
For your own sake ;) :
pls organize your code
add an output if something cant be loaded from your db, look therefor in the php example
why the 2. Query, you already loaded all data from 'mytable' with "select *"?
nevertheless here an example based on your code (not tested):
index.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
include("connection1.php");
$result = mysql_query("SELECT * FROM mytable ORDER BY id");
?>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<div>
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["text"]; ?></td>
<td>
<div class="onoffswitch">
<input type="hidden" value="<?php echo $row["id"]; ?>" />
<input type="checkbox" class="onoffswitch-checkbox"
<?php
if($row['text']=="off")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<div id="display">
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('.onoffswitch').click(function(){
var hiddenValueID = $(this).children(':hidden').val();
if ($(this).children(':checked').length == 0)
{
var valueData = 'on';
}
else
{
var valueData = 'off';
}
$.ajax({
type: "POST",
url: "ajax.php",
data: {value: valueData, id: hiddenValueID} ,
done: function(html){
$("#display").html(html).show();
}
});
});
});
</script>
</div>
ajax.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
$id=$_POST['id'];
mysql_query("update mytable set text='$value' where id=$id");
echo "<h2>You have Chosen the button status as:" .$value."</h2>";
}
?>
You can look in your browsers-console if you get an answer from your ajax-request.