Different elements with the same name in PHP - php

In PHP, I would like to know how to provide the same name to different elements and still echo them out individually. I think the array mechanism works here but I don’t know how.
echo "<input type='checkbox' name='seat[]' id='something'/>";
echo "<input type='checkbox' name='seat[]' id='something1'/>"
Then to echo their values out I use the following:
<?php
if(isset($_POST['seat[]']))
{
echo ' ', $_POST['seat[]'] ;
}
?>
Please guide me!!

It's just an array of data. You can access elements of it just like any other array:
foreach ($_POST['seat'] as $seat) {
echo $seat . "<br>\n";
}
or using a numerical index:
echo $_POST['seat'][0]; // value of the first submitted checkbox

Related

form post of values in array and manage them divided by key

I dont know how to manage this situation, I'm a noob coder, I have a page that shows you all available lots where you can unload a specific item from that lot.
This is the foreach that prints out:
$lotto, $totalelotto, $data, and ask for qtyvalue to unload from $lotto (input can be also NULL)
foreach ($dataslotto as $data) {
$totalelotto = totlotto($database, $data['lotto']);
$lotto = $data["lotto"];
$data = $data["data"];
echo "<tr>";
echo "<td>".$lotto."</td>";
echo "<input type=\"hidden\" value=\"".$lotto."\" name=\"array[]\" />";
echo "<td>".$totalelotto."</td>";
echo "<input type=\"hidden\" value=\"".$totalelotto."\" name=\"array[]\" />";
echo "<td>".$data."</td>";
echo "<input type=\"hidden\" value=\"".$data."\" name=\"array[]\" />";
echo "<td><input type=\"text\" class=\"form-control\" placeholder=\"Qta.\" required name=\"qtyvalue\"></td>";
echo "</tr>";
}
I dont know how to set name="" of input fields (because the number of fields can change if there are many lots) and I dont know how to send $_POST data as array, and then foreach group of $lotto, $totalelotto, $data, $qtyvalue where is set $qtyvalue do another query.
I put it in no regular code, I know it looks bad but it's just for giving you an idea.
$_POST[''formarray];
foreach ( /* values recieved in each <tr> inside formarray where $_POST['qtyvalue'] is not empty */ ){
#EXECUTE THIS
}
Thanks for help!!
And sorry for my bad coding skills.
$_POST is an Associative/Key Value pair, it's key is whatever is set as the inputs name.
so if you wanted to send the users input username to the backend PHP script
You set the value and it's name
<input type="text" name="username" value="User123">
then to retrieve the user name you can do
print_r($_POST["username"]);
to print the value.
So you ask how do you loop over each one in $_POST, that's pretty simple you can could a foreach loop over the entire $_POST array.
foreach($_POST as $key => $value)
{
//check something has been entered for the current value we are iterating over
if($value != null)
{
print_r($key . " value is : " . $value);
}
}
this would loop over each item in the $_POST array with key being set to whatever the DOM elements name is.
Don't forget the $_POST array is just that an array, you could do
var_dump($_POST);
and see everything that was sent in the POST request.
You can use array names for your inputs. Say you have a row of your table like this:
<tr>
<td><input ... name="lotto[]"></td>
<td><input ... name="totalelotto[]"></td>
<td><input ... name="data[]"></td>
<td><input ... name="qtyvalue[]"></td>
</tr>
Then you will get arrays $_POST['lotto'], $_POST['totalelotto'] and so on, each with the same number of elements, and elements with same index belonging to one row of the table. You could then process those elements like this
foreach ($_POST['lotto'] as $i=>$lotto) {
if ($_POST['qtyvalue'][$i] > 0) {
...
}
}

Radio button set's array created dynamically with echo in php. How to get their values?

I've created a php page which retrieves question and options(as MCQ's) from the database and in that Radio buttons are created dynamically using echo()The name for each radio button group is assigned into an array from which one by one index's value is set to the name attribute.Example:
$result=mysqli_query($db,$sql);
$numrows=mysqli_num_rows($result);//gets the number of questions
$radiogrp_name=array();
for($i=0;$i<$numrows;$i++){ //creating array of names for each set of radio buttons
$radiogrp_name[$i]="q".$i;
}
$i=0;
while(($myrow=mysqli_fetch_array($result)) && ($i<$numrows)){
echo $myrow["q_no"].". ";
echo $myrow["ques"]."<br><br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='a'/>".$myrow["A"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='b'/>".$myrow["B"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='c'/>".$myrow["C"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='d'/>".$myrow["D"]."<br><br><br>";
$i++;
}
How would I get the selected radio buttons and set them into a SESSION variable? Can anyone help with this? Or any other way to implement this?Thank you in advance!
On the page that the form is POST'ed to you are going to need to look through the $_POST array and strip out the relevant answers. Ideally you would have a list of expected indexes to drive that page, but if not you could just try to pattern match the indexes.
Something like:
$results = array();
foreach($_POST as $key => $value){
if(preg_match("/q(\d{1,3})/", $key, $matches)){
$results[$matches[1]] = $value;
}
}
results would then contain an array of values indexed by the numeral in name="q#" and you would just set it to the session or do with it whatever you need.
EDIT
by the way, you'll need to wrap your string inclusion in curlies since it is an array.
echo "<input type='radio' name='{$radiogrp_name[$i]}' value='d'/>"

foreach loop through specific $request

I have a form that has several input types all that need to be a handled differently. I did a loop to name them like so in the form:
product-0-length
product-0-size
product-1-length
product-1-size
product-2-length
product-2-size
On my processing php (where the form info gets sent) I want to iterate a loop to handle say size different from length. My thought was this but no luck:
<?php
$i = 0;
foreach($_REQUEST['product-'.$i.'-length'] as $key => $val) {
//style or do what I need with the length information for each product
echo '<li>'.$key.'='.$val .'</li>';
$i++;
}
?>
As I suggested in the comments above you could use a different naming scheme for your input fields. Take a look at this example for the form creation:
<?php
foreach ($i=0; $i<3; $i++) {
echo "<input type=\"text\" name=\"product[$i][length]\">\n";
echo "<input type=\"text\" name=\"product[$i][size]\">\n";
}
When submitting this form the server will translate the notation into a php array which is a very convenient thing. You can simply iterate over that array:
<?php
foreach ($_POST['product'] as $key=>$product) {
echo "<li>Length of product $key: " . $product['length'] . "</li>\n";
echo "<li>Size of product $key: " . $product['size'] . "</li>\n";
}
Note that this is a very primitive example, all error checking and the like is missing to keep things crunch. Also I did not test this code here, it is just meant to point you into the right direction, but I hope there are not too many typos in there...

Unsure how to get $_POST[''] value from form when the name is dynamic?

So at the moment I have the following code which is correct but I'm just unsure how I will be able to get the name=".$job_id." as it will be dynamic, applicant-jobs.php below:
<div id="container">
<?php
foreach($jobs as $job){
$job_id = $job['id'];
echo form_open('applications/applicants');
echo "<div class=\"job\">";
echo $job['name'];
echo "</div>";
echo "<input type=\"hidden\" name=".$job_id.">";
echo form_submit('submit', 'View Applicants');
echo form_close();
}
?>
Any help is greatly appreciated, many thanks. P.S. I'm using codeigniter.
You can iterate arrays by key value pairs.
foreach($_POST as $key => $val)
{
}
Read more on the docs here; http://php.net/manual/en/control-structures.foreach.php
Edit, I slightly misunderstood your question. What you're looking for is the value attribute of the <input> element:
echo "<input type=\"hidden\" name=\"job\" value=\"".$job_id."\">";
Then in PHP you just access it like:
$job_id = $_POST['job'];
By the looks of it, the name doesn't need to be dynamic at all. You're only using one per form.
The name can be static, and have the value dynamic.
ex:
echo "<input type=\"hidden\" name=\"job_id\" value=\"".$job_id."\" />";
it can be accessed via $_POST['job_id']
Don't you want to set the value rather than the name?
eg.
echo "<input type=\"hidden\" name=\"job_id\" value =\"".$job_id."\">";
Also you've got the from open and close within the loop, i think you want them outside the loop otherwise you have multiple forms and will only end up submitting one.

Passing variable/array through several pages in php

my code starts with multiple php arrays with XML data being passed through multiple checkboxes in the following way($i cycles through items in XML doc, values pass specific data in row i that is selected):
$MoneyLine = $event->periods->period[0]->moneyline; //background info
$AwayMoneyLine[] = $MoneyLine->moneyline_visiting; //background info
<input type='checkbox' name='AwayMoneyLine' value='$Date[$i];$AwayRotNum[$i];$AwayParticipantName[$i];$ATotalPoints[$i]'/>
On my next page, I pass the variables in the following manner, but they are not resulting on the next page.:
if(isset($_POST['AwayMoneyLine'])){
foreach($_POST['AwayMoneyLine'] as $value) {
$d = explode(';',$value);
echo '<input type="hidden" name="hidden[]" value="$d[0];$d[1];$d[2];$d[3];$d[4]">';
Here is how I'm attempting to get the data on the next page. Any suggestions on how I can pass the variables through to this page? On the form (also on var_dump of $d), I get $d[0], $d[1], etc. Any help is greatly appreciated!:
foreach($_POST['hidden'] as $value) {
$f = explode(';',$value);
echo 'Here is your following bet:';
echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="#585858" width=100%>';
echo "<tr><td>$e[0]</td><td>$f[0]</td><td>$f[1]</td><td>$f[2]</td><td>$f[3]</td><td>$f[4]</td></tr>";
}
echo '</table>';
Change the code to:
echo "<input type=\"hidden\" name=\"hidden[]\" value=\"$d[0];$d[1];$d[2];$d[3];$d[4]\">";
$d[0];$d[1];$d[2];$d[3];$d[4] will not be printed as you put inside single quote. You should use double quote and escape attributes like type=\"hidden\", name=\"hidden[]\", etc.,
eg:
<?php
$name = 'foo';
echo "$name";
echo '<br />';
echo '$name';
?>
Output:
foo
$name
use $serial=serialize($array);
echo "<input type=\"hidden\" name=\"hiddenfield\" value=\"<?php echo$serial; ?>">";

Categories