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);
Related
I am looking to insert rows to the table but
The rows are echoed but not displayed inside the table.
The HTML code of the project is:
<div class="container">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>SN</th>
<th>Category</th>
<th>Parent</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody id="displayCategory">
</tbody>
</table>
</div>
The main.js file of the project is:
It fetches data from the PHP file below:
$(document).ready(function () {
manageCategory();
function manageCategory(){
$.ajax({
url: DOMAIN + "/includes/process.php",
method: "POST",
data: { manageCategory: 1 },
success: function (data) {
$("#displayCategory").html(data);
alert(data);
console.log(data);
}
})
};
}
PHP code of the project is:
<?PHP
if (isset($_POST["manageCategory"])) {
$m = new Manage();
$result = $m->manageTableWithPagination('pdcts_categories', 1);
$rows = $result['rows'];
$pagination = $result['pagination'];
if (count($rows) > 0) {
foreach ($rows as $row) {
$n = 0;
?>
<tr>
<td><?php echo ++$n; ?></td>
<td><?php echo $row["Category"]; ?></td>
<td><?php echo $row["Parent"]; ?></td>
<td>
Active
</td>
<td>
Edit
Delete
</td>
</tr>
<?php
}?>
<!-- <tr><td colspan="5"><?php echo $pagination; ?></td></tr> -->
<?php exit(); }
}?>
Try appending data (insted of html) like so:
$("#displayCategory").append(data);
note: check in the console that your php code is returning valid html (<tr> data)
How can I hide a particular <tr> based on php variable associated with that <tr>(incremental variable $i) with jQuery when the table is being populated dynamically through Mysql. I have looked around but not getting any clue.
<table id="example">
<thead>
<tr>
<th>S.No.</th>
<th>Item Number</th>
<th>Question</th>
<th>Option-A</th>
<th>Option-B</th>
<th>Option-C</th>
<th>Option-D</th>
<th style="text-align: center;">Correct Ans.</th>
<th style="text-align: center;">Marks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($query != '') {
$res = mysql_query($query) ;
$i = 1;
while($row = mysql_fetch_array($res))
{
extract($row);
?>
<tr class="record">
<td><?php echo $i ; ?></td>
<td><?php echo $item_no ; ?></td>
<td><?php echo $level_id ; ?></td>
<td><textarea disabled name="question" rows="4" cols="35"><?php echo $question ; ?></textarea></td>
<td><?php echo $option_A ; ?></td>
<td><?php echo $option_B ; ?></td>
<td><?php echo $option_C ; ?></td>
<td><?php echo $option_D ; ?></td>
<td><?php echo $correct_ans ; ?></td>
<td><?php echo $marks ; ?></td>
<td> </td>
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
var $tr = $(this).closest('tr');
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type : "POST",
url : "delete_entry.php", //URL to the delete php script
data: info,
success : function(response) {
if(response=='deletion success'){
$tr.find('td').fadeOut(1000,function(){ $tr.remove(); });
}
}
});
}
return false;
});
});
</script>
</tr>
<?php $i++; } }
</tbody>
</table>
And my ajax page,
<?php
header('Content-Type: application/json');
session_start();
require("../config.php");
require("../Database.class.php");
require("../site.php");
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$fnc=new site_functions($db);
$id = $_POST['id'];
$deleted_date = date("Y-m-d h:i:s");
$deleted_by = $_SESSION['session_admin_id'] ;
$nots = $db->idToField("tbl_ques","notes",$id);
if ($nots == "")
{
$date_string = "last deleted on|".$deleted_date."|" ;
}
else {
$date_string = $nots."last deleted on|".$deleted_date."|" ;
}
$fnc->update_is_not_deleted_for_Pearsonvue("tbl_ques",$id, "$deleted_date", $deleted_by);
$notes_data = array("notes"=>$date_string);
if($db->query_update("tbl_ques", $notes_data, "id=$id")){
http_response_code();
echo json_encode('deletion success');
}else{
http_response_code(204);
}
?>
You are already close - in fact it looks like your code should work on the first click at least. Anyway,
Change line
$tr.find('td').fadeOut(1000,function(){ $tr.remove(); });
to
$('#' + del_id).closest('tr').remove(); });
What this does is find the delete button element in the DOM, then look 'up' the DOM structure to the first TR element, and remove that from the DOM.
It is generally better to rely on simple variables within async callbacks because relying on objects, such as $this or in your case $tr can cause issues where the object pointed to by the $tr variable is not the one you expected.
EDIT: Added the working snippet below to illustrate the technique. If you still have a problem please create a minimal verifiable version as a snippet from your code so that we can pinpoint the issue.
$('.del').on('click', function() {
$(this).closest('tr').remove();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="example">
<thead>
<tr>
<th>First name</th>
<th>Second name</th>
<th>Age</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Joe</td>
<td>Bloggs</td>
<td>22</td>
<td style="text-align: center;">
<button id='A1' class='del'>Delete</button>
</td>
</tr>
<tr>
<td>Jane</td>
<td>Bloggs</td>
<td>19</td>
<td style="text-align: center;">
<button id='A2' class='del'>Delete</button>
</td>
</tr>
<tr>
<td>Joanne</td>
<td>Bloggs</td>
<td>28</td>
<td style="text-align: center;">
<button id='A3' class='del'>Delete</button>
</td>
</tr>
</tbody>
</table>
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!
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