So I'm trying to use a table to update some records in my database but each time I click on update it won't work and it won't do anything. A part of the code below was found in an another topic but it was incomplete so I added some other things.
Js script
$(function(){
$("#loading").hide();
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('update.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
This is the table fetching the rows from the database, however everything works besides updating.
HTML & PHP
<form method="post" action="update.php">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table table-striped table-dark">
<tr bgcolor="#df4662" style="color:#FFFFFF;">
<td>ID</td>
<td>Nickname</td>
<td>Name</td>
<td>Rank</td>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td contenteditable="true" id="id:<?php echo $row["id"]; ?>"><?php echo $row["id"]; ?></td>
<td contenteditable="true" id="username:<?php echo $row["username"]; ?>"><?php echo $row["username"]; ?></td>
<td contenteditable="true" id="name:<?php echo $row["steamid"]; ?>"><?php echo $row["steamid"]; ?></td>
<td contenteditable="true" id="ranks:<?php echo $row["ranks"]; ?>"><?php echo $row["ranks"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
After a few errors I've been able to have a clean error_logs, but now I don't get any error even after pressing the update button.
update.php
<?php
include '../database.php'
?>
<?php
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_id = strip_tags(trim($field_name));
$split_data = explode(':', $field_id);
$id = $split_data[1];
$field_name = $split_data[0];
if(!empty($id) && !empty($field_name) && !empty($val))
{
$affected_rows = mysqli_query($mysqli,"UPDATE users SET $field_name = '$val' WHERE id = $id");
echo $affected_rows;
echo "Updated";
} else {
echo "Invalid Request";
}
}
}
else {
echo "Invalid Requests";
}
?>
EDIT: Thanking Sam now the problem is just that the record won't update at all
Related
I am calling Api : $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order=' . $orderrecords[$k]["order_id"] . '&username=admin&password=admin123'; and fetching Status results of all Order IDS & displaying in php page when we refresh php page.
Now i want to select Order IDs through checkbox, than when i click on button "Show Status" , than only i want to Call Api & update the Selected Order IDs status in web page.
<p><button type= "button" class="call">Show Status</button></p>
<table class="tbl-qa" border="1">
<thead>
<tr>
<th class="table-header"></th>
<th class="table-header">ORDERID</th>
<th class="table-header">Status</th>
</tr>
</thead>
<tbody id="table-body">
<?php
$tabindex = 1;
if (!empty($orderrecords))
{
foreach($orderrecords as $k => $v)
{ ?>
<?php
$hide = '';
$data['username'] = 'admin';
$data['password'] = 'admin123';
$url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order=' . $orderrecords[$k]["order_id"] . '&username=admin&password=admin123';
$ch = curl_init();
// some curl code
$res = explode("\n", $output);
if (!isset($res[13]))
{
$res[13] = null;
}
$status = $res[13];
?>
<tr class="table-row" id="table-row-<?php echo $orderrecords[$k]["id"]; ?>" tabindex="<?php echo $tabindex; ?>">
<td><input onclick="assignorderids('<?php echo $orderrecords[$k]["order_id"]; ?>')" type="checkbox" name="assigneeid" id="assigneeid-<?php echo $orderrecords[$k]["order_id"]; ?>" value="<?php echo $orderrecords[$k]["order_id"]; ?>"></td>
<td><?php echo $orderrecords[$k]["order_id"]; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php
$tabindex++;
}
} ?>
</tbody>
</table>
Please help me how i can achieve this ?
Update : assignorderids function
function assignorderids(oid)
{
var checkstatus=$("#assigneeid-"+oid).is(":checked");
var morderId =document.getElementById("orderids").value;
if(checkstatus==false)
{
var arrayorder = JSON.parse("[" + morderId + "]");
document.getElementById("orderids").value='';
for (var i = 0; i < arrayorder.length; i++) {
var orderstatusValue=arrayorder[i];
if(orderstatusValue!=oid){
if (document.getElementById("orderids").value=='')
{
document.getElementById("orderids").value=orderstatusValue;
}
else
{
var newvalue=document.getElementById("orderids").value;
document.getElementById("orderids").value=newvalue+","+orderstatusValue;
}
}
}
}
else
{
if(morderId=='')
{
document.getElementById("orderids").value=oid;
}
else
{
document.getElementById("orderids").value=morderId+","+oid;
}
}
}
Url Output
I want to create delete feature using function and jquery
My jquery works and show messages but nothing happen "Nothing Deleted"
Jquery Code
<script type="text/javascript">
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to remove this record?'))
{
$.ajax({
url: 'delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record removed successfully");
}
});
}
});
PHP Function Code
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
Delete.php Code
include ('function.php');
$id = $_GET['id'];
$table = 'msg';
delete($table,$id);
HTML Code
<table class="table table-striped" style="background-color: #ffffff;">
<tr>
<th>ID</th>
<th>From</th>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
$i = '1';
$username = $user_data['username'];
$query = "SELECT * FROM msg WHERE `go_to` = '$username' Order by id";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['come_from']; ?></td>
<td>
<a href="read_message/<?php echo $row['id']; ?>"><?php if(count_msg_not_opened($username, $row['id']) > '0')
{
echo $row['title'];
}
else
{
echo '<b>' . $row['title'] . '</b>';
} ?></a></td>
<td><?php echo $row['date']; ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
</td>
</tr>
<?php } ?>
</table>
I also include "jquery.min.js"
When I press "Delete" bottom this message appears "Are you sure to remove this record?"
I pressed "Yes" then this message appears "Record removed successfully", but nothing was deleted.
I don't know where the problem is.
You forgot to add the id attribute to the <tr>
<tr id="<?php echo $row['id']; ?>">
You should also add error checking and prepared statements to your PHP code.
Are you sure that you have connected your PHP-code to your SQL-database?
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
The code above is relying on a connection already existing within your PHP-file. See this to find out how to apply a connection.
Well, I think it might be easier to explain my question by the image below:
As can be seen in the picture, if user select "By title", a textbox will be appeared where user can write a movie title (I also used jQuery auto-completion for this textbox). Then, if user click on the button "Movies by this title", a new window will be shown where there is a list of movies containing the term in the textbox.
My question:
I would like to integrate a small image of each of these movies beside them (and maybe some other information like movie year, genre..) like what amazon does (Please see here). I used renderitem for the auto-complete part and it works fine, but actually I have no idea how to do the same in the new window.. I would be very grateful if someone can help me.
This is my code:
<div id="m_scents" class="field">
<label style="margin-bottom:10px;" for="m_scnts"></label>
<input class="autofill4" type="textbox" name= "q27[]" id="q" placeholder="Enter movie titles here" />
<input type="button" value="Movies by this title" id="btnMove" style="display:none;"/>
</div>
<script type="text/javascript">
var selected;
$(document).ready(function () {
$("input[id='selectType']").change(function(){
if ($(this).val() == "byTitle") {
$("#m_scents2").hide();
$("#btnMove").show();
$("#m_scents").show();
$("#q").focus();
$("#q").autocomplete({
minLength: 0,
delay:5,
source: "query.php",
focus: function( event, ui ){
event.preventDefault();
return false;
},
select: function( event, ui ) {
window.selected = ui.item.movieName;
return false;
}
}).data("uiAutocomplete")._renderItem = function( ul, item ) {
return $("<li></li>")
.data( "item.autocomplete", item )
.append( "<a>" + (item.posterLink?"<img class='imdbImage' src='imdbImage.php?url=" + item.posterLink + "' />":"") + "<span class='imdbTitle'>" + item.movieName + "</span>" + "<div class='clear'></div></a>" )
.appendTo( ul );
};
}
});
$('#btnMove').on('click', function (e) {
popupCenter("movieBytitle.php","_blank","400","400");
});
</script>
This is movieBytitle.php:
<body>
<div id= "field"
</div>
<script type="text/javascript">
var selected = parent.window.opener.selected;
$.ajax({
url: 'childfilm.php',
datatype: "json",
data:{p:selected},
success: function(response) {
$("#field").html(response);
}
});
</script>
</body>
and this is childfilm.php:
<?php
if(isset($_GET['p']) && !empty($_GET['p'])){
try{
include('imdbConnection.php');
$sql = $conn->prepare("SELECT DISTINCT movieName FROM films WHERE movieName LIKE :p");
$sql->execute(array(':p' => '%'.$_GET['p'].'%'));
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$option = '' . $row['movieName'] . '<br />';
$html .= $option;
}
} catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
echo $html;
exit;
}
?>
UPDATE:
This is the new childfilm.php (Thanks to #ghost help):
if(isset($_GET['p']) && !empty($_GET['p'])){
include('imdbConnection.php');
$sql = $conn->prepare("SELECT DISTINCT movieName FROM films WHERE movieName LIKE :p");
$sql->execute(array(':p' => '%'.$_GET['p'].'%'));
}
?>
<table>
<tr>
<th></th>
<th>Title</th>
<th>Year</th>
<th>Genre</th>
</tr>
<?php while($row = $sql->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><img class='imdbImage' src='imdbImage.php?url="<?php echo $row['posterLink'];?>'</td>
<td><?php echo $row['movieName']; ?></td>
</tr>
<?php endwhile; ?>
</table>
and this is imdbImage.php:
<?php
header("Content-type: image/jpeg");
$url = rawurldecode($_REQUEST['url']);
echo file_get_contents($url);
?>
New problem:
This is the result (Still, the image is not shown properly):
If you already got those information in the table, then just include it in the fetching and present it in tabular form:
<?php
if(isset($_GET['p']) && !empty($_GET['p'])){
include('imdbConnection.php');
$sql = $conn->prepare("SELECT DISTINCT movieName FROM films WHERE movieName LIKE :p");
$sql->execute(array(':p' => '%'.$_GET['p'].'%'));
}
?>
<table>
<tr>
<th></th>
<th>Title</th>
<th>Year</th>
<th>Genre</th>
</tr>
<?php while($row = $sql->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><img src="path/to/images/<?php echo $row['filename']; ?>" alt="" /></td>
<td><?php echo $row['movieName']; ?></td>
<td><?php echo $row['year']; ?></td>
<td><?php echo $row['genre']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I've got the following table on a webpage:
<table class="table table-striped table-condensed">
<thead>
<tr>
<center><th>Name</th><th>Rank</th><th>MOS</th><th>Tag</th><th>MOSTs</th><th>Prom. Date</th><th>DI</th><th>CMO</th><th>MCRC</th><th>Medals</th><th>Leave</th></center>
</tr>
</thead>
<tbody>
<?php
$rank = 'O-10';
$result->execute();
while($row = $result->fetch()) {
?>
<tr>
<td id="habbo_name:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['habbo_name']);?></td>
<td id="rank:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rank']);?></td>
<td id="rating:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rating']);?></td>
<td id="tag:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['tag']);?></td>
<td id="asts:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['asts']);?></td>
<td id="promotion_date:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['promotion_date']);?></td>
<td id="rdc_grade:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rdc_grade']);?></td>
<td id="cnl_trainings:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['cnl_trainings']);?></td>
<td id="mcrc:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['mcrc']);?></td>
<td id="medals:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['medals']);?></td>
<td id="leave:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['leave']);?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I have this code which sends a request to ajax.php whenever a td is clicked:
<script type="text/javascript">
$(function(){
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = '$(this).text()' ;
$.post('ajax.php' , field_userid + "=" + value, function(data){
});
});
});
</script>
ajax.php:
<?php
include 'functions/user.php';
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_userid = strip_tags(trim($field_name));
$split_data = explode(':', $field_userid);
$user_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($user_id) && !empty($field_name))
{
$query = "UPDATE `personnel` SET $field_name = '$val' WHERE user_id = $user_id";
$result = $con->prepare($query);
$result->execute();
} else {
echo "Invalid Requests";
}
}
} else {
echo "Invalid Requests";
}
?>
The problem I have is with 3 of the fields - MCRC, Medals and Leave - the others all update fine.
The MCRC field will either be empty or contain the + symbol. Using firebug, this is the POST request when trying to send a +:
As you can see, the + is removed in the parameters. The same goes for medals.
Leave seems to have a different issue. The parameters are fine but the database is not updated with the [TEST]
If anyone could explain a way for me to send symbols/see what's going wrong with leave that would be great. Thanks.
Got it working by changing
var value = $(this).text();
to
var value = encodeURIComponent($(this).text());
I have an HTML which displays data from a database table. There'a checkbox field, which stores id of the user. And when I click on a row, that ID has to be passed to another page using a JavaScript function. But I have encountered a few errors. I don't know where I went wrong.
This is my Javascript function. It is used to make the table rows clickable. SO when I click on a row, the corresponding ID is passed to editgroup.php page.
<script>
$('table.table tbody tr').click(function(event)
{
if(event.target.type === 'checkbox')
{
event.preventdefault()
}
else
{
window.location='edituser.php?val=<?php echo $id; ?>';
}
});
</script>
Below given is my table:
<table id="edituser" class="clickable"
<thead>
<th></th>
<th>UserID</th>
<th>Username</th>
</thead>
<tbody>
<?php
for($i=0; $i<$x; $i++)
{
$id= $val_array[$i]['id'];
$userName= $val_array[$i]['userName'];
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $id; ?>"></td>
<td><?php echo $id; ?></td>
<td><?php echo $userName; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
you have to make some change
First in your tr add id like this
<?php
for($i=0; $i<$x; $i++)
{
$id= $val_array[$i]['id']; //data from database
$userName= $val_array[$i]['userName']; //data from database
?>
<tr id="<?php echo $id;?>"> <!-- add id here -->
<td ><input type="checkbox" value="<?php echo $id; ?>"></td>
<td><?php echo $id; ?></td>
<td><?php echo $userName; ?></td>
</tr>
<?php } ?>
now your JS
<script>
$('table.clickable tbody tr').click(function(event){
if(event.target.type === 'checkbox'){
event.preventdefault()
}else{
var val = $(this).attr('id');
window.location='edituser.php?val='+val;
}
}); </script>
Your Javascript will be
$('table.table tbody tr').click(function(event){
if(event.target.type === 'checkbox'){
event.preventdefault()
}else{
var value = (this).find("input[type='checkbox']").val(); /* Get value from clicked tr Checkbox */
window.location.replace('edituser.php?val='+value); /* Pass the value */
}
});
Create new function with dynamic id for your table row
<tr onclick="edit_user(<?php echo $id;?>)" >
....
</tr>
Then you can find your row of where you have to click
<script>
function edit_user(user_id) {
if(user_id != '') {
window.location='edituser.php?val='+user_id;
}
}
</script>
Or else
This is more even simple your code.
<input type="checkbox" value="<?php echo $id; ?>" onclick="document.location.href='edituser.php?val=<?php echo $id; ?>'">