How to update Database with data from divs with same id - php

I am trying to using Ajax to update the database to skip the standard submission and not refresh the page just update results (entries)
So, first I have a table with some data which is already saved into links table on my database
So here is my codes:
1st links.php:
<div id="links_list">
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Link</td>
<td>Posted By</td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$rows = $db->query("SELECT * FROM `links` ");
$totalLinks = $rows->num_rows;
if($totalLinks == 0){
echo "<tr><td colspan='5'><div class='' style='font-size: 16px;color: #F44336;background: #fff;width: fit-content;margin-left: 4px;'><i class='fa fa-meh-o'></i> <b>Error :</b> No LINK Has Been Added Yet.</div></td></tr>";
} else {
while ($row = mysqli_fetch_assoc($rows)) {
?>
<tr>
<td id="links_row" valign="middle">
<input type="checkbox" id="selectr-link-<?php echo $row['link_id']; ?>" name="select_link[]" value="<?php echo $row['link_id']; ?>" style="float:left;margin-top:3px;">
<span style="float:left;width:25px;height:20px;margin: 0 3px;text-align:center;font-size: 20px;line-height: 22px;color: #009688;"><?php if($row['link_type'] == "Iframe"){ echo "<i class='fa fa-tv'></i>"; } elseif($row['link_type'] == "Direct"){ echo "<i class='fa fa-globe'></i>"; } ?></span>
<span class="spanInMobile" style="float:left;height:20px;margin-right:5px;"><?php echo $row['link_url']; ?></span>
</td>
<td valign="middle">
<?php echo $row['posted_by']; ?>
</td>
<td valign="middle" class="text-right">
<?php $id = $row['link_id']; $status = $row['link_status']; if($status == '0'){ ?>
<span title="Enable" class="disabled-entry" id="enable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } elseif($status == '1'){ ?>
<span title="Disable" class="enabled-entry" id="disable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } ?>
<script>
$(document).ready(function(){
$('#enable_link').CLICK(function(){
var link_id = $('#enable_link').attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
});
</script>
<script>
$(document).ready(function(){
$('#disable_link').click(function(){
var link_id = $('#disable_link').attr('data-id');
$.ajax({
url: "../src/ajax/disable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
</div>
** enable.php **
<?php
require_once ('../system/config.php');
$link_id = $_POST['link_id'];
$db->query("UPDATE `links` SET `link_status` = '1' WHERE `link_id` = '".$link_id."'");
?>
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Link</td>
<td>Posted By</td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$rows = $db->query("SELECT * FROM `links` ");
$totalLinks = $rows->num_rows;
if($totalLinks == 0){
echo "<tr><td colspan='5'><div class='' style='font-size: 16px;color: #F44336;background: #fff;width: fit-content;margin-left: 4px;'><i class='fa fa-meh-o'></i> <b>Error :</b> No LINK Has Been Added Yet.</div></td></tr>";
} else {
while ($row = mysqli_fetch_assoc($rows)) {
?>
<tr>
<td id="links_row" valign="middle">
<input type="checkbox" id="selectr-link-<?php echo $row['link_id']; ?>" name="select_link[]" value="<?php echo $row['link_id']; ?>" style="float:left;margin-top:3px;">
<span style="float:left;width:25px;height:20px;margin: 0 3px;text-align:center;font-size: 20px;line-height: 22px;color: #009688;"><?php if($row['link_type'] == "Iframe"){ echo "<i class='fa fa-tv'></i>"; } elseif($row['link_type'] == "Direct"){ echo "<i class='fa fa-globe'></i>"; } ?></span>
<span class="spanInMobile" style="float:left;height:20px;margin-right:5px;"><?php echo $row['link_url']; ?></span>
</td>
<td valign="middle">
<?php echo $row['posted_by']; ?>
</td>
<td valign="middle" class="text-right">
<?php $id = $row['link_id']; $status = $row['link_status']; if($status == '0'){ ?>
<span title="Enable" class="disabled-entry" id="enable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } elseif($status == '1'){ ?>
<span title="Disable" class="enabled-entry" id="disable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } ?>
<script>
$(document).ready(function(){
$('#enable_link').click(function(){
var link_id = $('#enable_link').attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
<script>
$(document).ready(function(){
$('#disable_link').click(function(){
var link_id = $('#disable_link').attr('data-id');
$.ajax({
url: "../src/ajax/disable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
It works only if the first row was been enabled and then the second one... ect but if you want to enable the 2nd row for example it won't work until you enable the first one
I tried with each() function but I am not sure that the code was right written

you can't use same ID for pointing multiple elements in DOM, instead use CLASS.
so change your click event binding to a class instead of ID
Example
$(document).ready(function(){
$('.enabled-entry').click(function(){
var link_id = $(this).attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
Also add this script after while loop

Related

How to call alert after ajax success data request?

view:
<script>
$(document).ready(function(){
$(".user_id").click(function(){
jid = $(".jid").attr('id');
var uids = [];
$("input[name='user_id']").map(function() {
uids.push({id: this.id, value: this.checked ? 1 : 0});
});
$.ajax({
type:"POST",
data:{"jid":jid, "uid":uids},
url:"<?php echo base_url(); ?>shortlist",
success:function(data){
$(".short_emp").html(data);
}
});
});
$(document).on('click' ,".user_idd" , function(){
jidd = $(".jidd").attr('id');
var uidss = [];
$("input[name='user_idd']").map(function() {
uidss.push({id: this.id, value: this.checked ? 1 : 0});
});
$.ajax({
type:"POST",
data:{"jidd":jidd, "uidss":uidss},
url:"<?php echo base_url(); ?>interview",
success:function(data){
alert(data);
//$(".short_interview").html(data);
}
});
});
});
</script>
<div class="wizard_horizontal">
<h2>Received</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody>
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$this->db->where('share_with_emp','1');
$sqll = $this->db->get();
if($sqll->num_rows() > 0)
{
$resultt = $sqll->result_array();
foreach($resultt as $roww)
{
?>
<tr>
<td>
<h6><?php echo $roww['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jid" class="jid form-control" id="<?php echo $roww['jid']; ?>"/>
<input type="checkbox" name="user_id" id="<?php echo $roww["uid"]; ?>" class="user_id" <?php if($roww['shortlist']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<p>No data Found</p>";
}
?>
</tbody>
</table>
</section>
<h2>Shortlisted</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody class="short_emp">
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$this->db->where('shortlist','1');
$sql_short = $this->db->get();
$result_short = $sql_short->result_array();
foreach($result_short as $fetch)
{
?>
<tr>
<td>
<h6><?php echo $fetch['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jidd" class="jidd form-control" id="<?php echo $fetch['jid']; ?>"/>
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</section>
</div>
controller:
<?php
public function shortlist()
{
$jid = $this->input->post('jid');
$uid = $this->input->post('uid');
foreach($uid as $user)
{
$data = array('shortlist'=> $user['value']);
$where = 'jid="'.$jid.'" and uid="'.$user['id'].'"';
$this->db->where($where);
$query = $this->db->update('upload_detail',$data);
}
if($query==true)
{
$this->db->select('*');
$this->db->from('upload_detail');
$where = "jid='".$jid."' and shortlist='1'";
$this->db->where($where);
$sql_short = $this->db->get();
if($sql_short->num_rows() > 0)
{
$result_short = $sql_short->result_array();
foreach($result_short as $fetch)
{
?>
<tr>
<td>
<h6><?php echo $fetch['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jidd" class="jidd form-control" id="<?php echo $fetch['jid']; ?>"/>
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<p>No data Found</p>";
}
}
else
{
echo "<p>Unable to Proceed</p>";
}
}
public function interview()
{
$jidd = $this->input->post('jidd');
$uidd= $this->input->post('uidss');
echo $jidd;
print_r($uidd);
}
?>
In my code, I have table first with heading Recieved where I got data. Now, what am I doing when I click on class="user_id" it run successfully and load content over $(.short_emp).html(data) which is the part of heading shortlist inside the <tbody class="short_emp"> but when I click on $(".user_idd").click(function(){ then no alert are showing I don't know why? So How can I resolve this issue? Please help me.
Thank You
try to add attribute data-id in an input field and call that in a function
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" data-id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
$(document).on("click", ".user_idd", function(){
var attr = $(this).attr('data-id');
alert(attr);
});
hope it will work for you :)

showdata function not working

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.

unable to do inline editing with php code igniter and jquery. Not able to update table values in the database

this is my edit_table.php in which dynamic table is generated and by on click function i try to update database.
<script>
function table_edit(btn)
{
var id=btn.id;
if(btn.value=="Edit")
{
document.getElementById('college_id'+id).setAttribute("contenteditable" , "true");
document.getElementById('name'+id).setAttribute("contenteditable" , "true");
//document.getElementById('university_id'+id).removeAttribute("Readonly");
document.getElementById('university_id'+id).setAttribute("contenteditable" , "true");
document.getElementById(id).value="Save";
return false;
}
if(btn.value=="Save")
{
document.getElementById('name'+id).removeAttribute("contenteditable");
document.getElementById('college_id'+id).removeAttribute("contenteditable");
// document.getElementById('university_id'+id).setAttribute("Readonly" , "readonly");
document.getElementById('university_id'+id).removeAttribute("contenteditable");
document.getElementById(id).value="Edit";
var newuniversity_id=document.getElementById('university_id'+id).innerHTML;
var newcollege_id=document.getElementById('college_id'+id).innerHTML;
var newname=document.getElementById('name'+id).innerHTML;
alert(newname+" "+newcollege_id+" "+newuniversity_id+" "+id);
var dataString = 'name1=' + name + '&university_id=' + newuniversity_id + '&college_id=' + newcollege_id + '&id=' + id;
$.ajax({
type: "POST",
url: "update_table_data",
data: dataString,
success: function(html) {
alert("ajax calling done");
}
});
return true;
}
}
function table_update(){
//document.getElementById("edit_rows").value="Save"
alert("Working!");
}
</script>
<?php
function test()
{
echo "<script>alert('hello');</script>";
} ?>
<div id="page-content-wrapper">
<div class="container-fluid">
<i class="fa fa-bars"></i> <span>Menu</span>
<div class="content-block">
<div class="login-sign forgot_pass login forgot text-center">
<!-- <form action="<?php echo base_url(); ?>edit_table" method="post" accept-charset="utf-8"> -->
<div class="login-signup-head">Edit Table</div>
<table>
<tbody>
<thead>
<tr class="tredit">
<!-- <th width="10"></th> -->
<th> Id </th>
<th > University Id </th>
<th > Name </th>
<th > College Id </th>
<th width="100"> </th>
</tr>
</thead>
<tbody>
<?php
foreach($student_data as $row){
?>
<tr class="tredit" id="row_edit">
<td><?php echo $row['id']; ?></td>
<td id="university_id<?php echo $row['id']; ?>" ><?php echo $row['university_id']; ?></td>
<td id="name<?php echo $row['id']; ?>" ><?php echo $row['name']; ?></td>
<td id="college_id<?php echo $row['id']; ?>" > <?php echo $row['college_id']; ?></td>
<td><input type='button' class='editable' onclick=" return table_edit(this)" value='Edit' id= "<?php echo $row['id']; ?>">
<input type='button' class='tabledelete' onclick="table_update()" value='Delete' ></td>
</tr>
<?php
}
?>
</div>
</div>
</div>
<!-- </form> -->
</div>
</div>
</tbody>
</table>
and this is the route
$route['update_table_data'] = 'home/update_table';
and the following functions are for fetching the data as well as updating the data.
public function tableedit(){
if($this->session->userdata('university_id')){
$user_id = $this->session->userdata('university_id');
$data['user_id'] = $user_id;
$data['student_data'] = $this->home_model->table_data($user_id);
$this->load->view('home/new-header',$data);
$this->load->view('home/left_university_sidebar',$data);
$this->load->view('edit_table',$data);
$this->load->view('home/footer');
} else {
$data['error'] = 'Table Cannot be viewed!';
$this->load->view('home/header');
$this->load->view('edit_table');
$this->load->view('home/footer');
}
}
public function update_table() {
if($this->session->userdata('university_id')){
if($this->input->server("REQUEST_METHOD") === "POST"){
$university_id=$_POST['university_id'];
$college_id=$_POST['college_id'];
$id=$_POST['id'];
$name=$_POST['name'];
$this->home_model->update_table($university_id,$college_id,$name,$id);
}
}
}
and finally this is the model for update query.
public function update_table($university_id,$college_id,$name,$id) {
$data = array('University Id' => $university_id, 'name' => $name, 'College Id' => $college_id);
$this->db->where('id', $id);
$this->db->update('college', $data);
}
editable table
edit values
but when i save and refresh the page, no change occur in the table.
my issue is database is not updated when i update it from save button.
first of all :
Formatting your code , that must be clean and easily readable
for your ajax call you dont need use routed url , it work at background, you can use like this :
put : <script>var base_url='<?php echo base_url(); ?>'</script>
in your header menu , or upper place that you use in your theme.
then make ajax call like this:
$.ajax({
type: "POST",
url: base_url+"youController/yourAction",
data: dataString,
success: function(html) {
alert("ajax calling done");
}
});

Store Value from On/Off Button in MySQL Database Using PHP Ajax

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.

Can't call Ajax function from my php script, working with live table it

I'm working on this project that uses a live table edit style (works perfectly) but trying to include the twitter style follow and unfollow in one of the columns (The last one precisely) the php works well but I've having problems returning the data to ajax to post to another php script.
Below shows the major part of the script (The php and ajax). I included a php comment on the important area of interest.
<?php $query_pag_data = "SELECT * FROM applicant_result WHERE year='$year' AND
class='$class' ORDER by candidate_no "; $uid=strip_tags($id);
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$finaldata = "";
$tablehead= '';
$tablehead= "<tr>
<th class='data'>#</th>
<th class='data'>Applicant ID</th>
<th class='data'>Year</th>
<th class='data'>Class</th>
<th class='data'>$subject_1</th>
<th class='data'>$subject_2</th>
<th class='data'>$subject_3</th>
<th class='data'>$subject_4</th>
<th class='data'>$subject_5</th>
<th class='data'>Total</th>
<th class='data'>Status</th>
<th class='data'>Interview</th>
</tr>";
while($row = mysql_fetch_array($result_pag_data)) {
$id=htmlentities($row['candidate_no']);
$subject_1=htmlentities($row['subject_1']);
$subject_2=htmlentities($row['subject_2']);
$subject_3=htmlentities($row['subject_3']);
$subject_4=htmlentities($row['subject_4']);
$subject_5=htmlentities($row['subject_5']);
$total=htmlentities($row['total']);
$status=htmlentities($row['interview']);
$uid= strip_tags($row['candidate_no']);
/* HELLO FORUMITES, THIS IS THE MAJOR AREA OF FOCUS HERE */
if($status!=0){$button="
<span id='loading<?php echo $uid; ?>'></span>
<span class='button following' id='following<?php echo $uid; ?>' `onClick='follow_or_unfollow(<?php echo $uid; ?>,'following');'>Following</span>`
<span style='display:none;' class='button follow' id='follow<?php
echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
";}
else{
$button="
<span id='loading<?php echo $uid; ?>'></span>
<span class='button follow' id='follow<?php echo $uid; ?>'
onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
<span class='button following' style='display:none;'`id='following<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid;` ?>,'following');'>Following</span>
";}
$tabledata.="<tr id='$id' class='edit_tr'>
<td class='edit_td' ><span class='text'>$counter</span></td>
<td class='edit_td' ><span class='text'>$id</span></td>
<td class='edit_td' ><span class='text'>$year</span>
<input type='hidden' value='$year' class='editbox' id='six_input_$id' /></td>
<td class='edit_td' ><span class='text'>$class</span>
<input type='hidden' value='$class' class='tbox' id='seven_input_$id' /></td>
<td class='edit_td' >
<span id='one_$id' class='text'>$subject_1</span>
<input type='text' value='$subject_1' class='editbox' id='one_input_$id' /></td>
<td class='edit_td' ><span id='two_$id' class='text'>$subject_2</span>
<input type='text' value='$subject_2' class='editbox' id='two_input_$id'/></td>
<td class='edit_td' ><span id='three_$id' class='text'>$subject_3 </span>
<input type='text' value='$subject_3' class='editbox' id='three_input_$id'/></td>
<td class='edit_td' ><span id='four_$id' class='text'>$subject_4</span>
<input type='text' value='$subject_4' class='editbox' id='four_input_$id' /></td>
<td class='edit_td' ><span id='five_$id' class='text'>$subject_5</span>
<input type='text' value='$subject_5' class='editbox' id='five_input_$id' /></td>
<td class='edit_td' ><span class='text'>$total</span></td>
<td class='edit_td' ><span class='text'>$status</span></td>
<td>$button </td>
</tr>";
$counter++;
}
$finaldata = "<table width='100%'>".$tablehead." ".$tabledata. "</table>";
echo $finaldata;
NOW THE AJAX
<script type="text/javascript">
$(document).ready(function() {
$('.following').hover(function() {
$(this).text('Unfollow');
}, function() {
$(this).text("Following");
});
});
function follow_or_unfollow(id, action) {
var dataString = "id=" + id;
$.ajax({
type: "POST",
url: "follow_or_unfollow.php",
data: dataString,
beforeSend: function() {
if (action == "following") {
$("#following" + id).hide();
$("#loading" + id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
else if (action == "follow") {
$("#follow" + id).hide();
$("#loading" + id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
},
success: function(response) {
if (action == "following") {
$("#loading" + id).html('');
$("#follow" + id).show();
}
else if (action == "follow") {
$("#loading" + id).html('');
$("#following" + id).show();
}
}
});
}
</script>
Try changing your ajax type to GET type: "GET",
Also how are you receiving your global at the other end of the php
Did you used GET or POST?

Categories