Change value of opener document by jquery with data from mysql - php

I have a problem with change value of opener document by jquery with data from mysql. Mysql data is displayed in table, in every TR I have button which should change data in opener document with data from mysql but it puts everywhere 0. Where is the problem? I have a following code:
<script type="text/javascript">
$(".formbutton").click(function() {
var parent = $(this).parents('tr');
var nazwa = $('#nazwa', parent).val();
var osoba = $('#osoba', parent).val();
var nip = $('#nip', parent).val();
$("#nazwa",opener.document).val(+nazwa)
$("#adres",opener.document).val(+osoba)
$("#nip",opener.document).val(+nip)
window.opener.focus();
window.close();
});
</script>
<? echo'<table>
<thead>
<tr>
<th>Nazwa</th>
<th>Osoba</th>
<th>Nip</th>
<th></th>
</tr></thead><tbody>';
$id=$_GET['id'];
$link=mysql_query("SELECT * FROM qwerty where qwerty like '%$id%'");
while($wiersz=mysql_fetch_array($link))
{
echo'<tr>';
echo'<td id="nazwa">'.$wiersz['nazwa'].'</td>';
echo'<td id="osoba">'.$wiersz['osoba'].'</td>';
echo'<td id="nip">'.$wiersz['nip'].'</td>';
?>
<td align="center" width="260px"><form><input type="button" class="formbutton" value="PUT IN"/></form></td>
<?
}
echo'</tbody></table></div>'; ?>
Thanks in advance

Using .val() on a td doesn't work.
Use e.g. var nazwa = $('#nazwa', parent).text(); or var nazwa = $('#nazwa', parent).html();
Finally, as mentioned by PH. T. ids must be unique, so use css classes:
var nazwa= $('.nazwa', parent).text();
...
$("#nazwa",opener.document).val(+nazwa)
...
echo'<td class="nazwa">'.$wiersz['nazwa'].'</td>';
I assume that the td named nazwa contains a number and that #nazwa in parent is a unique form element.

Related

Hide table rows one by one when click them using jQuery

I have usual sql scenario with php (mysqli_fetch_assoc) and create table. I want to hide rows randomly when i click on them. I may also need to delete them from the database at the same time, because this file is calling from another with ajax every 5 sec. and hiding is not enough because after calling the rows appear again.. :) I'm no sure that this is the best solution but I'm learning now.
I'm trying something like this it's working, but i can hide only last row. I want hide every row randomly :)
jQuery().ready(function(){
$( "#trow" ).click(function() {
$( "#trow" ).hide()
});
});
<?php while ($row = mysqli_fetch_assoc($result_down)):
$datetime = $row['datetime'];
$device = $row['host'];
$message = $row['msg'];
?>
<tbody>
<tr id="trow">
<td id="device"><?php echo $datetime;?></td>
<td id="datatime"><?php echo $device;?></td>
< td id="message"><?php echo $message;?></td>
</tr>
</tbody>
<?php endwhile;?>
Try to change the trow from an id to a class name
<tbody>
<tr class="trow">
<td id="device"><?php echo $datetime;?></td>
<td id="datatime"><?php echo $device;?></td>
<td id="message"><?php echo $message;?></td>
</tr>
And in the JQuery side, call the click event on the class and not the id this time. The $(this) means the clicked element.
jQuery().ready(function(){
$( ".trow" ).click(function() {
$( this ).hide()
});
});
Simply hiding rows after a click can be done like this:
(function () {
$('table tr').click(function() {
$(this).hide();
});
})();

Passing td value to modal html php

<td value="541">123</td>
<td value="542">456</td>
$("#table td").click(function(){
var value=$(this).html();
alert(value);
});
i want to pass the td value 541 to modal
but when i click I only get the value 123
As value is not a valid attribute for a td tag, you should use data-* attributes to store custom data.
The data-* attributes gives us the ability to embed custom data attributes on all HTML elements.
See docs: https://www.w3schools.com/tags/att_global_data.asp
$("#table td").click(function(){
var value=$(this).data("value");
alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
<tr>
<td data-value="541">123</td>
<td data-value="542">456</td>
</tr>
</table>
value isn't really used on the td element. However, you can always grab its value like this:
$("#table td").click(function(){
var value=$(this).attr("value");
alert(value);
});

Edit particular field in a row of the table and update edited value in MySQL table

I am fetching data from database in a table.Now i want to edit particular field in a row of the table and save that value into MySQL.
This is my complete code for displaying data in table and editing. Here i want to edit the status. It is editable but after editing how to get the value from the table and update that value to MySQL.
<?php
include "config.php";
$sql = "select * from d_jobs where Status ='drafted' ";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
//echo $count;
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
$("document").ready(function(){
$('#sdata').click(function(){
var myTxt = $(('.status')+[i]).html();
console.log(myTxt);
var id = $(('.status')+[i]).attr('id');
console.log(id);
}
/* $.ajax({
type: 'post',
url: 'job_field_edit.php',
data: 'varname=' +myTxt
}); */
});
});
</script>
</head>
<body>
<table border="2" align="center">
<thead>
<tr>
<th>Job Name</th>
<th>Builder</th>
<th>Job Type</th>
<th>Status</th>
<th>Effective Date Start</th>
<th>Estimated completion date</th>
<th>Job Gal Glag</th>
<th>Take List Flag</th>
<th>Planner</th>
<th>Project Manager</th>
<th>Hand Over</th>
<th>Other Comments</th>
</tr>
</thead>
<tbody>
<?php
if( $count ==0 ){
echo '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
while( $row = mysqli_fetch_array($result)){
echo "<tr><td>{$row['JOB_NAME']}</td>
<td>{$row['BUILDER']}</td>
<td>{$row['JOB_TYPE']}</td>
<td contenteditable='true' id='{$row['JOB_ID']}' class='status[{$row['JOB_ID']}]'>{$row['status']}</td>
<td>{$row['EFFECTIVE_START_DATE']}</td>
<td>{$row['ESTIMATED_COMPLETION_DATE']}</td>
<td>{$row['JOB_GAL_FLAG']}</td>
<td>{$row['JOB_TAKE_LIST_FLAG']}</td>
<td>{$row['Planner']}</td>
<td>{$row['Project_Manager']}</td>
<td>{$row['Handover']}</td>
<td>{$row['Comments']}</td>
<td><input name='need_delete[{$row['JOB_ID']}]' type='checkbox' id='checkbox[{$row['JOB_ID']}]' value='{$row['JOB_ID']}'></td>
</tr>\n";
}
}
?>
</tbody>
</table>
<input id="sdata" type="button" value="Send Data" />
</body>
</html>
<?php
mysqli_close($conn);
?>
There's a number of ways to approach this. One is to dispense with the manual Send Data button and allow javascript to respond automatically to users' contenteditable edits.
Unfortunately, contenteditable elements don't fire a change event but they do fire a blur event, from which a change event can be triggered.
First, build your contenteditable elements like this :
<td contenteditable=\"true\" class=\"status\" data-job-id=\"{$row['JOB_ID']}\">{$row['status']}</td>
Then, for each contenteditable element, attach a blur handler, and initialize a data-value attribute equal to innerText.
$('[contenteditable]').on('blur', function(e) {
var self = $(this);
if(self.text() !== self.data('value')) {
self.trigger('change');
}
self.data('value', self.text());
}).each(function() {
$(this).data('value', $(this).text()); // equivaluent to data-value="{$row['status']}".
});
Thus you can have contenteditable elements that mimic, at least in part, HTML input elements.
So now you can attach a change handler as you would for a standard HTML input element.
$(".status").on('change', function(e) {
var job_id = $(this).data('jobId');
var old value = $(this).data('value');
var current value = $(this).text(); // in this regard, <input> is not mimicked. For a genuine <input> we would write `$(this).val()`.
// here, perform ajax to keep server-side up to date.
});
DEMO
Note: This approach is slightly over-the-top when [contenteditable] and .status are virtual synonyms for each other. However, in the general case where there might be several different classes of contenteditable element, then you would likely avoid much repetition of code.

jQuery toggle divs and hide divs

I need some help, i have a forum page and i cant get the toggle part to work
Here is the code:
<script type="text/javascript">
$(document).ready(function() {
$(".button").click(function(event) {
$("#div").hide();
var tmp = ($("#div" + $(this).attr("target")).is(":visible") ? false : true)
if (tmp) $("#div" + $(this).attr("target")).toggle();
});
});
</script>
<h2>Forum</h2>
<table cellspacing="0" cellpadding="0" width="100%" class="table">
<tr style="background: #f2f2f2;"><td><strong>Name</strong></td><td></td></tr>
<tr style="display:none;" id="divDynamicID"><td> NAME</td><td align="right" width="40"><img src="../img/pencil.png"> <img src="../img/delete.png"></td></tr>';
<tr class="Test"><td> Name</td><td align="right" width="40"><img src="../img/pencil.png" class="button" target="dynamicID"> <img src="../img/delete.png"></td></tr>';
When you click the .button i want it to hide all open divs and just open that one you click at, but at this time i only get the show part to work not the not show part.
In your jQuery code I found code with #div, but could not find div with id div in HTML code. # will only be used with id of the HTML control
Whereas div is the selector used for HTML controls
I'm not very sure about your question. If I've understood properly I'd say two things. First, you can have a local variable for the tr id which you want to display. I guess the id is the value of the target attribute for button class with "div" prefixed. And secondly, you can have a class for each of the rows so that you can hide them all when needed.
echo '<tr style="display:none;" id="div'.$obj->boardID.'"><td> '.$obj->name.'</td><td align="right" width="40"><img src="../img/pencil.png"> <img src="../img/delete.png"></td></tr>';
echo '<tr class="Test"><td> '.$obj->name.'</td><td align="right" width="40"><img src="../img/pencil.png" class="button" target="'.$obj->boardID.'"> <img src="../img/delete.png"></td></tr>';
In the abodes, I put a class trClass. You can change it to whatever you feel contextual to your code.
$(".button").click(function(event) {
$('.trClass').hide();
var trId="div"+$(this).attr("target");
var tmp = $("#" + trId).is(":visible");
if (tmp) $("#" + trId).show();
});

How to remove a row from a table

I have an html table, and on each row, i would like to add a delete button.
When the user clicks on it, I would like to do two things:
1) remove the row from the table.
2) extract data from the row that was removed - a particular cell, and then make an ajax call using this value to a function that will remove the record from the database. The function returns a fresh list of items, which i will then use to redisplay the same table.
I found the following post from someone else on stackoverflow:
How to remove current row from table in jQuery?
So I guess that addresses point number one. But I don't know how to modify the code in the selected answer of the post to allow for grabbing the id value in the row, and then making an ajax call.
<table class="table table-bordered table-striped" id="databaserecords">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
<th>Voice</th>
<th>Jumbo</th>
<th>Mode</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($dblist as $record): ?>
<tr>
<td class ='recordId'><?php echo $record['Id'] ?></td>
<td class ='recordName'><?php echo $record['Name'] ?></td>
<td><?php echo $record['Status'] ?></td>
<td><?php echo $record['Voice'] ?></td>
<td><?php echo $record['Jumbo'] ?></td>
<td class='recordmode'><?php echo $record['Mode'] ?></td>
<td><button id="deleteRecord">Delete Record</button></td>
</tr>
<?php endforeach ?>
<script>
$('#deleteRecord').live('click', function() {
//get a count of all records. only allowed to delete if you have more than one record.
var recCount = $('#databaserecords tbody tr').length;
if (recCount > 1)
{
$thevalueIwanttoSave = $(this).closest('recordId').val();
alert($thevalueIwanttoSave);
}
});
</script>
Right now, the alert always says that my variable is undefined.
Can you point me in the right direction? I do know how to code an ajax call... I think I just need help with grabbing the value from the table.
Thanks.
You selector is not correct.
$thevalueIwanttoSave = $(this).parent().siblings('.recordId').text();
closest selects the closest parent of the element(recordId is not parent/grandparent or.. of your button element) and you have also missed a . for class selector. val is used for getting/setting values of form elements, for td elements you should use text or html method. Also note that IDs must be unique(you can use classes instead) and live method is deprecated, you can use on method.
$('#databaserecords').on('click', '.deleteRecord', function() {
var recCount = $('#databaserecords tbody tr').length;
if (recCount > 1) {
var text = $(this).parent().siblings('.recordId').text();
alert(text);
// $(this).closest('tr').remove()
}
});​
siblings()
parent()
closest()
text()
on()
$thevalueIwanttoSave = $(this).parent('tr').find('.recordId').text();
it should be .recordId, not recordId
$thevalueIwanttoSave = $(this).closest('.recordId').val();
alert($thevalueIwanttoSave);
Add the ID in an attribute on your delete button and you can just get it via jQuery.
in your php loop:
<button class="deleteRecord" recordID="<?php echo $record['Id'] ?>">Delete Record</button>
in JQuery on
$(".deleteRecord").click(function() {
var id = $(this).attr("recordID");
...
});
You need to use a . to select the element with class recordId. Also use text() to get the value:
$thevalueIwanttoSave = $(this).siblings('.recordId').text();
alert($thevalueIwanttoSave);

Categories