Hard one to explain in the title, but a bit of code says it all:
<tr class="">
<td>
<input value="9" name="set[122][order]"></input>
<input class="set-id" type="hidden" value="1" name="set[122][ex_id]"></input>
</td>
<td>
<input value="0.00" name="set[122][weight]"></input>
</td>
<td> … </td>
<td>
<img class="deleteRowButton" border="0" title="Toggle Delete Set" alt="Delete Set" src="mysite/images/icons/png/delete-3x.png"></img>
</td>
</tr>
I have a bit of jQuery code that is activated when the img (deleteRowButton) is clicked:
$('.deleteRowButton').click (function() {
$(this).parents("tr").toggleClass( "deleteSet" );
var id = $('.set-id', $(this).closest('td')).val(); // this bit not working
$('.editWoForm').append('<input type="hidden" name="deleteSet[]" value="' + id + '" />');
});
The deleteRowButton code basically just inserts a hidden input tag at the bottom of my form, so i have the ability to process these to remove entries from db.
BUT, what I need to do is grab the value from set[], so in this example 122. It can come from any of the inputs, as the whole tr is related to one entry. 122 is the db id, so that's what I need to grab.
So ideally, when the user clicks on the deleteRowButton, it generates and inserts:
<input type="hidden" name="deleteSet[]" value="122" />
Thanks in advance!
Solution
Thanks to #ArunPJohny for the assistance.
$('.deleteRowButton').click (function() {
var $tr = $(this).parents("tr").toggleClass( "deleteSet" );
var id = $tr.find('.set-id').attr('name').match(/\d+/)[0];
if($tr.hasClass( "deleteSet" )){
$('.editWoForm').append('<input type="hidden" name="deleteSet[]" value="' + id + '" />');
}
else{
$('input[name="deleteSet[]"][value="' + id + '"]').remove();
}
});
This will get the id, append a hidden input field with said id as the value, then if the button is pressed again (to toggle the delete state) the hidden input field is removed.
One way here is to fine the set-id element which is within the current tr element. what you are trying to do is to find an set-id which is within the td which contains the clicked deleteRowButton.
$('.deleteRowButton').click(function() {
//use closest instead of parents
var $tr = $(this).closest("tr").toggleClass("deleteSet");
//find the set-id within the current tr
var id = $tr.find('.set-id').attr('name').match(/\d+/)[0];
//$('.editWoForm').append('<input type="hidden" name="deleteSet[]" value="' + id + '" />');
$('#log').text(id)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr class="">
<td>
<input value="9" name="set[122][order]"/>
<input class="set-id" type="hidden" value="1" name="set[122][ex_id]"/>
</td>
<td>
<input value="0.00" name="set[122][weight]"/>
</td>
<td> … </td>
<td>
<img class="deleteRowButton" border="0" title="Toggle Delete Set" alt="Delete Set" src="mysite/images/icons/png/delete-3x.png"/>
</td>
</tr>
<tr class="">
<td>
<input value="9" name="set[123][order]"/>
<input class="set-id" type="hidden" value="2" name="set[123][ex_id]"/>
</td>
<td>
<input value="0.00" name="set[123][weight]"/>
</td>
<td> … </td>
<td>
<img class="deleteRowButton" border="0" title="Toggle Delete Set" alt="Delete Set" src="mysite/images/icons/png/delete-3x.png"/>
</td>
</tr>
</table>
<div id="log"></div>
Related
I have a PHP generated list of product links that, when clicked, adds a product to a shopping cart. At present, there's a quantity of '1' hard-coded in the links, e.g.
Add To Cart
I'd like to add a quantity field to each item in the list, so a customer can add the quantity they want to the cart.
This list doesn't use a form, so how could I introduce a user input qty field and use it in each link? My searches have turned up nothing, so I take it that it can't be done the way I have it set up now. No matter how I change this list, I need to pass an array to the /shop/checkout PHP script.
I'm probably looking at this the wrong way, so any pointers would be appreciated.
Thanks!
EDIT
It must have something to do with the fact that my product list is in a table. When I change your example to use a table instead of divs, it breaks. Here's a sample:
<table class="products_list">
<tr class="product_list--item">
<td> 12486XC4 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
<tr class="product_list--item">
<td> 13486XC5 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
<tr class="product_list--item">
<td> 14486XC6 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
</table>
Also, 1[sku] and 1[qty] are invariable in the links. They are used in the destination PHP script for each product on this page. The only thing that changes from product to product is the value of the sku.
You can use javascript for this instead of one big form and complex php code. Simply put each link in the wrapper with <input type="number"/> that will allow to add quantity to your product. Then you can change URI for checkout. Working example with comments of how to do this i have add below.
Hope that this will help!
Update
Because you are not showing your code its hard to understand what is not working on your side. So show us what you did and lets find out the issues instead of pin pointing the problem ;)
I don't see any issues with rendering your template using php and putting values of proper variables as a values of html attributes.
You can add name attribute which can be equal to your qty array name for proper sku. Example
<input name="1[qty]" class="item_qty" type=number min="1" max="100" value="1" />.
In php it will look like this (and again this is just an example of how you can achieve your result)
<input name="<?="1[qty]={$qty}";?>" class="item_qty" type=number min="<?=$qty;?>" max="100" value="1" />
<?=?> is an echo tag. You can replace with <?php ?>
Later you can simply grab the value of the attribute using JS and update each corresponding checkout link. Moreover your case didn't work because in RegExp symbols like [ and ] should be escaped -> \[ and \].
function addSlashes(string, vowels) {
let length = vowels.length;
let finalString = "";
let startOffset = 0;
let endOffset = 0;
for (let i = 0; i < length; i++) {
if ((endOffset = string.indexOf(vowels[i])) !== -1) {
finalString += string.substring(startOffset, endOffset) + "\\";
if (endOffset === string.length - 1) {
finalString += string[string.length - 1];
}
startOffset = endOffset;
}
}
return finalString;
}
function updateQueryStringParameter(uri, key, value) {
let escapedKey = addSlashes(key, ["[", "]"]);
let re = new RegExp("([?&])" + escapedKey + "=.*?(&|$)", "i");
let separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
} else {
return uri + separator + key + "=" + value;
}
}
function displayQuantityForProduct(element, text = "") {
element.textContent = text;
}
const inputs = document.querySelectorAll(".product_list--item .item_qty");
const length = inputs.length;
for (let i = 0; i < length; i++) {
inputs[i].addEventListener("input", function() {
// also add check whenever input value is not an empty string and its an integer which is bigger or equal to 1
if (this.value && parseInt(this.value) >= 1) {
// selfName
let selfName = "1[qty]";
let children = this.closest(".product_list--item").children;
// children will consist of td elements from each tr with class product_list--item in your table
// [0] first element of the array is sku (1) <td> 12486XC4 </td>
// [1] second element of the array is amount with span (2)
// [2] is input with qty (3)
// [3] is Add to cart link (4) and so on...
let displayQty = children[1].firstElementChild;
let addToCartLink = children[3].firstElementChild;
let newUri = updateQueryStringParameter(
addToCartLink.getAttribute("href"),
selfName,
this.value
);
displayQuantityForProduct(displayQty, this.value);
addToCartLink.setAttribute("href", newUri);
}
});
}
<table class="products_list">
<tr class="product_list--item">
<td> 12486XC4 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
<tr class="product_list--item">
<td> 13486XC5 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
<tr class="product_list--item">
<td> 14486XC6 </td>
<td> Amount: <span class="item_display--1[qty]">1</span> </td>
<td> <input class="item_qty" id="itm_qty" type="number" min="1" max="100" value="1" size="5" /> </td>
<td> Add to cart </td>
</tr>
</table>
Am having table data (retrieve data from mysql table and fetch in to table). table contains several records.I want to display checked checkbox value with input box value and checkbox when i clicking button in php. Checked checkbox value and checked input has deen displayed correctly using join function. but checked with checkbox is not showing correctly. In my code, when i clicking button all checked check values are displayed. my problem to display only checked checkbox with checkbax using join function.
My table:
<table border="0" cellpadding="10" cellspacing="1" width="500" class="tblListForm">
<tr class="listheader">
<td></td>
<td>Username</td>
<td>First Name</td>
<td>Last Name</td>
<td>Permissions</td>
<td>CRUD Actions</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$classname="evenRow";
else
$classname="oddRow";
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><input type="checkbox" class="chk_id" name="chk_id" id="chk_id" value="<?php echo $row["userId"]; ?>" /></td>
<td><?php echo $row["userName"]; ?></td>
<td><input type="text" name="firstName" class="firstName" id="firstName" value="<?php echo $row["firstName"];?>" /></td>
<td><?php echo $row["lastName"]; ?></td>
<td><input type="checkbox" name="grant" class="grant" id="grant" value="Y" /></td>
<td><img alt='Edit' title='Edit' src='images/edit.png' width='15px' height='15px' hspace='10' /> <img alt='Delete' title='Delete' src='images/delete.png' width='15px' height='15px'hspace='10' /></td>
</tr>
<?php
$i++;
}
?>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
my jquery code what i have tried:
$('#save_value').click(function () {
alert("Checkbox running");
var chk_id = [];
var firstName = [];
var grant = [];
$.each($("input[ id='chk_id']:checked"), function () {
chk_id.push($(this).val());
firstName.push($(this).parent().parent().find("#firstName").val());
grant.push($(this).parent().parent().find($("#grant").is(':checked'));
});
alert(chk_id);
alert(firstName);
alert(grant);
});
Here,
am getting checked checkbox and checked input value. my problem to dispaly the checked checkbox with check value.
Thanks#
You made a few small mistakes:
You can't have multiple elements with the same ID, IDs must be unique. So I removed all duplicate IDs from your HTML (id="chk_id",id="firstName",id="grant") and in your JS, used the classes instead.
You missed a closing bracket in grant.push($(this).parent().parent().find($(".grant").is(':checked')));.
.find($(".grant").is(':checked')) isn't working as you expect, and also not necessary.
Use this instead: .find(".grant:checked").
And finally, the reason why your alert showed two values whether the checkboxes were checked or not: grant.push( ... ); always pushes something into the array, if the jQuery-selector matched nothing and would return false, that value would still be pushed into the array.
In fact, if you correct all three points above, and don't check the permission checkbox, the value in the array will be undefined. If you do check the box, it will be Y.
So, in order to make it work, you just have to put the grant.push( ... ); inside an if-clause, where you check for ".grant:checked":
if ($p.find(".grant:checked").length) {grant.push($p.find(".grant:checked").val());}
- $p stands for $(this).parent().parent(), I stored a reference in a var.
- .length checks if the length of the returned object is greater than 0. Without it, the if-clause would still always be true, because jQuery still returns an object (with value undefined).
See code snippet below for a demo:
$('#save_value').click(function() {
var chk_id=[], firstName=[], grant=[];
$.each($("input[class='chk_id']:checked"),function() {
var $row = $(this).parent().parent();
chk_id.push($(this).val());
firstName.push($row.find(".firstName").val());
if ($row.find(".grant:checked").length) {grant.push($row.find(".grant:checked").val());}
});
console.log(chk_id, firstName, grant);
});
table,input[type=button] {float:left;} /*ONLY SO console.log() DOESN'T COVER BUTTON*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="0" cellpadding="0" cellspacing="0" width="500" class="tblListForm">
<tr class="listheader"><td></td><td>Username</td><td>First Name</td><td>Last Name</td><td>Permissions</td></tr>
<tr class="evenRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="4" /></td>
<td>sardhar</td>
<td><input type="text" name="firstName" class="firstName" value="sardhar" /></td>
<td>mohamed</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
<tr class="oddRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="3" /></td>
<td>fg</td>
<td><input type="text" name="firstName" class="firstName" value="vb" /></td>
<td>vb</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
jsfiddle: https://jsfiddle.net/3utno9at/
I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:
<form method="post" action="exam_page.php" >
<table >
<tr style="background-color: #7F859E;color:white; height:50px;">
<th style="padding-left:140px; width:550px;">Paper</th>
<th style="padding-left:40px;">Time</th>
<th style="padding-left:40px;">Duration</th>
<th style="padding-left:40px; width:250px;"></th>
</tr>
<?php
$i=1;
while($row=mysql_fetch_array($rs)){?>
<tr style="height:80px; background-color: #CCCCCC;">
<td style="padding-left:40px;">
<input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
</td>
<td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
</tr>
<?php $i++; } $_SESSION['exam']=$i; ?>
</table>
</form>
Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.
<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
Start
</button>
Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)
To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.
<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">
and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.
If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.
Try using javascript onclick functions:
<script>
function getPaperCode(paperCode)
{
alert(paperCode);
}
</script>
Then edit your input add onclick event:
<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>
Once you click. it will alert the value of the button
passed your unique id value or examcode via hidden field like this
<input type="hidden" name="id" value="<?=$row['eid']?>">
and on button click perform query like
$id=$_POST['id'];
select * from table_name where id='$id';
I'm currently running an html and jQuery code that works but looks very ugly for me. I would really appreciate suggestion to do this more smarter.
Goal: update cells of a php generated table using an input field. Each cells contains a basic value which should be multiplied by the input.
Currently, I read the value of the input, take the basic value of each cell from an hidden input and rewrite the whole html of each cell ( hidden input + updated data).
Html code:
<table id='tbl'>
<thead>
<tr>
<th colspan="2">Quantity:
<input type="text" id="quantity" name="quantity" value="1">
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input name="1_1" type="hidden" value="11"><span>11</span>
</td>
<td>
<input name="1_2" type="hidden" value="12"><span>12</span>
</td>
</tr>
<tr>
<td>
<input name="2_1" type="hidden" value="21">21
</td>
<td>
<input name="2_2" type="hidden" value="22">22
</td>
</tr>
<tr>
<td>
<input name="3_1" type="hidden" value="31">31
</td>
<td>
<input name="2_2" type="hidden" value="32">32
</td>
</tr>
</tbody>
</table>
Script:
$('#quantity').keyup(function () {
var quantity = parseFloat($(this).val());
if (quantity == "" || isNaN(quantity))
quantity = 1;
$('#tbl tbody tr td').map(function () {
var $row = $(this);
var $refvalue = $row.find(':input[type=hidden]').val();
var $refname = $row.find(':input[type=hidden]').attr('name');
$row.html('<input name="' + $refname + '" type="hidden" value="' + $refvalue + '">' + $refvalue * quantity);
});
});
Fiddle : http://jsfiddle.net/5KKrD/
Is there a better way to do ? I'm able to change both table, js and so on.
Regards
this is my approach - use data attribute (instead of a hidden input) to pass the multiplier, e.g.:
<td class="cell" data-value="11">11</td>
with this - you can grab the value very easily using jquery, e.g.:
$('.cell').data('value')
DEMO
I have refined your code a bit,its working smoothly
JS CODE:
$('#quantity').keyup(function () {
if ($(this).val()) {
var quantity = parseInt($(this).val());
if (quantity == "" || isNaN(quantity)) quantity = 1;
$('#tbl tbody tr td input').each(function () {
var row = $(this);
var mulVal = row.val() * quantity;
$(this).attr('value', mulVal);
$(this).parent().find('span').text(mulVal);
//var $refvalue = $row.find(':input[type=hidden]').val();
//var $refname = $row.find(':input[type=hidden]').attr( 'name' );
//$row.html( '<input name="'+ $refname +'" type="hidden" value="'+ $refvalue + '">' + $refvalue * quantity );
});
}
});
HTML CODE:
<table id='tbl'>
<thead>
<tr>
<th colspan="2">Quantity:
<input type="text" id="quantity" name="quantity" value="1">
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input name="1_1" type="hidden" value="11" /><span>11</span>
</td>
<td>
<input name="1_2" type="hidden" value="12" /><span>12</span>
</td>
</tr>
<tr>
<td>
<input name="2_1" type="hidden" value="21" /><span>21</span>
</td>
<td>
<input name="2_2" type="hidden" value="22" /><span>22</span>
</td>
</tr>
<tr>
<td>
<input name="3_1" type="hidden" value="31" /><span>31</span>
</td>
<td>
<input name="2_2" type="hidden" value="32" /><span>32</span>
</td>
</tr>
</tbody>
</table>
LIVE DEMO
Happy Coding :)
It might be a bit over-kill for your needs, but this is what backbone.js aims to do - remove data from the HTML DOM and instead treats it Models and Views.
http://backbonejs.org/#examples
Apart from that, what's wrong with your current approach - what specifically do you want to improve?
The code cleaned up a bit:
$('#quantity').keyup(function() {
var quantity = parseFloat($(this).val());
quantity = ( quantity == "" || isNaN( quantity ) ? 1 : quantity;
$('#tbl tbody tr td').map(function() {
var $hiddenElement = $(this).find(input[type='hidden']);
$(this).html( '<input name="'+ $(hiddenElement).attr('name') +'" type="hidden" value="'+ $(hiddenElement).val() + '">' + (parseFloat($(hiddenElement).val()) * quantity) );
});
});
I think yours is just fine, the only thing I would personally do to improve it is place html in a var and use it as a template to keep syntax cleaner, I would replace this
$row.html( '<input name="'+ $refname +'" type="hidden" value="'+ $refvalue + '">' + $refvalue * quantity );
with this
var template ='<input name="{name}" type="hidden" value="{refvalue}">{res}</a>';
then
$row.html(template.replace('{name}', $refname) ); // and so on
I wanted to put a checkbox autosum on my website so that anyone who check a box and submit, the values (Points) of those check box are automatically added to my total sum (on home page actually)
I have found this code so far for java script.
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td> <input name="sum_m_1" value="25" type="checkbox" id="sum_m_1" onclick="UpdateCost()"> </td>
<td> ResponseOption_1 </td>
</tr>
<tr>
<td> <input name="sum_m_2" value="15" type="checkbox" id="sum_m_2" onclick="UpdateCost()"> </td>
<td> ResponseOption_2 </td>
</tr>
</table>
<input type="text" id="totalcost" value="">
With JAVASCRIPT:
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=1; i<3; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(2);
}
window.onload=UpdateCost
</script>
This will add up the values but i wanted to add by checking check boxes --> Submit --> collect the data --> add to my main total.
Any helps would be much appreciated.
Simple changes:
Add a button "calculate cost".
Removed the onclick events from the checkboxes and added the onclick event to the button instead.
Note that "window.onload=UpdateCost" is still there because what you want ideally is for the total cost to be displayed properly as 0.00 when the page loads.
HTML
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td> <input name="sum_m_1" value="25" type="checkbox" id="sum_m_1"> </td>
<td> ResponseOption_1 </td>
</tr>
<tr>
<td> <input name="sum_m_2" value="15" type="checkbox" id="sum_m_2"> </td>
<td> ResponseOption_2 </td>
</tr>
</table>
<input type="text" id="totalcost" value="">
<input type="button" value="update cost" onclick="UpdateCost();">
JavaScript
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=1; i<3; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem. checked == true) { sum += Number(elem. value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(2);
}
window.onload=UpdateCost
</script>
Hope that's the kind of thing you wanted, let me me know what you think.