value won't display by each row - php

I have a problem.
an array would return me some dynamically data from it.
<?php
foreach($ItemArray as $key => $value) {
echo '
<tr><td height="30" valign="middle">'.$value['nr'].'</td>
<td valign="middle">'.$value['product'].'</td>
<td valign="middle">'.$value['describe'].'</td>
<td valign="middle" id="price_'.$key.'">'.$value['price'].'</td>
<td valign="middle" class="box_darker" id="amount_'.$key.'">
<input name="Field_Amount_'.$key.'" id="Field_Amount_'.$key.'" class="Field_Amount" type="text" /></td>
<td valign="middle" id="price_'.$key.'">$key</td></tr>'; }
;?>
Now I would test if the value returns me the right one when click on belong field (td/price)
$(document).on('click', '[id^=price_]', function(){
var Amount = $('[id^=Field_Amount_]').val();
alert(Amount);
});
But nevermind wich field (td/price) in each row I click, it alert me only the value from first row! maybe because the data loads dynamically?
Sorry for that poor English.

That's because you perform your second search on the global scope as well, and val() will return the value from the first element. Use $(this).parent().find() instead:
$(document).on('click', '[id^=price_]', function(){
var Amount = $(this).parent().find('[id^=Field_Amount_]').val();
alert(Amount);
});
Alternately, you should also be able to use .closest():
$(document).on('click', '[id^=price_]', function(){
var Amount = $(this).closest('[id^=Field_Amount_]').val();
alert(Amount);
});

Your trigger event is working fine since every row reacts to the click.
What's "wrong" is the selector for the Field_amount. This always selects the first Field_Amount_ because the selector is working on the whole page.
Try using the .closest() function:
$(document).on('click', '[id^=price_]', function(){
var Amount = $(this).closest('[id^=Field_Amount_]').val();
alert(Amount);
});

Try This
$(document).on('click', '[id^=price_]', function(){
var key = $(this).attr('id').split('price_');
var amount = $("#Field_Amount_"+key[1]).val();
});

Related

showing php hidden rows when passed to modal

I have an HTML table that has rows retrieved from database.
using tbody tag I hid some rows to show them in a modal and do some editing if necessary.
the modal works perfectly with editing in database and all but the data that supposed to be displayed for editing are hidden as well!
how to fix this?
here is my table with hidden rows code which are inside a while statement:
<tbody style="display:none">
<td data-target="FLOOR"><?php echo $row['FLOOR'];?></td>
<td data-target="ORDER_OFFICE"><?php echo $row['ORDER_OFFICE'];?></td>
<td data-target="SERIAL_NO"><?php echo $row['SERIAL_NO'];?></td>
<td data-target="POINT_NO"><?php echo $row['POINT_NO'];?></td>
<td data-target="NOTE"><?php echo $row['NOTE'];?></td>
<td data-target="OTHER_NAME"><?php echo $row['OTHER_NAME'];?></td>
<td data-target="ORDER_TELE"><?php echo $row['ORDER_TELE'];?></td>
</tbody>
<script>
// append values in input fields
$(document).on('click','a[data-role=update]',function(){
var id = $(this).data('id');
var FLOOR = $('#'+id).children('td[data-target=FLOOR]').text();
var ORDER_OFFICE = $('#'+id).children('td[data-target=ORDER_OFFICE]').text();
var SERIAL_NO = $('#'+id).children('td[data-target=SERIAL_NO]').text();
var POINT_NO = $('#'+id).children('td[data-target=POINT_NO]').text();
var NOTE = $('#'+id).children('td[data-target=NOTE]').text();
var OTHER_NAME = $('#'+id).children('td[data-target=OTHER_NAME]').text();
var ORDER_TELE = $('#'+id).children('td[data-target=ORDER_TELE]').text();
$('#FLOOR').val(FLOOR);
$('#ORDER_OFFICE').val(ORDER_OFFICE);
$('#SERIAL_NO').val(SERIAL_NO);
$('#POINT_NO').val(POINT_NO);
$('#NOTE').val(NOTE);
$('#OTHER_NAME').val(OTHER_NAME);
$('#ORDER_TELE').val(ORDER_TELE);
$('#userId').val(id);
$('#myModal3').modal({backdrop: "static"});
});
// now create event to get data from fields and update in database
$('#save').click(function(){
var ID = $('#userId').val();
var FLOOR = $('#FLOOR').val();
var ORDER_OFFICE = $('#ORDER_OFFICE').val();
var SERIAL_NO = $('#SERIAL_NO').val();
var POINT_NO = $('#POINT_NO').val();
var NOTE = $('#NOTE').val();
var OTHER_NAME = $('#OTHER_NAME').val();
var ORDER_TELE = $('#ORDER_TELE').val();
$.ajax({
url : 'edit_order.php',
method : 'post',
data : {FLOOR : FLOOR, ORDER_OFFICE : ORDER_OFFICE, SERIAL_NO : SERIAL_NO, POINT_NO : POINT_NO
,NOTE : NOTE, OTHER_NAME : OTHER_NAME, ORDER_TELE: ORDER_TELE, ID: ID},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=FLOOR]').text(FLOOR);
$('#'+id).children('td[data-target=ORDER_OFFICE]').text(ORDER_OFFICE);
$('#'+id).children('td[data-target=SERIAL_NO]').text(SERIAL_NO);
$('#'+id).children('td[data-target=POINT_NO]').text(POINT_NO);
$('#'+id).children('td[data-target=NOTE]').text(NOTE);
$('#'+id).children('td[data-target=OTHER_NAME]').text(OTHER_NAME);
$('#'+id).children('td[data-target=ORDER_TELE]').text(ORDER_TELE);
$('#myModal3').modal('toggle');
}
});
});
});
</script>
thank you.
If you want to show this table for editing just delete style="display:none" from tbody tag by using javascript after you rendered page or if the condition you wait turn to true. I do not recommend to use style directly inside of tag if you want to change it conditionally. You can do it by using javascript.
Or you can check if hide or not when rendering on PHP side.
<tbody style="<?php echo $ifConditionTrue ? "display:none" : "";?>">'
Or short way
<tbody style="<?= $ifConditionTrue ? "display:none" : "";?>">'

Delete button works on only first row not on the rest of the rows

I have fetched the values(card details) from an array and put them in an html table, I have also added the delete button for deleting the particular card.
I am deleting this through ajax. When i click on the delete button of the first row, it works perfect but when i click on the other delete buttons from 2nd or 3rd row, there is no action.
Please suggest the correct path to sort out this problem.
Thanks in advance
Here is my code
<tr>
<td><?php echo $val['last4'];?></td>
<td><?php echo $val['exp_month'];?></td>
<td><?php echo $val['exp_year'];?></td>
<td><button id = "del">Delete</button></td>
</tr>
Ajax part
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$('#del').click(function() {
//var a=document.getElementById("").value;
var val0 = $('#id').val();
var val01 = $('#cust').val();
var fade = document.getElementById("fade");
var show = function(){
fade.style.display = "block";
}
show();
$.ajax({
type: 'POST',
url: 'mywebsite.com/deleting.php',
data: { card_id: val0,cust_id: val01 },
success: function(response) {
fade.style.display = "none";
// alert(response);
mystring = response.replace(/^\s+|\s+$/g, "");
$('#result11').html(mystring);
}
});
});
</script>
You have the same id on your buttons. Id's must be unique within the html document and can not be shared between multiple elements. Read more about the html id attribute
Change the button to use a class instead:
<td><button class="del">Delete</button></td>
Then change your jQuery to bind to that class instead:
$('.del').click(function(e) {
// Since we're using ajax for this, stop the default behaviour of the button
e.preventDefault();
// Get the value from the clicked button
var val0 = $(this).val();
I also added the event e in the function call, and stopped the default behaviour of the button (which is to submit the form) when it's clicked.
You need to give delete buttons a class and bind event on that class not with id. Then on bind function select values from that row elements and then pass them to Ajax.

How do I use jQuery to redirect to a clicked TR in a dynamically-created table?

I have jQuery code that works beautifully for clicking a row in a table that exists at page-load and getting redirected to a new page:
$(document).ready(function() {
$("#myTable").on('click','tr',function() {
var href = $(this).find("a").attr("href");
if(href)
{ window.location = href; }
});
});
I have a second table that gets generated by a jQuery POST and is added dynamically and I know it works because I can alert() myself when I click on a row of a dynamically-created table:
$(document).on('click', '#myDynamicTable tr', function(e) {
var href = $(this).find("a").attr("href");
alert(href);
});
Obviously, href is undefined. So what code can I use to replace the alert() line with an actual redirect to the link that's in that particular TR?
Update with HTML code:
A sample HTML code that is returned via POST to the main page where the user clicks TRs:
<table id='myDynamicTable' border="1" cellspacing="0" cellpadding="10">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
</table>
Again, the only difference between this dysfunctional table row clicking and other tables on the same page is the fact that myDynamicTable is generated dynamically via jQuery based on a search string put into an input type="Text" by the user.
If you just want to change the browser URL, just set window.location:
Presumably your have anchors inside TDs inside your TRs (you should show sample HTML too).
$(document).on('click', '#myDynamicTable tr', function(e) {
var href = $(this).find("a").attr("href");
window.location = href;
});
Technically you are meant to set window.location.href, but all browsers allow the shortcut. Javascript: Setting location.href versus location
User This
<table id='myDynamicTable'>
<tr>
<td>
redirect to
</td>
</tr>
your jquery code is true
$(document).on('click', '#myDynamicTable tr', function(e) {
var href = $(this).find("a").attr("href");
window.location=href;
});

Selecting text input from PHP form using jQuery

I have php code that creates a table. For each item on the purchase order a new row is created and populated. The goal is when the purchase order is edited it gets posted back and saved in the database. Everything is working except my jQuery code will only iterate over the first row for however many rows I have. (So if I have three rows it will print the first rows cartid three times.)
Sorry if things look messy.
PHP Code:
<?php while($row = mysql_fetch_array($itemsquery)){?>
<tr class="item-row" id="<?php echo $row['cart_id']?>">
<td class="item-name"><div class="delete-wpr"><textarea><?php echo $row['on0'];?> <?php echo $row['itemname'];?></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td>
<td class="description"><textarea class="cartid"><?php echo $row['cart_id'];?></textarea></td>
<td><textarea class="cost" class="text-input" type="text"><?php echo $row['itemprice'];?></textarea></td>
<td><textarea class="qty" type="text" name="name"><?php echo $row['quantity'];?></textarea></td>
<td><span class="price" type="text">$<?php
$linecost = $row['quantity'] * $row['itemprice'];
echo $linecost;?></span></td>
</tr>
<?php }?>
jQuery statement to see what it has grabbed:
$(document).ready(function(){
$(".item-row").change(function() {
$((".cartid")).each(function(){
row = {
cartid: $(".cartid").val()
}
alert(row.cartid);
performAjaxSubmission();
})
});
It looks like you just have your selectors mixed up. You are wanting to iterate over each of the rows, but your selector is referencing $(".cartid"), which would always return the first result ever time.
This should fix your problem:
$(document).ready(function(){
$(".item-row").change(function() {
$(".item-row").each(function(){
var rowContext = $(this);
var row = {
cartid: $(".cartid", rowContext).val()
}
alert(row.cartid);
performAjaxSubmission();
});
});
You have extra () in your .cartid selector.
$(document).ready(function(){
$(".item-row").change(function() {
$(".cartid").each(function(){
row = {
cartid: $(".cartid").val()
}
alert(row.cartid);
performAjaxSubmission();
})
});
Also, shoudn't you be looping trough .item-row instead of .cartid?
You're iterating like this:
$(".cartid").each(function(){
row = {
cartid: $(".cartid").val()
}
Seems like you should be doing
$(".cartid").each(function(){
row = {
cartid: $(this).val()
}
Haven't checked the rest of the code.
EDIT:
$(".cartid").each(function(){
var cart = $(this);
row = {
cartid: cart.val()
}

Obtaining ROW ID from a Table created with MySql to use with an OnClick Function

I have a table created with PHP from an Sql Database that successfully displays the table information
using the following code example.
<table class="mytable loottable" id="guildmembers">
<?php
$query = mysql_query("SELECT $member.charname, $member.char_lvl, $trskill.skill_desc, .....
if (mysql_num_rows($query) > 0) {
while (false !== ($row = mysql_fetch_assoc($query))) {
echo '<tr id="'.$row['charname'].'">'; <-- setting row id here
echo '<td class="lootrow" id="memth1">'.$row['charname'].'</td>';
echo '<td class="lootrow" id="memth2">'.$row['rank_desc'].'</td>';
echo '<td class="lootrow" id="memth3">'.$row['class_name'].'</td>';
echo '<td class="lootrow" id="memth4">'.$row['char_lvl'].'</td>';
echo"</tr>\n";
}
mysql_free_result($query);
}
?>
</table>
Example of page source where i have verified that each table row is assigned the correct id.
<tr id="Calysta"><td class="lootrow" id="memth1">Calysta</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Inquisitor</td></tr>
<tr id="Rynanx"><td class="lootrow" id="memth1">Rynanx</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Mystic</td>
I am setting each table ROW ID to the first field which is always unique. Then i am trying to use this
ID in the following function to redirect users to a profile page when they click on a row in the table.
<script type="text/javascript">
$(document).ready(function () {
$("table.mytable").each( function() {
table = $(this);
if ($(table).attr("id")=="guildmembers") {
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + $(this).attr("id") + "/";
});
}
})
});
</script>
Which should open a new page in the browser with the following address for instance if the
first row is clicked : "http://everquest2.com/Kithicor/Calysta/"
Instead no matter which row is clicked the web url being created
is "http://everquest2.com/Kithicor/guildmembers/"
which is the Tables ID. not the ID of the row being clicked. I have tried various solutions
and cannot figure how to get the row ID in this click function. Any help with this would
much appreciated. Thanks.
Here you go, just did this quick, changed some things, please be mindful of using duplicate id's its BAD, you shouldn't. Also you over complicated your Javascript:
HTML EXAMPLE:
<table class="mytable loottable" id="guildmembers">
<tr class="linkRow" id="test1">
<td>test1</td>
</tr>
<tr class="linkRow" id="test2">
<td>test2</td>
</tr>
<tr class="linkRow" id="test3">
<td>test3</td>
</tr>
</table>
jQuery:
$(document).ready(function () {
$(".linkRow").each( function() {
id = $(this).attr("id")
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + id + "/";
});
});
});
jsFiddle Link:
http://jsfiddle.net/TJBX5/
You are installing your click handler on the table. Therefore, when you call $(this).attr("id"), you are asking for the table's id. You need to install the click handler on each row in the table.

Categories