jQuery Datatable: Edit button - php

I added Edit button to my jQuery Datatable. When the user clicks on this button, the row becomes editable. Updated row should be saved to MySQL DB, but here comes the problem - the updated row is not saved to DB. Firebug does not show any error message. Can someone help me figure out the problem?
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#newspaper-b').dataTable({
"sPaginationType":"full_numbers",
"aaSorting":[[3, "asc"]],
"bJQueryUI":true
});
$(".edit_but").click(function() {
var ID=$(this).attr('id');
$("#first_"+ID).hide();
$("#last_"+ID).hide();
$("#first_input_"+ID).show();
$("#last_input_"+ID).show();
});
$(".edit_tr").change(function() {
var ID=$(this).attr('id');
var first=$("#first_input_"+ID).val();
var last=$("#last_input_"+ID).val();
var dataString = 'flightNum='+ ID +'&from='+first+'&STADate='+last;
$("#first_"+ID).html('<img src="load.gif" />'); // Loading image
if(first.length>0&& last.length>0) {
$.ajax({
type: "POST",
url: "callpage.php?page=tables/edit.php",
data: dataString,
cache: false,
success: function(html) {
$("#first_"+ID).html(first);
$("#last_"+ID).html(last);
}
});
} else
{
alert('All fields must be filled out.');
}
});
// Edit input box click action
$(".editbox").mouseup(function() {
return false
});
// Outside click action
$(document).mouseup(function() {
$(".editbox").hide();
$(".text").show();
});
$("tr").click(function(){
$(this).addClass("selected").siblings().removeClass("selected");
});
});
</script>
<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
<th scope="col">Flt Num</th>
<th scope="col">From</th>
<th scope="col">STA Date</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):
$flightNum=$row['flightNum'];
$from=$row['frm'];
$STADate=$row['STADate'];
?>
<tr id="<?php echo $flightNum; ?>" class="edit_tr">
<td><?php echo $row['flightNum'];?></td>
<td class="edit_td">
<span id="first_<?php echo $flightNum; ?>" class="text">
<?php echo $from;?>
</span>
<input type="text" value="<?php echo $from;?>"
class="editbox" id="first_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td">
<span id="last_<?php echo $flightNum; ?>" class="text">
<?php echo $STADate; ?>
</span>
<input type="text" value="<?php echo $STADate; ?>"
class="editbox" id="last_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td"><?php echo $row['pkID']; ?></td>
<td id="<?php echo $flightNum; ?>" class="edit_but">
<div>
<img src='images/edit.png' alt='Edit' />
</div>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
edit.php
<?php
include_once 'include/DatabaseConnector.php';
if(isset($_POST['flightNum'])) {
$flightnum=$_POST['flightNum'];
$from=$_POST['from'];
$STADate=$_POST['STADate'];
$query = 'UPDATE flightschedule
SET frm="'.$from.'",STADate="'.$STADate.'"
WHERE flightNum="'.$flightNum.'"';
DatabaseConnector::ExecuteQuery($query);
echo '1';
} else {
echo '0';
}
?>

Try $(".edit_tr input").change() as the trigger.
Change events are limited to inputs, selects and textareas only, but you've tried binding it to a table row. The code above will bind it to any inputs inside the row with the class.

Related

Fetching data in PHP

I have <table> that display data. I have a button if I click, the data will be displayed in #datacontainer. But the problem is only first row in my table is shown.
Here's my code
HTML - here's the table, imagine that I have 4 data in my table (DATABASE) but the first row only shows the data when you click the button.
<table>
<tr>
<th width="15%;">Image</th>
<th width="0">ID</th>
<th width="50%;">Name</th>
<th>Action</th>
</tr>
<?php while ($row = mysqli_fetch_array($accounts)) { ?>
<tr>
<td><img src="images/<?php echo $row['image']; ?>"></td>
<td><?php echo $row['id']; ?><input type="text" id="text_id" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['lastname']; ?>, <?php echo $row['firstname']; ?></td>
<td>
<i class="fab fa-reddit-alien" id="showdatacontainer" onclick="return chk()"></i>
<i class="far fa-edit" style="margin: 0 7% 0 7%;"></i>
<i class="fas fa-trash"></i>
</td>
<td>
</td>
<td>
</td>
</tr>
<?php } ?>
</table>
<div id="datacontainer"></div>
Javascript - and here's my jquery code
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function chk()
{
$("#datacontainer").slideToggle(300);
var id = document.getElementById('text_id').value;
var dataString = 'id='+ id;
$.ajax({
type: "post",
url: "showdata.php",
data:dataString,
cache:false,
success: function(data){
$("#datacontainer").html(data);
}
});
return false;
}
</script>
PHP - and here's my PHP code "showdata.php"
<?php
$db = mysqli_connect('localhost', 'root', '', 'psbr');
if (isset($_POST['id'])) {
$id = $_POST['id'];
$accounts = mysqli_query($db, "SELECT * from accounts where id = '$id'");
}
?>
<table>
<?php while ($row = mysqli_fetch_array($accounts)) { ?>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<tr>
<td><?php echo $row['lastname']; ?></td>
</tr>
<?php } ?>
</table>
<style type="text/css">
.w
{
font-size: 12vh;
}
</style>
please help me out of this problem thankyou! :D
You are grabbing the value by ID, yet there are several elements with the same ID, because you are in a loop, therefore, you will always get the first answer.
<td><?php echo $row['id']; ?><input type="text" id="text_id" value="<?php echo $row['id']; ?>"></td>

Delete SQL Record From Within jQuery Dialog

I have a table generated from a php while loop displaying records in a file called userlist.php. Using data-id, each table now has its SQL record id associated with it.
In my container.php file, I have my initial jQuery code which loads userlist.php into a div on a 1 second interval. Now, when clicking each separate table, I hide userlist.php and show another file in a div called userdetails.php, which contains more information about each user.
Now, I am trying to use a jQuery dialog window to show a confirmation message before a record is deleted. In userdetails.php you can see an ajax snippet which I have modified based on some suggestions. The problem is that inserting this into the file prevents each table to be clicked at all to show the details. If I remove it, details can be accessed.
Here is my code without the ajax snippet, including it does not allow accessing the details:
Click Here
container.php
<div id="userlist"></div>
<div id="userdetails"></div>
<script>
setInterval(function(){
$("#userlist").load("userlist.php");
}, 1000);
$('#userdetails').hide();
$(document).on("click",".user", function() {
var id = $(this).attr("data-id");
$.get("userdetails.php", {id: id}).done(function(data) {
$('#userdetails').html(data);
$('#userdetails').show();
$('#userlist').hide();
});
})
$(document).on("click","#back", function() {
$('#userlist').show();
$('#userdetails').hide();
});
</script>
userlist.php
<?php
include 'dbh.php';
$result = $conn->query("SELECT * FROM users");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$color = array("ADMIN"=>"#ebc45b", "MOD"=>"#8fce61", "USER"=>"#9b9ed2");
?>
<table data-id="<?php echo $row['id']; ?>" class="user" title="User ID: <?php echo $row['id']; ?>">
<tr>
<td align="left">User ID:</td>
<td align="right"><?php echo $row['id']; ?></td>
</tr>
<tr>
<td align="left"><?php echo $row['address']; ?></td>
<td align="right"><?php echo $row['zip']; ?></td>
</tr>
<tr>
<td align="left"><?php echo $row['city']; ?></td>
<td align="right"><?php echo $row['state']; ?></td>
</tr>
<tr>
<td align="left"><span style="color: <?php echo $color[$row['user_level']]; ?>"><?php echo $row['user_level']; ?></span></td>
<td align="right">"member since..."</td>
</tr>
</table>
<?php
}
}
?>
userdetails.php
<?php
include 'dbh.php';
$result = $conn->query("SELECT * FROM users WHERE id=" . $_GET["id"]);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="menu">
<span id="back">BACK</span>
<span id="delete" data-id="<?php echo $row['id']; ?>">DELETE</span>
<span id="new">NEW</span>
<span id="edit">EDIT</span>
</div>
<table class="userdetails">
<tr>
<td align="left">First Name:</td>
<td align="right"><?php echo $row['first_name']; ?></td>
</tr>
<tr>
<td align="left">Last Name:</td>
<td align="right"><?php echo $row['last_name']; ?></td>
</tr>
<tr>
<td align="left">Age:</td>
<td align="right"><?php echo $row['age']; ?></td>
<tr>
</tr>
<td align="left">Sex:</td>
<td align="right"><?php echo $row['sex']; ?></td>
</tr>
</table>
<script>
//JQ Delete
$(document).on("click","#delete", function() {
var id = $(this).attr('data-id');
$("#dialog-confirm").data('del-id', id).dialog('open').html('Delete user ' + id + '?');
});
$("#dialog-confirm").dialog({
resizable: false,
title: 'Confirm Delete',
height:150,
modal: true,
autoOpen:false,
buttons: {
'Yes': function() {
var id = $(this).data('del-id');
$.ajax({
type:'POST',
url:'deleteuser.php',
data:{id: id},
}),
$('#append').append('Deleted User: ' + id + '<br>');
$(this).dialog('close');
},
'No': function() {
$(this).dialog('close');
}
}
});
</script>
<div id="dialog-confirm" style="display:none;"></div>
<span id="append"></span>
<?php
}
}
?>
deleteuser.php
<?php
include 'dbh.php';
$sql = "DELETE FROM users WHERE id=" . $_POST["id"];
if ($conn->query($sql) === TRUE) {
echo "User has been deleted!!!";
} else {
echo "Error deleting user: " . $conn->error;
}
$conn->close();
Just looking for a little help, thank you!
Finally got this working with:
$.ajax({
type:'POST',
url:'deleteuser.php',
data:{id: id},
}),
I think that this is what you want:
$.ajax({
url: "deleteuser.php",
type: "post",
data: 'deleteuser.php?id=' + id,
cache: false,
success: function(data) {
// Close your dialogue on success
}
});
Hope this helps!

how can set multiple submit buttons for uploading

i want 14 records to be uploaded. Before uploading i want to select corresponding check button. At a time only one record can be uploaded. So all 14 records are arranged in a table with two buttons, one for choose file and other for submit. But problem is that only very first document is uploaded, others don't get uploaded. later i knew that i must use more than one form for all submission. how can it possible?
<div class="portal-body" style="min-height: 268px;">
<table class="table striped hover bordered" id="sample_editable_4">
<thead>
<tr>
<th id="eval_style">Sl No.</th>
<th id="eval_style">Document Name</th>
<th id="eval_style">select</th>
<th id="eval_style">Choose</th>
<th id="eval_style">Submit</th>
</tr>
</thead>
<tbody>
<?php $i=1;
foreach($evidence_details->result() as $row_evidence) { ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row_evidence->evidence_name; ?> </td>
<td>
<input type="checkbox" name="evidences" id="<?php echo $row_evidence->evidence_id; ?>" value="<?php echo $row_evidence->evidence_id; ?>" <?php if(in_array($row_evidence->evidence_id,$evidence)) { ?>checked="checked"<?php } ?> />
</td>
<td><input type="file" name="multiUpload" id="multiUpload" multiple /></td>
<td><button type="submit" class="btn blue" name="submitHandler" id="submitHandler" style="margin-top: 15px;margin-left: 54px;">Submit</button></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
var config = {
support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/pdf",
form: "demoFiler", // Form ID
dragArea: "dragAndDropFiles",
uploadUrl: "<?php echo base_url(); ?>home/multiple_upload"
}
$(document).ready(function()
{
initMultiUploader(config);
});
</script>
controller
function multiple_upload()
{
$application_id=$this->session->userdata('application_id');
if(!is_dir('./application/assets/uploads/'.$application_id))
{
mkdir('./application/assets/uploads/'.$application_id, 0777, TRUE);
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], "./application/assets/uploads/".$application_id."/".$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
}
**multiupload.js**
function initMultiUploader(){
new multiUploader(config);
}

jquery / javascript ajax not working when loaded into page via jquery load()

Have tried looking at answers similar but can't quite understand it.
I have tested the following code [www.mangocleaning.com/testing/pg1.php][1] and it works fine when used directly on its own page. However, when I load it via ajax load() to act as content within a current page it stops working. Can anyone help please? I copy and paste a lot and have limited understanding so please make it as simple for me to understand as you can please. Many thanks
<script>
function showEdit(editableObj){$(editableObj).css("background","#FFF");}
function saveToDatabase(editableObj,column,id) {
$(editableObj).css("background","#CCC url(loaderIcon.gif) no-repeat right");
$.ajax({
url: "pg3.php",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data){
$(editableObj).css("background","#CCC");
}
});
}
</script>
</head>
<form name='userForm' id='userForm'>
<div><input type='text' name='company_name' placeholder='Company Name' /></div>
<div><input type='text' name='contact' placeholder='Contact' /></div>
<div><input type='text' name='priority' placeholder='Priority' /></div>
<div><input type='text' name='email_to' placeholder='Email to' /></div>
<div><input type='text' name='email_from' placeholder='Email from' /></div>
<div><input type='text' name='begin_message' placeholder='Dear / Hi / Hello' /></div>
<div><input type='text' name='message_text' placeholder='Message content' /></div>
<div><input type='text' name='end_message' placeholder='Many thanks / Kind regards' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
<div class="table_outer">
<table class="table1" id="table1">
<thead>
<th>id</th>
<th>Company</th>
<th>Person</th>
<th>Email to</th>
<th>Email from</th>
<th>Begin message</th>
<th>Text</th>
<th>End message</th>
<th>Delete</th>
</thead>
<?php
require_once("pg5.php");
$db_handle = new DBController();
$sql = "SELECT * from InvoiceEmailData";
$data = $db_handle->runQuery($sql);
foreach($data as $data_variable=>$dsdjgskjghkjfh) {
?>
<tr>
<td contenteditable="false" onBlur="saveToDatabase(this,'id','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["id"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Company','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Company"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Person','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Person"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EmailTo','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EmailTo"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EmailFrom','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EmailFrom"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'BeginMessage','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["BeginMessage"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Text','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Text"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EndMessage','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EndMessage"]; ?></td>
<td> <input type="submit" id='<?php echo $data[$data_variable]["id"] ?>' value='Delete' class="delete-button" /> </td>
</tr>
<?php
}
?>
</table>
JQUERY/AJAX TO SEND AND THEN RECEIVE BACK FORM DATA TO/FROM PG2 TO ADD TO TABLE ABOVE - ALSO DELETE ROWS USING DELETE QUERY FROM PG3
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
delete_task(); // Call the delete_task function
$(document).ready(function(){
$('#userForm').submit(function(){
$.ajax({
type: 'POST',
url: 'pg2.php',
data: $(this).serialize()
})
.done(function( data ) {$(data).appendTo('#table1').hide().fadeIn(2000);delete_task();
})
.fail(function() { alert( "Posting failed." ); });
return false;
});
});
function delete_task() {
$('.delete-button').click(function(){
var current_element = $(this);
var id = $(this).attr('id');
$.post('pg4.php', { list_entry_id: id }, function() {
current_element.parent().fadeOut(300, function() { $(this).closest('tr').remove(); });
});
});
}
i added extra code at the end and it worked:
you need to add e.preventDefault(); to prevent default browser submit
and at the end of your sample code to have the javascript work
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
function showEdit(editableObj){$(editableObj).css("background","#FFF");}
function saveToDatabase(editableObj,column,id) {
$(editableObj).css("background","#CCC url(loaderIcon.gif) no-repeat right");
$.ajax({
url: "pg3.php",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data){ $(editableObj).css("background","#CCC");
}
});
}
</script>
</head>
<form name='userForm' id="userForm">
<div><input type='text' name='company_name' placeholder='Company Name' /></div>
<div><input type='text' name='contact' placeholder='Contact' /></div>
<div><input type='text' name='priority' placeholder='Priority' /></div>
<div><input type='text' name='email_to' placeholder='Email to' /></div>
<div><input type='text' name='email_from' placeholder='Email from' /></div>
<div><input type='text' name='begin_message' placeholder='Dear / Hi / Hello' /></div>
<div><input type='text' name='message_text' placeholder='Message content' /></div>
<div><input type='text' name='end_message' placeholder='Many thanks / Kind regards' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
<div class="table_outer">
<table class="table1" id="table1">
<thead>
<th>id</th>
<th>Company</th>
<th>Person</th>
<th>Email to</th>
<th>Email from</th>
<th>Begin message</th>
<th>Text</th>
<th>End message</th>
<th>Delete</th>
</thead>
<?php
require_once("pg5.php");
$db_handle = new DBController();
$sql = "SELECT * from InvoiceEmailData";
$data = $db_handle->runQuery($sql);
foreach($data as $data_variable=>$dsdjgskjghkjfh) {
?>
<tr>
<td contenteditable="false" onBlur="saveToDatabase(this,'id','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["id"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Company','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Company"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Person','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Person"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EmailTo','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EmailTo"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EmailFrom','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EmailFrom"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'BeginMessage','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["BeginMessage"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'Text','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["Text"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'EndMessage','<?php echo $data[$data_variable]["id"]; ?>')" onClick="showEdit(this);"><?php echo $data[$data_variable]["EndMessage"]; ?></td>
<td> <input type="submit" id='<?php echo $data[$data_variable]["id"] ?>' value='Delete' class="delete-button" /> </td>
</tr>
<?php
}
?>
</table>
JQUERY/AJAX TO SEND AND THEN RECEIVE BACK FORM DATA TO/FROM PG2 TO ADD TO TABLE ABOVE - ALSO DELETE ROWS USING DELETE QUERY FROM PG3
<script>
//delete_task(); // Call the delete_task function
$(document).ready(function(){
$('#userForm').submit(function(e){
e.preventDefault();
$.ajax({
method: 'POST',
url: 'pg2.php',
data: $(this).serialize()
})
.done(function( data ) {$(data).appendTo('#table1').hide().fadeIn(2000);delete_task();
})
.fail(function() { alert( "Posting failed." ); });
return false;
});
});
function delete_task() {
$('.delete-button').click(function(){
var current_element = $(this);
var id = $(this).attr('id');
$.post('pg4.php', { list_entry_id: id }, function() {
current_element.parent().fadeOut(300, function() { $(this).closest('tr').remove(); });
});
});
}
</script>
</html>

Didn't show counter number and counter bar in php voting system

here is my index.html:
<div style="margin:50px">
<img src="likebig.png" alt="Like" border="0">
<img src="dislike.png" alt="dislike" border="0">
<div id="votebox">
<span id='close'>X</span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="content"></div>
</div>
</div>
and rating.php:
include("db.php");
if($_POST['id'] > 0 && $_POST['name']> 0){
$id=mysql_escape_String($_POST['id']);
$name=mysql_escape_String($_POST['name']);
mysql_query("update messages set $name=$name+1 where id='$id'");
$result=mysql_query("select up,down from messages where id='$id'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<tr>
<td width="30px">
<img src="likeup.png">
</td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px">
<img src="likedown.png">
</td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}
and this is ajax script:
<script type="text/javascript">
$(document).ready(function() {
$(".like").click( function() {
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html){
$("#flash").fadeOut("slow");
$("#content").html(html);
}
});
});
$(".close").click(function() {
$("#votebox").slideUp("slow");
});
});
</script>
I have tried for a long time, still i can't able to fix error.
It didn't show counter number and count-bar after clicking the like or unlike button.
Now it shows like this http://s15.postimg.org/e6pvjkrej/Untitled_1.png.
I need like this: http://s15.postimg.org/qmmldbkqj/Untitled_2.png
May how can i fix this? can anyone help me to fix this ?
thanks in advance.
It was not only the "greenbar" and "redbar". The problem was also the output. You need to echo all the html so that ajax could get the html.
<?php
include("db.php");
if($_POST['id'] > 0 && $_POST['name']> 0){
$id=mysql_escape_String($_POST['id']);
$name=mysql_escape_String($_POST['name']);
mysql_query("update messages set $name='".$name+1."' where id='".$id."'");
$result=mysql_query("select up,down from messages where id='".$id."'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
//HTML Output for AJAX Calls
echo "<div style='margin-bottom:10px'>
<b>Ratings for this blog</b> (". $total ." total)
</div>
<table width='700px'>
<tr>
<td width='30px'>
<img src='likeup.png'>
</td>
<td width='60px'>". $up_value ."</td>
<td width='600px'><div id='greebar' style='width:'". $up_per ."%'></div></td>
</tr>
<tr>
<td width='30px'>
<img src='likedown.png'>
</td>
<td width='60px'>".$down_value."</td>
<td width='600px'><div id='redbar' style='width:'". $down_per ."%'></div></td>
</tr>
</table>";
}
?>
Here's the code for HTML Output from the tutorial for reference:
echo '<b>Ratings for this blog</b> ( '.$total.' total)
Like :'.$up_value.'
<div id="greebar" style="width:'.$up_per.'%"></div>
Dislike:'.$down_value.'
<div id="redbar" style="width:'.$down_per.'%"></div>';
link

Categories