This is the HTML:
<input type="text" name="shortcut[]" value="a"/> do <input type="text" name="ses[]" value="1" disabled/><br>
<input type="text" name="shortcut[]" value="b"/> do <input type="text" name="ses[]" value="2" disabled/><br>
<input type="text" name="shortcut[]" value="c"/> do <input type="text" name="ses[]" value="3" disabled/><br>
How do I pass the values to PHP but connect the indexes of both arrays?
i.e. put in database value 1 where something = a,
put in database value 2 where something = b
and so on ...
The indexes are connected automatically, since they're numeric arrays.
$nvals = count($_REQUEST['shortcut']);
for ($i = 0; $i < $nvals; $i++) {
// do something with $_REQUEST['shortcut'][$i] and $_REQUEST['ses'][$i]
}
Combined array: array_map(null,$_POST['shortcut'],$_POST['ses']);
But you could of course could foreach over one of the 2, and fetch the other by key.
Note that if you have elements which may or may not be sent (checkboxes for instance), the only way to keep groups together is to assign them a number beforehand (name=sess[1], name=sess[2], etc.)
You can specify shortcut value as the key and the ses value as the value attribute:
<input type="text" name="input[a]" value="1" />
<input type="text" name="input[b]" value="2" />
<input type="text" name="input[c]" value="3" />
On the server-side you could use a foreach loop to iterate over the array:
foreach ($_POST['input'] as $shortcut => $ses) {
// process $shortcut and $ses
}
Related
Im trying to get all of the checked values using array i already get all of the checked box and get the value but my problem is i want to pass to array all of value of checbox, 1 for check and 0 for uncheck. sorry for the english.
this is what i want to get:
array[0] = 1 //check
array[1] = 0 // uncheck
// and so on.
i have already tried a hidden box with same name of checkbox but it give me wrong data of array and i am sure its not the answer
<input type = "checkbox" name="checkbox[]" value="1">
$checkbox1 = ($_POST['checkbox'] <> 0) ? ($_POST['checkbox']) : (empty($_POST['checkbox'])) ?'0' : '';
output: 0
output of print_r($checkbox1) : 0
expected output : array[0]=> 1 --> check array[1] => 0 -> uncheck
When working with a PHP backend where you want to post an array of values (using the name="something[]" syntax) you need to create <input type="hidden"> and <input type="checkbox"> pairs that will refer to the exact same array index.
You do so by explicitly defining the index in the name attribute.
For example
<input type="hidden" name="checkbox[0]" value="0">
<input type="checkbox" name="checkbox[0]" value="1">
<input type="hidden" name="checkbox[1]" value="0">
<input type="checkbox" name="checkbox[1]" value="1">
<input type="hidden" name="checkbox[2]" value="0">
<input type="checkbox" name="checkbox[2]" value="1">
I believe this also works for ASP.NET-MVC backends. Not sure about others though.
If you're looping over records or a range, this is as simple as keeping an iteration index and using it in the name attributes
<?php for ($i = 0; $i < $range; $i++): ?>
<input type="hidden" name="checkbox[<?= $i ?>]" value="0">
<input type="checkbox" name="checkbox[<?= $i ?>]" value="1">
<?php endfor ?>
I have a form that may submit 1 line item or 200 to quote. using FPDF to create results and everything is working perfectly, but I was looking for a way to automate picking up post values. So far I have been doing them manually, which is very difficult to make code changes:
//conditional statement to verify values exist, otherwise we write a blank line
$item=$_POST['item3'];
$uprice=$_POST['uprice3'];
$udprice=$_POST['udprice3'];
$quan=$_POST['quan3'];
//add values to report
//repeat for next result
$item=$_POST['item4'];
$uprice=$_POST['uprice4'];
$udprice=$_POST['udprice4'];
$quan=$_POST['quan4'];
I was wondering if there is a way to add a variable inside the post value, like $_POST[$nextitem]?
You can loop until the next number is not available anymore:
<?php
$i = 1;
while (isset($_POST['item' . $i])) {
$item = $_POST['item' . $i];
$uprice = $_POST['uprice' . $i];
$udprice = $_POST['udprice' . $i];
$quan = $_POST['quan' . $i];
// do your stuff
$i++;
}
concatenating a string 'var' with int 1 will result in a string 'var1' in PHP.
I would just grab the entire array with $array = $_POST and then use foreach() loops to manipulate it later.
Alternatively, it sounds like you have an arbitrary number of things on your form. Do it like this:
<input type="text" name="item[]" />
<input type="text" name="quan[]" />
<input type="text" name="udprice[]" />
<input type="text" name="item[]" />
<input type="text" name="quan[]" />
<input type="text" name="udprice[]" />
<input type="text" name="item[]" />
<input type="text" name="quan[]" />
<input type="text" name="udprice[]" />
<input type="text" name="item[]" />
<input type="text" name="quan[]" />
<input type="text" name="udprice[]" />
You can see more about this in this very helpful answer: HERE
When you submit this arbitrary number of fields, you will end up with a nice multi-dimensional array. Iterate over it like this:
$i = count($_POST['item']);
$payload = array();
while ($i <= count($_POST['item']) {
$payload[] = array(
'item' => $_POST['item'][$i],
'udprice' => $_POST['item'][$i],
'quan' => $_POST['quan'][$i]
);
$i++;
}
Now $payload is a fancy array that can be inserted into databases and such.
This question already has answers here:
php: check if an array has duplicates
(17 answers)
Closed 7 months ago.
is there a way to write an if else statement by the input field name?
I have a loop that dynamically creates 9 input field,
The loop
for($i=0; $i<($n*$n); $i++){
echo "<tr>";
for($j=0; $j<($n*$n); $j++){
$number = "column".$i.$j;
if($i%$n==0 && $j%$n==0 && $j!==0 && $i!==0){
echo "<td><input class='field' type='text' name=$number value=$_POST[$number]></td>";
}
and the output is this.
<input class="field" type="text" name="column00" value="1">
<input class="field" type="text" name="column01" value="2">
<input class="field" type="text" name="column02" value="1">
.....
<input class="field" type="text" name="column09" value="3">
So what I am trying to do is, that if there is a inputed number on row 1 to 9 that is equal to another number in row 1 to 9 it will echo out that there more than one number thats equal to each other.
I tried something like this but it wouldn't work.
if($number==$number){
echo = "equal number in the same row";
}
Make an array of all the inputs from a row. Then do:
$unique = array_unique($row_values);
if (count($unique) != count($row_values)) {
echo "No duplicate numbers in a row!";
}
Do the same thing for each column.
Use field names in this form:
<input name="number[<row>][<column>]" ...
So, for instance:
<input name="number[0][0]" value="1" />
<input name="number[0][1]" value="2" />
<input name="number[0][2]" value="5" />
<input name="number[0][3]" value="3" />
...
<input name="number[1][0]" value="4" />
<input name="number[1][1]" value="9" />
When posted, you will receive a two-dimensional array:
[[1, 2, 5, 3, ...], [4, 9, ...]]
Now it basically becomes an array problem you have to solve for:
All rows and columns
Rows and columns within each grid
To help you find the value that occurs multiple times in an array:
$multiples = array_filter(array_count_values($arr), function($freq) {
return $freq > 1;
});
// outputs a non-empty array with the numbers that occur more than once.
print_r(array_keys($multiples));
I want insert following data by insert_batch as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?
I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
Refer to http://codeigniter.com/user_guide/database/helpers.html
The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.
LOL, it's not pretty, but it might work sometimes:
<input name="u_id[]" value="76">
<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">
<input name="u_id[]" value="77">
<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">
<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">
$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
for($ii=0;$ii<count($un[$i]);$ii++){
(count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
(count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
(count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
$this->db->insert('units', array(//use db insert here
'u_id'=>$u_id[$i][0],
'un'=>$unn,
'ue'=>$uen,
'up'=>$upn,
));
}
}
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.
I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>