get data from url when multiple exist with same name - php

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

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.

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

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

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/>';
}
}

How can I get value of a text box which is looped using post method to insert value in database?

I had following code.My textbox attend is looped for number of times.I want to get value of each attend when my form is post to insert value in database.
<form method="post" action="insert.php">
<label>Subject</label>
<input type="text" maxlength="30" name="subject" />
<label>Total Lectures</label>
<input type="text" maxlength="30" name="total" />
<table width="537" border="1">
<tr>
<td width="90">Name</td>
<td width="139">Roll Number </td>
<td width="136"> </td>
<td width="144">Attendence</td>
</tr>
<?php
$query=mysql_query("SELECT * FROM STUDENT");
$i=0;
while($rec=mysql_fetch_array($query)){
$i++;
?>
<tr>
<td><?php echo $rec['name']; ?></td>
<td><?php echo $rec['roll_number']; ?></td>
<td> </td>
<td><input type="text" maxlength="10" name="atten" /></td>
</tr>
<?php } ?>
</table>
<input type="submit" value="submit" />
</form>
and my insert.php page is
if($_SERVER['REQUEST_METHOD']=='POST'){
$query=mysql_query("SELECT * FROM STUDENT");
while($rec=mysql_fetch_array($query)){
$attend=$_POST['atten'];
$subject=$_POST['subject'];
$total=$_POST['total'];
$query1=mysql_query("INSERT INTO course VALUES('$i','$subject','$total','$attend','')") or die(mysql_error());
}
}
I am getting only 1 value of text box.
The problem is that you have many inputs all with the name atten. When you post this form, only one of those values will be carried forward.
If you change the name to atten[] all of the values would be posted as an array, which you would then have to loop through to build you insert query.
You could access this posted array as follows:
$attendee_array = $_POST['atten'];
foreach($attendee_array as $attendee) {
// perform insert or build insert query (prefereable as you can do all these inserts at once) here
// make sure to escape your data for input
}
Change the names of your inputs:
<input name="subject[]" value="Lorem" />
<input name="total[]" value="ipsum" />
<input name="atten[]" value="dolor" />
Then:
$_POST['subject'][0] == 'Lorem'
$_POST['total'][4] == 'amet'
If so, that would make my life ten times easier, as I could send an
indefinite amount of information through a form and get it processed
by the server simply by looping through the array of items with the
name "subject". etc.
You need to use input array like:
<input type="text" maxlength="10" name="attend[]" />
And then you will get all the values of attend in an array and you can loop through that $_POST['attend'].

Categories