I have an input :
<input type="text" name="input['.$opt_id.']">
and I can get $opt_id value on php side with :
foreach ($_POST['input'] AS $key => $value)
{
$opt_id=$value;
}
but I want to get second value like this :
<input type="text" name=input"['.$opt_id.']['.$lang_id.']">
How can I get $opt_id and $lang_id? I want to insert them on different columns in the database.
Assuming that you don't have 2 entries having the same opt_id and lang_id then you can use a single key instead of 2:
HTML:
<input type="text" name="input[<?php echo "{$opt_id}_{$lang_id}"; ?>]" />
PHP:
foreach ($_POST['input'] as $optIdAndLangId => $value) {
list($opt_id, $lang_id) = explode('_', $optIdAndLangId);
}
Within HTML markup you should insert PHP variables or any other PHP code in such way:
<input type="text" name="input[<?php echo $opt_id; ?>]">
...
<input type="text" name=input"[<?php echo $opt_id; ?>][<?php echo $lang_id; ?>]">
Try Like This
you can process the data with something like this:
<?php
foreach($_POST['input'] as $key => $opt_id){
foreach($opt_id as $ans=>$lang_id){
echo 'option id :'.$ans.' Lang Id : '.$lang_id;
}
}
Related
If i had a form input field such as
<input type="text" name="name">
i know i can name the field dynamically like this
<?php
$name = 'name';
?>
<input type="text" name="<?php echo $name; ?>">
i can also add a text string after the variable like this
<input type="text" name="<?php echo $name; ?>secondname">
that would now be called 'namesecondname'
what i need to know is if this is possible to get in codeigniter controller after the form has been submitted.
for example lets assume this is my form
<?php
$hello = 'hello';
?>
<input type="hidden" name="somename" value="<?php echo $hello; ?>">
<input type="text" name="<?php echo $hello; ?>world" value="some value">
My field name is now 'helloworld' then in my controller i want to get it like this :
$var = $this->input->post('somename');
$field = $this->input->post($var.'world');
$field = 'some value' but how do i use a variable and text dynamically as the inputs name?
you can use $_POST. It will get all the post value, then you can iterate to get name & value
$postdata = $_POST;
foreach($postdata as $key => $value)
{
//key is your input name, and value is the input value
//do your operation
}
i have multiple input elements like:
<input type="text" name="attribute_name[attr_1]" placeholder="Attribute Name" class="form-control" required="">
<input type="text" name="attribute_name[attr_2]" placeholder="Attribute Name" class="form-control" required="">
..
..
Now i want to loop through all the input elements and i want to get array key also, i.e. attr_1 in this case :
i'm using the following code but it is not getting key:
foreach($request->input('attribute_name.*') as $key => $val)
{
print_r($key);
print_r($val);
}
I agree with #lagbox you have to use attribute_names instead of attribute_names.*
foreach($this->request('attribute_names') as $key => $value) {
print_r($key);
}
As the attribute_names itself an array while defining it under input box you just have to use it under foreach and you will easily get the key of each input boxes.
The short example of dynamic input :
<form action="" method="post">
<input type="text" name="attribute_name[]" value="One">
<input type="text" name="attribute_name[]" value="Two">
<input type="text" name="attribute_name[]" value="Three">
<input type="submit" value="Submit">
</form>
Then in your controller you will be able to get all attribute_name values like this :
$attribute_name = $request->attribute_name; // give you an array with values
You can get all data by a foreach loop :
foreach($request->attribute_name as $key => $value) {
echo "Key : " . $key . ", Value : ". $value . "<br>";
}
Output :
Key : 1, Value : One
Key : 2, Value : Two
Key : 3, Value : Three
You should use name 'attribute_names' to get array not 'attribute_name.*' try the following code
$requestData = $request->input('attribute_names');
foreach($requestData as $key => $val)
{
print_r($key);
print_r($val);
}
Here, I want to get value of textbox, but I coudn't.
Where am I wrong?
Is it must to use session if i want to get value of textbox?
<input type="submit" id="processorder" name="processorder" class="submit-green" value="Process Order"/>
<?php
foreach ($order_list as $row)
{
?>
<td class="align-center">
<input type="text" name="text[]" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
</td>
</tr>
<?php
$i++;
}
?>
if(isset($_POST['processorder']))
{
$txtvalue = $_GET['text[]'];
echo "helo".print_r($txtvalue);
}
this is wrong,
$txtvalue = $_GET['text[]'];
it should be
$txtvalue = $_GET['text'];
^ no [] here
Replace $_GET['text[]'] to $_GET['text']
The field would not be called text[] to PHP. It would just be called text, but would be an array instead of a string.
Try this:
if(isset($_POST['processorder']))
{
$arrayvalue = $_GET['text'];
foreach($arrayvalue as $txtvalue){
echo "helo" . $txtvalue . "<br>";
}
}
As your text input name is text[] with a bracket, it indicates that it is sending as an array, so to get the value you need
if (!empty($_POST["test"][0]) ) {
$test_value = $_POST["test"][0];
}
I am using 0 here since your code only shows one input text, for the server-end will only receive one array value
why you use array name="text[]" if you don't want to get multiple data using "text" than make it name="text"
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'] // get value
or
you want to multiple data using same name "Array"(mean using array)
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'][0] // 0 is a index value
I have inherited some code which contains hidden inputs with multiple square brackets - example below:
<input name="myinput[1]['sausages'][2][]" type="hidden" />
<input name="myinput[1]['bacon'][1][]" type="hidden" />
<input name="myinput[2]['steak'][1][]" type="hidden" />
<input name="myinput[2]['mince'][2][]" type="hidden" />
The first value is referring to a 'type' field in the database, the second is the 'item' and the third is the 'order'
How can I extract this info using a for loop in php?
you can use a for-each to travel
foreach($myinput as $temp)
{
foreach($temp as $a)
{
echo $a;
}
echo "\n";
}
or if you want to read data using it's key use like this..,
foreach($myinput as $temp)
{
echo $temp['type'];
echo $temp['item'];
echo $temp['order'];
echo "\n";
}
I would like to post data in checkboxes, as like
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[<?php echo $foreignid; ?>]"
value="<?php echo $id;?>"
/>
If I go this way only one key-value pair remains in my $_POST array.
If I leave the photoids[] array empty, (not echoing out the $foreignid) all of the key-value pairs remains in $_POST array, but then i don't have access to the $foreignid variable which that I need in my code.
What is the best workaround for this problem?
Use this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids_<?php echo $foreignid; ?>"
value="<?php echo $id;?>"
/>
And then parse keys in $_POST array that begin with photoids to obtain foreignid.
Or use this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[]"
value="<?php echo $foreignid . '_' . $id;?>"
/>
And then parse values to obtain foreignid and id.
Of course, assuming that underscore '_' will not show up in foreignid or id.
You can parse name or value using:
$src = '123_987';
$arr = explode('_', $src);
$arr[0] will contain 123 and $arr[1] will contain 987.
Maybe something like this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[<?php echo $foreignid; ?>]"
value="<?php echo $foreignid."/".$id."/".$data;?>"
/>