Multi-dimensional form $_POST data keys not set - php

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]" ...

Related

set_checkbox value not returned

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

Submit html table data with via form (post)

I want to post this html table on submit button click.
01 - I want to retrieve the data of this table in php code (server side)
02 - I also want to display this table on another page.
I'm using
PHP, JQuery
I have html table with many rows in form tag.
<form id="frm_test" class="form-vertical" method="post">
<table id="mytable">
<tr>
<td>
<input type="text" name="color_1" value="" />
</td>
<td>
<input type="text" name="color_2" value="" />
</td>
<td>
<input type="text" name="color_3" value="" />
</td>
<td>
<input type="text" name="color_4" value="" />
</td>
</tr>
......
......
......
......
.....
</table>
<input type="submit" name="submit" value="submit" />
</form>
===========
there are 4 input fields in each row (4 cells in each row). and hundred rows in table. So if I get value via name in php code then I have to write lot of code to get 100(rows) * 4 (input fields) = 400 inputs. So my question was "What is the best way to achieve this"
Since you are trying to submit multiple rows of inputs/selects with the same 'name' attribute to a backend, you would need to add square brackets [] to the end of the name value in those inputs (in the table rows).
<form>
<input type="number" name="examplevar" />
<table>
<tr>
<td><input type="text" name="color_1[]" /></td>
<td><input type="text" name="color_2[]" /></td>
</tr>
<tr>
<td><input type="text" name="color_1[]" /></td>
<td><input type="text" name="color_2[]" /></td>
</tr>
<table>
<input type="submit" />
</form>
This tells your browser to create an array for that name property.
In php, read it as $_POST['color_1'][0] and $_POST['color_2'][0], and loop as you'd like.
The brackets are in Phil Bailey's answer, but they are not pointed out.
(Added because a recent search led me to this)
To post a form you need to add the action tag which termines the path to go when the form is submitted
<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post">
When you want to POST values you should specify input fields with a certain name. If you only want the table is visible you should use the type hidden so the form will POST data, but the inputs aren't visible.
<tr>
<td>
My value
<input type="hidden" name="myValue" value="My value" />
</td>
</tr>
Once your form is posted you can catch the POST data in that PHP file like this:
//THE_PHP_FILE_TO_POST.php
if(isset($_POST))
{
$myValue = $_POST['myValue']; //Contains the string "My value"
//Do something with your POST
}
EDIT Get all table data
if(isset($_POST))
{
foreach($_POST as $inputName => $inputValue)
{
echo $inputName; //This is the name of an input field
echo $inputValue; //This is the value of the input field
}
}
PHP has a an odd naming convention for for table input fields you need to specify the name for the file as an array. For the following SQL Statement using PDO one processing method.
select id,color_1,color_2,color_3,color_4 from colors;
<?php
// Get data
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_NUM);
reset($results);
echo '<table>';
while (list(, $i = each($results)) {
echo '<tr><td><input type=hidden name="id[]" value="' . $i[0] . '"></td>';
echo '<td><input type=text name="color_1[]" value="' . $i[1] . '"></td>';
echo '<td><input type=text name="color_2[]" value="' . $i[2] . '"></td>';
echo '<td><input type=text name="color_3[]" value="' . $i[3] . '"></td>';
echo '<td><input type=text name="color_4[]" value="' . $i[4] . '"></td>';
echo '</tr>';
}
echo '</table>
?>
Simplified $_POST print_r output for 4 records:
Array ( [[id] => Array ( [0] => 20 [1] => 21 [2] => 44 [3] => 45 )
[color_1] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue )
[color_2] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red )
[color_3] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue )
[color_4] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red ) )
You need to add input field in your table to post data.
like
<input type="text" name="fieldname" value="" />
If you do not want to display field then you can make field hidden by using type="hidden"
Add action attribute in your form.
Action attribute indicates the url on which your data will be posted.

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

get data from url when multiple exist with same name

I have a form with checkboxes that posts data to the url for next page. I know this is not secure and is not being used in any project. This is only for learning purposes only.
I'm pushing tasks which are pulled from my database.
My url looks like this:
submitted.php?id=?12345
Depending on how many of the task checkboxes I check, I need it to push the IDs to next page.
here is my form:
<form action="Billing.php?id=<?PHP echo $customerid;?>" method="post">
<?php $query = "SELECT * FROM Service";
$result = mysqli_query($con,$query);
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { ?>
<input type="checkbox" name="service" value="<?php echo $line['Service_ID']?>"><?php echo $line['Service_Type']?> <br> <?php } ?>
<input type="submit" name="next" value="Check Out" style="bottom:3%;Right:3%;position:absolute;background-color:#B2B2B2;font-weight:bold;border-style:double;border-color:black;float:right;" >
</form>
</div><!--serviclist-->
<div id="cust">
<h3>Customer Information</h3>
<table style="left: 7%;right: 5%;top: 20%;position: absolute;">
<tr>
<td>Name:<input class="rightText" name="customername" type="text" value="<?PHP echo $name; ?> "></td>
</tr>
<tr>
<td>Make: <input name="make" type="text" value="<?PHP echo $make; ?>"></td><td>Model: <input class="rightText" name="model" type="text" value="<?PHP echo $model; ?>"></td>
</tr>
<tr>
<td>Color: <input name="color" type="text" value="<?PHP echo $color; ?>" ></td><td>VIN: <br><input class="rightText" name="VIN" type="text" value="<?PHP echo $vin; ?>" ></td>
</tr>
</table>
basically im trying to get it to post my customer id and my services that were checked to next page.
I don't know how you managed to end with this query string, but let's assume you have:
index.php?asd=1&asd=2&asd=3
The you can catch it via
var_dump($_SERVER['QUERY_STRING']);
Output
string 'asd=1&asd=2&asd=3' (length=17)
Then split it by & delimiter
var_dump(explode('&', $_SERVER['QUERY_STRING']));
Output:
array (size=3)
0 => string 'asd=1' (length=5)
1 => string 'asd=2' (length=5)
2 => string 'asd=3' (length=5)
Then split by = to find key=>value pairs.
However, this approach I find very unattractive and inefficient.
If you have control over the checkbox names, then build the query string like:
index.php?asd[]=1&asd[]=2&asd[]=3
Then
var_dump($_GET['asd']);
will return array
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
You wrote your url wrong, it should look like :
submitted.php?taskid[]=1&taskid[]=7
put checkbox as an ARRAY,
example
input type="checkbox" name="taskid[]" value="any value you like"
and in server side php, use a FOR-EACH LOOP to fetch all taskids
<?php
$tasks = $_GET['taskid'];
foreach($tasks as $values)
{
// do your operations here
echo $values;
}

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