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.
Related
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 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
});
I have created a web page where my customers will be able to enter details of all items there return back to my store. I have created in such a way that, 1 customer can enter his personal details once but can add any number of items he is returning. I have created a "add more" button to do this job. But when I am testing my site, database is only taking the last item entered and is not storing previous items.
<script> $(function () {
$('input.more').on('click', function () {
var $table = $('#input_fields');
var $tr = $table.find('tr').eq(0).clone();
$tr.appendTo($table).find('input').val('');
});
});
</script>
<tr>
<td width="33%"><label class="description" for="element_20">Manufacturer, Model, Serial # </label>
</td>
<td width="33%"><label class="description" for="element_10">Tag Number (If Any) </label>
</td>
<td width="33%"><label class="description" for="element_4">Item Description </label>
</td>
</tr>
</table>
<table id="input_fields" width="100%" border="0" cellpadding="4">
<tr>
<td width="33%"><input id="element_20" name="model" class="element text large" type="text" maxlength="255" value="<?php if($action != "add") echo $row["model"]; ?>"/></td>
<td width="33%"><input id="element_10" name="tag" class="element text large" type="text" maxlength="255" value="<?php if($action != "add") echo $row["tag"]; ?>"/></td>
<td width="33%"><input name="itemdesc" type="text" class="element text large" id="element_4" value="<?php if($action != "add") echo $row["itemdesc"]; ?>" /></td>
</tr>
</table>
<input class="more" type="button" value="Add more" name="addmore"/>
<ul>
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="Submit" value="Submit" />
In this, when Customer enters a item details and then clicks add more and enter other item details. Only the last enter item details are getting stored. But I want all items to be stored and displayed on DB.
You end up posting only a single model/tag/text to the server because you have the same name property for each copy of the input elements. When PHP tries to map the posted data to $_POST it will only map to a single model/tag/itemdesc key in $_POST because the input names are the same.
The easiest way to resolve this is to use array access notation in your input names like model[], tag[], itemdesc[]. This will allow PHP to assemble an array of values for each model/tag/itemdesc key in $_POST.
Try this. Do a var_dump($_POST) at the beginning of your script before changing your HTML code. Run a multi-item test case and look at the dumped value.
Then change to use array access notation in your HTML and see the difference the var_dump() gives.
I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>
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");