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
Related
I have <table> that display data. I have a button if I click, the data will be displayed in #datacontainer. But the problem is "error: expects parameter 1" I don't know what that's mean. Please help me out of this problem
Here's my code
HTML - here's the table, imagine that I have 4 data in my table then when I clicked the button, the #datacontainer will show with the data.
<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><input type="hidden" 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>
</tr>
<?php } ?>
</table>
<div id="datacontainer"></div>
and here's my script
<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()
{
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);
$("#datacontainer").slideToggle(300);
}
});
return false;
}
and here's my PHP that what I'm saying when I clicked the button. The error shows that "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result" please help me out of this problem. I really don't know what that's problem. Thankyou
<?php
$db = mysqli_connect('localhost', 'root', '', 'psbr');
if (isset($_POST['id'])) {
$id = $_POST['id'];
$accountsdata = mysqli_query($db, "SELECT * FROM accounts where id=:id");
$statement = $db->prepare($accountsdata);
$statement->bindParam(":id", $id);
$statement->execute();
$data = $statement->fetchAll(PDO::FETCH_ARRAY);
}
?>
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
you are mixing mysqli and pdo see here:
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
you need to change:
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
With:
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php foreach ($data as $val) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
And you are passing wrong dataString in your ajax code.
Change:
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);
$("#datacontainer").slideToggle(300);
With
var id = document.getElementById('text_id').value;
$.ajax({
type: "post",
url: "showdata.php",
data:{'id':id},
cache:false,
success: function(data){
$("#datacontainer").html(data);
$("#datacontainer").slideToggle(300);
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>
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!
I have a table with some checkboxes in every row. There is a select element in the table footer with several options. When the user chooses an option, i want to pass the value of any checked checkboxes and the selected option to a php script.
This is the form:
<form name="doall" method="POST" action="action.php">
<table>
<tbody>
<?php for($j=0;$j<count($userToRead);$j++){ ?>
<tr>
<td>
<input type="checkbox" id="selection_<?php echo $j ?>" name="objectSelected[<?php echo $userToRead[$j]['id'] ?>]" value="1"/>
</td>
<td align="center">
<?php echo $userToRead[$j]['id'] ?>
</td>
<td align="center" style="text-align:center;padding-left:15px;background-color:<?php echo $SListsStatus[intval($userToRead[$j]['userstatus'])][1]; ?>;" >
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['user']) ?>
</td>
<td>
<?php echo stripaccenti($SUserType[$userToRead[$j]['type']]['name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['first_name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['last_name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['title']) ?>
</td>
<td class="actBtns">
<a href="#" title="Update" class="tipS">
<img src="images/icons/edit.png" alt="" />
</a>
<a href="#" title="Remove" class="tipS">
<img src="images/icons/remove.png" alt="" />
</a>
</td>
</tr>
<?php } // end for ?>
</tbody>
<tfoot>
<tr>
<td colspan="9">
<div class="itemActions">
<label>Con tutte le selezionate:</label>
<select name="whatDoAll">
<option value="1">Attiva</option>
<option value="2">Disattiva</option>
</select>
<input type="submit" value="Esegui" class="button redB" style="margin: 5px;"/>
</div>
</td>
</tr>
</tfoot>
</table>
</form>
So in every line of my table I have a checkbox where the value is the ID of the db object that I need to modify with my php script.
This is my script:
$('input[type="submit"]').click(function() {
var output = '';
$('input[type="objectSelected"]:checked').each(function(index) {
output += $(this).val() + ", ";
});
alert(output + "is checked!");
});
In the php file i try to retrieve the variable with $_POST['objectSelected'] but the variable is always empty. Also, the script alert is empty.
The tables are create with jquery: if I use a pure html table with pure html checkboxes everything works!
You are doing wrong. You are specifying input type then the code should be:
<script type="text/javascript">
$('input[type="submit"]').click(function() {
var output = '';
$('input[type="checkbox"]:checked').each(function(index) {
output += $(this).val() + ", ";
});
alert(output + "is checked!");
});
</script>
Hope this helps you
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.