I have the following code
<table width="100%" border="1" cellpadding="15" align="center">
<?php
$res = $dbcon->query("SELECT * FROM actor");
while($row=$res->fetch_array())
{
?>
<tr>
<td><?php echo $row['actor_id']; ?></td>
<td><a href="?del=<?php echo $row['actor_id']; ?>" onclick="return confirm('sure to delete !'); " >delete</a></td>
</tr>
<?php
}
?>
This works fine in every way but the problem is that i cant seem to set the onclick to work onside an echo in this next piece of code.
I'm learning mysql/php so i'm experimenting with various codes.
FOR ($j=0 ; $j < $rows; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_ASSOC);
echo 'Actor ID: ' . $row['actor_id'] . '<br>';
echo 'Delete'. '<br><br>';
}
$result->close();
$conn->close();
?>
The delete button works i just don't have a warning anymore, i can't figure out
how to properly insert onclick="return confirm('sure to delete !')
You will have to escape the quotes. This is what you need
echo 'Delete'. '<br><br>';
If you use PHP for templates maybe you should consider more template oriented syntax
<?php while($row=$res->fetch_array()): ?>
<tr>
<td><?= $row['actor_id'] ?></td>
<td><a href="?del=<?= $row['actor_id'] ?>" onclick="return confirm('sure to delete !'); " >delete</a></td>
</tr>
<?php endwhile ?>
It will make escaping a lot easier.
Yo can make it in another way:
<td><a class="delete" href="?del=<?= $row['actor_id'] ?>" >delete</a></td>
<script>
$(document).ready(function){
$('.delete').click(function(){
var id = $(this).attr('del');
var confirmText = "Are you sure you want to delete this object?";
if(confirm(confirmText)) {
$.ajax({
type:"POST",
url:url,
data: 'id=' + id,
success:function () {
// Here goes something...
},
});
}
return false;
});
});
</script>
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 have a table named tbl_video that consist of two fields (video_id, video_title What I want to add a load more button using Ajax JQuery like social networking sites .I am able to get the first two records but when I try second time nothing happens. I dont get any errors .What am i doing wrong .
this my index.php file I create table structure here and then fetch two records.And get the second record's id value and send it to the more.php file
<body>
<table class="table table-bordered" id="load_data_table">
<thead>
<tr>
<td width="15%">Video Id</td>
<td>Video Title</td>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result)) {
$video_id = $row['video_id']; ?>
<tr>
<td><?php echo $row['video_id']; ?></td>
<td><?php echo $row['video_title']; ?></td>
</tr>
<?php
}
?>
<tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
data-id="<?php echo $video_id; ?>">More</button></td></tr>
</table>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#more').click(function(){
var id=$(this).data("id");
$.ajax({
url:"more.php",
data:{id:id},
method:"post",
success:function(data){
if(data!=''){
$('#load_data_table').append(data);
$('#remove_row').remove();
}
else{
$('#more').html("No Data");
}
}
and this the more.php file.I think it is self explanatory
$id = $_POST['id'];
$output = '';
$db = mysqli_connect('localhost', 'root', '', 'testing');
$data = mysqli_query($db, "select * from tbl_video where video_id >{$id} limit 2");
if (mysqli_num_rows($data)) {
while ($row = mysqli_fetch_array($data)) {
$output .= '
<tbody>
<tr>
<td>'.$row['video_id'].'</td>
<td>'.$row['video_title'].'</td>
</tr>
';
}
$output .= '
<tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
data-id="'.$row['video_id'].'">More</button></td></tr></tbody>
';
echo $output;
}
any help would be appriciated.
When you add a new row and there is a new button for More
It has the same id as the original button so there is a conflict there
also the javascript needs to be initiliazed something like this
PHP:
$output .= '<tr id="remove_row"><td><button type="button" class="more btn btn-success form-control" data-id="'.$row['video_id'].'">More</button></td></tr></tbody>';
Javascript:
<script>
function moreButton() {
$('.more').click(function(){
var id=$(this).data("id");
$.ajax({
url:"more.php",
data:{id:id},
method:"post",
success:function(data){
if(data!=''){
$('#load_data_table').append(data);
$('#remove_row').remove();
moreButton();
}
else{
$('.more:last').html("No Data");
}
}
});
}
$(document).ready(function(){
moreButton();
});
Don't forget to remove the id of the more button to a class with .more
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");
}
});
I'm having a problem in updating the qty field in my database using ajax. I don't know where the error occur because when I tried to click the minus button it keeps on subtracting it multiple times. I've google same problem but didn't find any solution. Here is my code.
Here is my CONTROLLER:
public function deduct(){
$this->inpatient_model->deduct();
$data['inpatient'] = $this->inpatient_model->getinpatientlab();
$this->load->view('admin/queryinpatient',$data);
}
Here is my MODEL:
public function deduct(){
$this->db->select('*');
$this->db->from('inpatientlab');
$this->db->where('ilid',$this->input->post('lid'));
$query = $this->db->get();
$row = $query->result_array();
if($query->num_rows() > 0 ){
if($row[0]['qty'] > 1){
$uno = 1;
$this->db->set('qty','qty-'.$uno,FALSE)
->where('ilid',$this->input->post('lid'))
->update('inpatientlab');
}
}
}
Here is my VIEW:
<?php if($inpatient): ?>
<table class="table table-striped">
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
<th>Sub Total</th>
<th>Action</th>
</tr>
<?php foreach($inpatient as $rows): ?>
<tr>
<td><?php echo $rows->ldesc; ?></td>
<td><?php echo $rows->qty; ?></td>
<td><?php echo $rows->lprice; ?></td>
<td><?php echo number_format($rows->qty * $rows->lprice,2); ?></td>
<td>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnRemove"><i class="icon-trash"></i></button>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnMinus"><i class="icon-minus"></i></button>
</td>
</tr>
<?php endforeach; ?>
</table>
<script type="text/javascript" >
(function(){
$(document).on('click','.btnMinus',function(){
var lid = $(this).val(),
pid = $('.pid').val(),
dataString = "id=" + pid + "&lid=" + lid;
console.log(dataString);
$.ajax({
type:"POST",
url: base_url + "inpatient/deduct",
data:dataString,
success:function(data){
$('.pickedlab').html(data);
}
})
return false;
});
})()
My View in here is also loaded via ajax inside a bootstrap-modal. I really have no idea why it keeps on subtracting multiple times. Any help?
Have you tried using the browser debug and find out whether your AJAX is calling multiple times. I am not familiar with your Controller-Model settings, normally my models are pure POCO class only. All computations are done at the controller level.
I'm working on a small little social network project. So far i have a message system that all works fine, users can send and receive messages. I'm looking for a way to let users delete their own messages.
A 'delete' row has been set to display down the side of each message through an echo command.
Is there a way to make this text clickable so that when a user clicks it this then changes the value in the database from '0' to '1'?
Code:
<?php
$page_title = "Messages";
include('includes/header.php');
confirm_logged_in();
include('includes/mod_login/login_form.php');
?>
<div class="modtitle">
<div class="modtitle-text">Inbox</div>
</div>
<div class="modcontent">
<strong>Inbox</strong> | Sent Messages
| Deleted <br /><br />
<table
width="100%" border="0" cellpadding="5" cellspacing="0">
<tr
bgcolor="#CCCCCC">
<td width="30%"><strong>Recieved</strong></td>
<td width="20%"><strong>From</strong></td>
<td width="28%"><strong>Subject</strong></td>
<td width="0%"><strong>Read/Unread</strong></td>
<td width="0%"><strong>Delete</strong></td>
</tr>
<?php
$inbox_set = get_inbox();
while ($inbox =
mysql_fetch_array($inbox_set)) {
?>
<?php if ($inbox['read'] == 0) { ?> <tr bgcolor="#6666B3"> <?php }
if ($inbox['read'] == 1) { ?> <tr>
<?php } ?>
<td><?php
$datesent1 = $inbox['date_sent'];
echo "$datesent1"; ?></td>
<a href="profile.php?id={$inbox['from_user_id']}"><td><?php echo "<a
href=\"profile.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}";
?></td></a>
<td><?php echo "<strong>{$inbox['subject']}</strong>";
?></td>
<td><?php if ($inbox['read'] == 0) {
echo "Unread";
}
if ($inbox['read'] == 1) {
echo "Read";
}
?></td>
<td>
<?php
if ($inbox['delete'] == 0) {
echo "Delete";
}
if ($inbox['delete'] == 1) {
echo "$deleted_set;";
}
; ?></td>
</td>
<?php }
?>
</tr> </table>
</div>
<?php include('includes/footer.php'); ?>
Use AJAX request to trigger proper action on server side.
Remember to check authorization otherwise one may delete messages owned by other users.
Use jQuery + Ajax to do this task similar like this
$.ajax({
type:"POST",
url: url, // action page url
data: "id="+id,
success: function(msg){
if(msg) {
//action here
}
});