set_checkbox value not returned - php

[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.

Related

Multi-dimensional form $_POST data keys not set

This is how I display the entire array:
print('<pre>');
print_r($_POST);
print('</pre>');
The Array :
Array
(
[Prod] => Array
(
[0] => Array
(
['id'] => 1
['name'] => value2
['desc'] => value
['cost'] => 500.0000
['sell_price'] => 1500.0000
['quant'] => 9
['vendor'] => 1002
['last_update'] => Joe Lee
)
[1] => Array
( ... )
['btnUpdate'] => Sumbitted Form
)
)
but if I change it to $_POST['Prod']['btnUpdate'] it throws an error..
This is how the Error if I try to access "btnUpdate"
Notice: Undefined index: btnUpdate in
C:\xampp\htdocs\Projects\Upwork\dharn\scratch\phase2\controller\update_Product.php
on line 6
I've read the duplicate but as you can see the entire array of $_POST.. in my knowledge $_POST['Prod']['btpUpdate'] should exist but NO it does not, php can't find it...
UPDATE
i've made a type mistake on creating the array..
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td>
<input type="text" hidden name="Prod[<?php echo $x?>]['id']" value="<?php echo $row['id']?>">
<?php echo $row['id']?>
</td>
<td>
<input type="text" name="Prod[<?php echo $x?>]['name']" value="<?php echo $row['name']?>">
</td>
<td>
<input type="text" name="Prod[<?php echo $x?>]['desc']" value="<?php echo $row['description']?>">
</td>
<td>
<input type="text" name="Prod[<?php echo $x?>]['cost']" value="<?php echo $row['cost']?>">
</td>
<td>
<input type="text" name="Prod[<?php echo $x?>]['sell_price']" value="<?php echo $row['sell_price']?>">
</td>
<td>
<input type="text" name="Prod[<?php echo $x?>]['quant']" value="<?php echo $row['quantity']?>">
</td>
<td>
<select name="Prod[<?php echo $x?>]['vendor']">
<?php echo Generate_Vendor($row['Vendor_name']); ?>
</select>
</td>
<td>
<input type="text" hidden name="Prod[<?php echo $x?>]['last_update']" value="<?php echo $row['Employee_name']?>">
<?php echo $row['Employee_name']?>
</td>
</tr>
<?php
$x++;
}
?>
</table>
<button type="submit" name="Prod['btnUpdate']" value="'Sumbitted Form'"> Update Product </button>
ANSWER
in a code like this name="Prod[<?php echo $x?>]['id']" right in the id part... that's the typo.. you should not put ( ' ' ) single-qoute on the array because it will generate an array like the one above where you could not access it because it is a string inside the key...
in simple words you should not create an array with a stringed key
The error here was that I was creating a parameterized array in a multi-Dimensional Array using brackets..
If you ever create a parameterized array don't do this
<input name="person['name']" ...
this wont work because the quotes are included in the key in the array.
To access these keys, you would need something like
$_POST['person']["'name'"]
which is unintuitive. Keys in form element names should only be an index like 0,1,2,3 or a word but not a quoted string.
Do it like this instead
<input name="person[name]" ...

In codeigniter, how will i know which checkboxes are checked?

I have a dynamic number of checkbox in my view,like this:
<?php foreach($documents as $row): ?>
<input type="checkbox" name="options[]" value="<?php echo $row->docu_title?>"><?php echo $row->docu_title?><?php endforeach; ?>
And I set the rule for this group of checkboxes to be required in my controller:
$this->form_validation->set_rules('options[]','options', 'required');
How will i know which checkboxes are checked? so whenever there are errors on the other fields i can still show the user the checkboxes that has been checked already. like this:
<input style="" type="text" class="form-control" name="ClientName" id="ClientName" value="<?php echo set_value('ClientName'); ?>">
You could use form helper 's set_checkbox() function.
This permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
<input style="" type="checkbox" class="form-control" name="ClientName" id="ClientName"
value="<?php echo set_value('ClientName'); ?>" <?php echo set_checkbox('ClientName', '1'); ?> />
For reference visit CodeIgniter User Guide Version 2.2.0
should be something like this
<?php foreach($documents as $row): ?>
<input type="checkbox" name="option[]" value="<?php echo $row->docu_title?>" <?php echo set_checkbox('option[]', $row->docu_title); ?>>
<?php echo $row->docu_title?>
<?php endforeach; ?>

Codeigniter set_value() With Dynamic Multidimensional Array Inputs

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...
}

pass value from page 1 to page 2 and display the value

this my id num column with a value
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
how can to pass value from column txtEmpID to another page(reset.php) and display the value in the reset.php
this is reset.php
<tr>
<td align="right">Employee ID </td>
<td align="left"><input name="staffid" type="text" class="textfield_login"
value="how to display the value from 1st page into here?"/></td>
</tr>
help me please
Wrap the elements in a form and post the form a page.Then you can access the post data via $_POST if your method is post and $_GET if your method in form is get.
<form method="post" action="reset.php">
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
</td>
</form>
In reset.php
echo $_POST['txtEmpID'];//Will contain the value of txtEmpID
There are two ways I can think of to do this.
The first is to use a form.
<form action="reset.php" method="get">
<input type="text" id="txtEmployeeId2" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
<input type="submit" value="Submit">
</form>
The second
is to use Javascript/jQuery to get the value and pass it through the URL as an equivalent GET request.
However, you will need some sort of trigger to call the javascript function, like a button, for example.
<button onclick="sendValue()">Submit</button>
<script type="text/javascript">
function sendValue()
{
parent.location = 'reset.php?txtEmployeeId2='+document.getElementById("txtEmployeeId2").value;
}
Note that this will require you to add the ID attribute to your textbox.
For both solutions, you simply have to change the line in your reset.php file:
This is assuming that both the name attribute of your input and the ID attribute are the same. Replace "txtEmployeeId2" with whatever name or ID you want to give to the text box inputs.
<input name="staffid" type="text" class="textfield_login" value="how to display the value from 1st page into here?"/>
To
<input name="staffid" type="text" class="textfield_login" value="<?php echo $_GET["txtEmployeeId2"];?>"/>
You can also use session. ($_SESSION superglobal array)

PHP post two values in one field, one of which is hidden

Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}

Categories