OK, I am generating a form which is pre-populated with existing values from the db:
View:
<tr>
<td><input name="set[<?php echo $cur_set['id']; ?>][order]" value="<?php echo $cur_set['wo_order']; ?>">
<input type="hidden" name="set[<?php echo $cur_set['id']; ?>][ex_id]" value="<?php echo $cur_set['ex_id']; ?>"></td>
<td><input name="set[<?php echo $cur_set['id']; ?>][weight]" value="<?php echo $cur_set['weight']; ?>"></td>
<td><input name="set[<?php echo $cur_set['id']; ?>][reps]" value="<?php echo $cur_set['reps']; ?>"></td>
</tr>
Example output:
<tr>
<td><input name="set[3][order]" value="1">
<input type="hidden" name="set[3][ex_id]" value="1"></td>
<td><input name="set[3][weight]" value="50.00"></td>
<td><input name="set[3][reps]" value="5"></td>
</tr>
So I have a range of these <tr>s each can have a unique 'id' but all have the same number of secondary indices [order], [ex_id], [weight] and [reps].
I cannot figure out 2 things:
How to apply the set_value() syntax when I don't know what the [id] is going to be.
How to set validation rules on the form. I have tried:
$this->form_validation->set_rules('set[][order]', 'Sets', 'required');
but it doesn't seem to work as set[][order] is not found...can I put some sort of wildcard in there? Like set[*][order]?
Thanks in advance.
Jon
In the controller, where you fetch the data and store it in a variable (I'll call it $data), do this:
$data = $this->MODEL_NAME->METHOD_NAME();
foreach($data as $set){
$this->form_validation->set_rules("set[{$set['id']}][order]");
$this->form_validation->set_rules("set[{$set['id']}][weight]");
etc...
}
Related
[enter image description here][1]i want repopulate my form using edit button
once i clicked edit button it ll shows what are all the selected items are to be displayed
<input type="checkbox" id="mycheck1" name="certid[]"
value="1" class="cbx" <?php echo set_checkbox('certid','1');?>
Controller
$data['getcert'] = $this->User_Model->getcert();
output should be in the form of array
Array
(
[id] => 1
[certificate_id] => 1
[fee] => 500
[APPNO] => 10001
[regno] => 01107402042
[certid] => 1
[noc] => 2
[paid] => 1000
)
<?php foreach($getcert as $student){
echo '<pre>';
print_r($student);
echo '</pre>';
?>
<td width="50px" align="center">1</td>
<td>Transcripts & Degree Certificate Attestation</td>
<td align="center"><input type="checkbox" id="mycheck1" name="certid[]"
value="<?php echo (!isset($student['certid'])) ? 1 : $student['certid']
?>"
class="cbx" <?php echo set_checkbox('certid[]','1');?> ></td>
<td> 500 </td>
<td> <input type="number" id="primaryincome1" min="1" max="999"
name="noc[]"
value="<?php echo (!isset($student['noc'])) ? '' : $student['noc'] ?>"
disabled> </td>
<div class="col-xs-2">
<td ><input type="text" id="totalamountremaining1" name="txt"
class="text-right" value="<?php echo (!isset($student['paid'])) ? 0 :
$student['paid'] ?>" size="5"></td>
</div>
that certid value only be placed in checkbox
This values are stored in db .how to assign that value in this view?
Right so, apparently set_checkbox() is more of something to be used when repopulating fields from form validation. E.g. the field has a post value and that post value needs to be brought back when validation fails. So it's not really something for determining if the db field matches the checkbox value.
Now I'm assuming that the logic you want is that if the value in the db for certid is 1 then the checkbox should be checked. In which case you can do:
<input type="checkbox" id="mycheck1" name="certid[]" value="<?php echo (!isset($student['certid'])) ? 1 : $student['certid'] ?>" class="cbx" <?php if ($student['certid'] == 1):?> checked="checked"<?php endif;?>>
try this:
<input type="checkbox" id="mycheck1" name="certid[]"
value="1" class="cbx" <?php echo set_checkbox('certid','1', $certid==1;?>>
with $certid as your db value
from manual:
third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE)'
so you test your db value if it fits the checkbox value, thats it.
This is not direct answers to your question but you will get an idea how to do this.
In the HTML:
<input type="hidden" name="certid" value="<?php print base64_encode(serialize($result)); ?>">
And in the controller:
$data = unserialize(base64_decode($this->input->post('certid')));
Using this method you can pass the array data from the frontend to your controller.
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've got a MySQL database and a table called courses. I would like to show the records of this table within an html table, but I would also like to edit them with a submit button at the end.
I know this has been asked before, but I'm still struggling with how I should properly proceed.
I currently have:
<tbody>
<?php
foreach ($db->getResultSet() as $row) {
extract($row);
?>
<tr>
<form id="myTable" name="editrecord" action="course.php" method="post">
<input type="hidden" name="code" value="<?php echo $code; ?>" /><!--PrimaryKey-->
<td><?php echo $code; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
<td><input type="text" name="desc" value="<?php echo $desc; ?>" /></td>
<td><input type="submit" name="Edit" value="Edit!" /></td>
</form>
</tr>
<?php
}
?>
</tbody>
Now this seems to work fine. However I'm aware that <form> should not be inside <tr> or anywhere else other that <td>
I'm aware I could use various divs and set display properties to table layout etc. however I'd like to avoid this as I am using pre-made javascript that can sort and filter data which requires me to use <table>
I also tried a for loop outside the table to create 1 form per record and use the primary key as the form's id. Then proceed with echo "<td><input form='$code' .... But this doesn't work in Explorer
What is the correct way to implement something like this?
I need to perform an addition of one of the database column's selected values and user input dynamically.
I choose the database values depending on a condition and I fetch it in an array.
Now, I need to sum both values and the result need to placed in another one text box.
I use this code :
while($res = mysqli_fetch_array($result)){
?>
<tbody>
<tr>
<td height="41"><?php echo $res['valuea']; ?></td>
<td><?php echo $res['valueb']; ?></td>
<td><?php echo $res['valuec']; ?></td>
<td><?php echo $res['valued']; ?></td>
<td><input name="txt" type="text" disabled="disabled" class="txt" id="txt" value="<?php echo $res['valuee']; ?>" readonly="readonly"/></td>
<td><input class="qty" type="text" id="qty" name="qty" onChange="third();"/></td>
<td><input class="amount" type="text" name="amount" id="amount" /></td>
</tr>
<?php
} //while
I think here is my mistake :
To add the values I used javascript but it accepts the first entry only... It doesn't work with the next entries :
<script type="text/javascript">
function third(){
var txt=document.getElementById('txt').value;
var qty=document.getElementById('qty').value;
var amount=txt*qty;
document.getElementById('amount').value=amount;
}
</script>
I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.
I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).
Here is the form
<center><h2>Create Event</h2></center>
<form method="post">
<?
$form = new common_functions();
if($form->check_saved_forms($my_username, "event") == TRUE){
$this->existing_forms(); //allow the user to select existing forms that they have created
}
?>
<tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
<tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
<tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
<tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>
</table>
<?
//this is the section giving me trouble
$form->add_friends($added_friends);
$form->register_tasks($tasks,$nums);
?>
</form>
Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.
Here is register (add_friend is the same, but labeled differently)
function register_tasks($tasks,$nums){
<table>
<td><input type="textbox" size="50" name="register_description"/> </td></tr>
<td><input type ="textBox" name="register_num" /> </td>
<td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
</table>
<?
if(count($tasks) > 0){
?>
<table>
<tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
<?
foreach (array_combine($tasks, $nums) as $task => $task_num){
?>
<tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
<td><input type="submit" name="remove_task" value="Remove"/></td>
<td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
<td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
<?
}
?></table><?
}
You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.