I am using Codeigniter. I generate a view that contains a table within a form with <tr>s like this:
<tr>
<td><input name="set[2][order]" value="3">
<input type="hidden" name="set[2][ex_id]" value="1"></td>
<td><input name="set[2][weight]" value="60.00"></td>
<td><input name="set[2][reps]" value="5"></td>
<td><img class="deleteRowButton" src="/assets/images/icons/png/delete-3x.png" border="0" alt="Delete Set" title="Toggle Delete Set"/></td>
</tr>
I have a little jQuery script that toggles the class of the <tr> when the img 'delete-3x.png' is clicked:
<script>
$('.deleteRowButton').click (function() {
$(this).parents("tr").toggleClass( "deleteSet" );
});
</script>
so it looks like this:
<tr class="deleteSet">
All this does so far is change the opacity of the <tr> so I make it looks greyed out, just to signify that it has been selected for deleting.
What i want to achieve when the user submits the form and am not sure how to handle in CI, is somewhere along the line, in plain English code, saying:
if tr class = "deleteSet", then delete from db
At the moment all inputs are just written to database. so I need a way of recognising that the user want to remove an entry.
write one function in controller which delete item by id
Html :
<table>
<tr>
<td><input name="set[2][order]" value="3">
<input type="hidden" name="set[2][ex_id]" value="1"></td>
<td><input name="set[2][weight]" value="60.00"></td>
<td><input name="set[2][reps]" value="5"></td>
<td><img class="deleteRowButton" url="/your-controller-name/method-name/item-id" src="/assets/images/icons/png/delete-3x.png" border="0" alt="Delete Set" title="Toggle Delete Set"/></td>
</tr>
</table>
Js:
$('.deleteRowButton').off('click').on('click',function(){
var _this = $(this);
var url = $(_this).attr('url');
$.post(url,function(data){
$(_this).parents('tr').remove();
});
});
You need different logic, your backend will never know HTML class. It can only know POST/GET params.
What I recommend is:
User clicks on DeleteSet button on current line item.
Javascript adds that line item ID to somewhere (in memory or as HTML hidden property)
When you press "save" button it will send list of ID's that you want to delete.
Psudo Example
HTML
<tr>
<td><input name="set[2][order]" value="3"> <input class="set-id" type="hidden" name="set[2][ex_id]" value="1"></td>
<td><input name="set[2][weight]" value="60.00"></td>
<td><input name="set[2][reps]" value="5"></td>
<td><img class="deleteRowButton" src="/assets/images/icons/png/delete-3x.png" border="0" alt="Delete Set" title="Toggle Delete Set"/></td>
</tr>
JS
$('.deleteRowButton').click (function() {
$(this).disable(); // Don't want to duplicate ids
var id = $('.set-id', $(this).closest('tr')).val();
$('.submit-form').append('<input type="hidden" name="delete[]" value="' + id + '" />');
// Now when form will be submitted it will be populated with item ids you want to delete
});
Related
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';
This is a bit weird but this time I have came across something that works on IE and not on other browsers like firefox and chrome..
Here is the issue:
I am dynamically loading a part of a page, that contains a form, into my existing page with jquery .load() method.
I have used .on method to attach event handler for the newly added elements.
But when I click on submit button the form submit method works fine, but it doesnt send any data in post query ( by the way, I have specified form method=POST")
The main issue is that before I fire the .load() method to obtain new elements and replace the existing ones, the .submit() works FINE. IT SENDS THE POST DATA.
But after the dom is replaced, there is no data in POST.
jQuery Code:
$(document).on("click", ".s_edit",function()
{$(this).parents('tr').children('form').submit();});
.s_edit is the form submit button:
PHP/HTML code (CodeIgniter):
<tr>
<form method="post" action="<?php echo base_url();?>index.php/userlist/inline_edit/<?php echo $r['id'];?>">
<td><input class="record_edit" type="text" name="name" id="name<?php echo $r['id'] ?>"/></td>
<td><input class="record_edit" type="text" name="age" id="age<?php echo $r['id']; ?>"/></td>
<td>
<span class="record_edit">
<input id="gen_m<?php echo $r['id']; ?>" type="radio" name="gender" value="m"/>Male<br/>
<input id="gen_f<?php echo $r['id']; ?>" type="radio" name="gender" value="f"/>Female
</span>
</td>
<td><input class="record_edit datepick" type="text" name="joining_date" id="joining_date<?php echo $r['id']; ?>"/></td>
<td>
<div class="record_edit">
<input type="submit" value="save" class="s_edit"/>
<input type="button" class="cancel_edit" value="Cancel"/>
</div>
</td>
</form>
</tr>
Because you have <tr><form><td>, other browsers are stripping the form element out, or re-arranging it in the DOM tree.
I.E. In Firefox,
<table>
<tr>
<form>
<td>a</td>
</form>
</tr>
</table>
goes to
<table>
<tbody><tr>
<form></form>
<td>a</td>
</tr>
</tbody></table>
Hence, your form is no longer wrapping the elements in the td.
Ok..I replaced the table formatting with div and table display properties in css..firefox and mozilla as Benno suggested was getting rearranged except for IE..still it somehow worked before invoking .load method of jquery..but now its working completely fine..
We've inherited a php/CodeIgniter app. Without going into all the reasons why, I need to feed values into a textarea field so I can group a bunch of data together and send it to a field in another app/outside vendor. This is the first time we have encountered this issue, but I don't think it is the last, so I want to prepare for it.
The specifics:
Web form with a bunch of fields on it. It's a self generating php/CodeIgniter app that the client controls, so fields are different from client to client.
Certain clients may need to send data from 3, 5, 7, etc., of the field within their form to an external vendor who receives all the data in one field on their end. So in short, using jQuery, I want to send data from certain fields to a textarea field.
For example, I want to send Center Title, Full Name, and Fruits to the textarea field with a line break at the end of each. Of course, if the user empties a field, that line item would be removed from the textarea field.
Click here to view my jsFiddle demo.
HTML Example:
<form method="post">
<br />
<br />
<fieldset name="Group1" style="border: thin; border-color: green">
<legend>General Information</legend>
<table style="width: 100%">
<tr>
<td style="width: 249px">Center Title:</td>
<td>
<select name="centers" id="centers">
<option value="Corp 1">Corp 1</option>
<option value="Shamrock Gold">Shamrock Gold</option>
<option value="Hensin Way">Hensin Way</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Full Name:</td>
<td>
<input name="fullname" id="fullname" type="text" size="20" />
</td>
</tr>
<tr>
<td style="width: 249px">Job Title:</td>
<td>
<input name="jobtitle" id="jobtitle" type="text" />
</td>
</tr>
<tr>
<td style="width: 249px">Known Alergies:</td>
<td>
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="Yes" />Yes
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="No" />No
</td>
</tr>
<tr>
<td style="width: 249px; height: 102px;">How Many?:</td>
<td style="height: 102px">
<select multiple="multiple" name="Select2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Fruits:</td>
<td>
<input name="Fruit[]" id="Fruit[]" type="radio" checked="checked" value="Apple" />Apple<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Orange" />Orange<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Fruit" />Fruit
</td>
</tr>
</table>
<label>Complete Info:</label>
<textarea name="allVendorInfo" id="allVendorInfo" cols="50" rows="7"></textarea><br />
<br />
</fieldset>
</form>
You can access selections/user-input using following code
$(document).ready(function(){
$("input").change(function(e){
if($(this).is(':checked')){
}else{
};
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
$("select").change(function(e){
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
});
EDIT: Original fiddle was broken for some reason, have updated the link
I was thinking something along the lines of this: http://jsfiddle.net/JRwzz/3/
The 'trigger' for this could be something other than the Run JS button of course, that's just there for example, i'd imagine it'd be on submit or other user action.
It clears the textarea first, then loops all of the input's and selects, depending on the type of element - checkbox, radio, select etc.. it uses slightly different methods to get the values (e.g. if it's a checkbox it only wants to get the value from a checked one)
It'll need a bit of polish in order to have a checkbox group's values all on one line and things like that, but hopefully this is enough for you to get the idea.
Then on each thing it finds it appends it to the textarea and puts a linebreak on the end.
It wouldn't be too hard to add a condition to check for another atribute (e.g. data-export="yes") to check for before including it in the textarea.
Just to note, I thought of it this way because you said all of the forms are dynamic, so I tried not to need to rely on ID's or names for things, it'll just apply to any form. If you can get the code that generates your forms to output an attribute in the html on the ones you want included in your textarea (and perhaps some method of your client selecting which ones that'll apply to in their administration area) then that'd be spot on, would save having to fiddle JS for every client.
Try something like this:
$("select, input").bind("change keyup", function() {
var form = $("<form>");
var data = new Object();
form.append($(":checked").clone());
$.each($("select"), function(i, item) {
if ($(item).val() != null) {
form.append(($(item).clone()));
}
});
$.each($("input[type='text']"), function(i, item) {
if ($(item).val().length > 0) form.append(($(item).clone()));
});
$("#allVendorInfo").val(form.serialize());
}).first().trigger("change");
I have a table with a column on prices and the next column is a check box to mark whether that item was paid. I was wondering how I could populate the text box with the amount when the check box is clicked.
Code:
<table>
<tr>
<td>Procedure</td>
<td>Amount</td>
<td align="center"><input type="checkbox"></td>
<input type="text" size="20" value="" class="currency">
</tr>
</table>
HTML
<table>
<tr>
<td>Procedure</td>
<td>Amount</td>
<td align="center">
<input type="checkbox">
</td>
<td>
<input type="text" size="20" value="" class="currency">
</td>
</tr>
</table>
JavaScript/jQuery:
$(function() {
$('table input[type="checkbox"]').change(function() {
var input = $(this).closest('td').next('td').find('input');
if ($(this).is(':checked')) {
var amount = $(this).closest('td').prev('td').text();
input.val(amount);
} else {
input.val('');
}
});
});
See a working example at: http://jsfiddle.net/KTQgv/2/.
Some code you can expand on:
<input type="checkbox" rel="textbox1" name="banana"/>
<textarea id="textbox1" ></textarea>
JS/jQuery:
$('input:checkbox').click(function(){
var tb = "#"+$(this).attr('rel');
if($(this).is(":checked"))
$(tb).append(this.name + "\n");
});
Fiddle: http://jsfiddle.net/maniator/hxkX3/
Semantically, a checkbox isn't really the best control to choose to initiate an action. I would have an edit or pay (something actionable) button/icon which initiates the action of allowing the user to enter a value.
However for the purposes of your example, the click event of the checkbox is enough to be able to change the contents of your table cell from the displayed text (if any) to a textbox.
Given
<td id="row_1">Unpaid</td>
Using
$('#row_1').html('<input type="text" name="txtRow1" />');
is simplistic, but enough to enable the user to type in a value which could then be posted to the server during a save action.