I have an array of inputs similar to below
<input name="attr[]" value = "a" type="text" />
<input name="attr[]" value = "b" type="text" />
<input name="attr[]" value = "c" type="text" />
On the server side I am using a foreach loop to fetch the value entered. However, I want the values a ,b and c as well. How do i do that in PHP?
I did something like this but its not working for me. The $key returned is just an index.
foreach($_POST['attr'] as $key=>$val)
{
//process each $key and $val
}
use this code for retrieving values
foreach($_POST['attr'] as $key=>$val)
{
echo $val;
}
Related
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);
}
I have a problem with <form> in php .
i have uncertain number of fields (inputs) . it may be 1 or 100 input field .
Is there any function or class to get uncertain number of fields ?
Try that :
echo '<form>';
foreach ($data as $value) {
echo '<input name="field[]" value="',$value,'">';
}
echo '<input type="submit"></form>';
If you send that form, $_POST['field'] will be an indexed array in which every entry will correspond to one of the inputs.
$_POST would contain all the fields from the form on submit
print_r($_POST) will display them in an array for you
You can use an array name for the input fields in your form. For example, you can make an array of title fields by adding [] after the name:
<input type="text" name="title[]" />
<input type="text" name="title[]" />
<input type="text" name="title[]" />
Now in your PHP code, this value will be an array containing an amount of values equal to the number of fields with this name. The following code would print all titles on separate lines:
foreach ($_REQUEST['title'] as $value)
echo $value . "\n";
Just count the $_POST or $_GET what you use.
<?php
if(isset($_POST['submit'])){
echo "The total number of input fields is";
echo count($_POST); // include submit also
}
?>
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;
}
}
I'm making a todo list and I've got everything working except for this one thing. I need to loop over inputs that's been submitted via a form, these inputs have the same name so what I've done is storing them as an array. Now I need to loop over them so I can send them into the database one by one. Here's what I tried:
if (isset($_POST['submit'])) {
$labelValues = $_POST['labelValue[]'];
$i = 0;
while($i < sizeof($labelValues)) {
$stmt = $db->prepare("INSERT INTO tenta_table (text) VALUES (:text)");
$stmt->bindParam(':text', $labelValues[$i]);
$stmt->execute();
$i++;
}
}
HTML, the inputs are marked with red:
But it doesn't seem to work, it's not giving me any errors so I have nothing to go on. Where am I going wrong here?
Your $_POST['labelValue'] will already be an array if you have named your inputs correctly, something like <input type="text" name="labelValue[]" /> would create and array called labelValue in your POST.
From there you should be able to use your current code with one minor change
if (isset($_POST['submit'])) {
$labelValues = $_POST['labelValue'];
$i = 0;
while($i < sizeof($labelValues)) {
$stmt = $db->prepare("INSERT INTO tenta_table (text) VALUES (:text)");
$stmt->bindParam(':text', $labelValues[$i]);
$stmt->execute();
$i++;
}
}
Above I have change $labelValues to equal $_POST['lableValue'] rather than $_POST['labelValue[]']
In your case only the last input element will be available.
If you want multiple inputs with the same name use name="foo[]" for the input name attribute. $_POST will then contain an array for foo with all values from the input elements.
<form method="post">
<input name="a[]" value="foo"/>
<input name="a[]" value="bar"/>
<input name="a[]" value="baz"/>
<input type="submit" />
</form>
The reason why $_POST will only contain the last value if you don't use [] is because PHP will basically just explode and foreach over the raw query string to populate $_POST. When it encounters a name/value pair that already exists, it will overwrite the previous one.
However, you can still access the raw query string like this:
$rawQueryString = file_get_contents('php://input'))
Assuming you have a form like this:
<form method="post">
<input type="hidden" name="a" value="foo"/>
<input type="hidden" name="a" value="bar"/>
<input type="hidden" name="a" value="baz"/>
<input type="submit" />
</form>
the $rawQueryString will then contain a=foo&a=bar&a=baz.
You can then use your own logic to parse this into an array. A naive approach would be
$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
list($key, $value) = explode('=', $keyValuePair);
$post[$key][] = $value;
}
which would then give you an array of arrays for each name in the query string.
or the best and simple approach for this
<form method="post">
<input name="a[0]" value="foo"/>
<input name="a[1]" value="bar"/>
<input name="a[2]" value="baz"/>
<input type="submit" />
</form>
You should replace
$labelValues = $_POST['labelValue[]'];
By
$labelValues = $_POST['labelValue'];
Not Sure, but as far as i remember it should be $labelValues = $_POST['labelValue']. I think your $labelValues is null and you don't even enter your loop. You should do a var_dump( $_POST ) to verify what you're working with.
Iam writing a program where i have a form with two fields and a 'PLUS BUTTON' upon clicking it two more fields will appear. By clicking PLUS Button again two more fields will generate and it continues as many times we click the PLUS BUTTON. Here's my program.
<form action="project_values/action_nowproject.php" method="post"enctype="multipart/form-data"><div id="item"><input type="text" name="add_qty" id="add_vender" class="ttexbox" required="required"><input type="text" name="add_name" class="ttexbox"><input onClick="addRowv(this.form);" type="button"style="cursor:pointer" class="addround" /></div></form>
in Javascript
<script type="text/javascript"> var rowNum = 0; function addRowv(frm) { rowNum ++;
var row = '<p id="rowNum'+rowNum+'"><span class="ftext">Item quantity:</span> <input type="text" name="m_name[]" value="'+frm.add_qty.value+'"><br> <span class="ftext">Item name: </span><input type="text" name="mi_name[]" value="'+frm.add_name.value+'"><br><br /> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
jQuery('#itemRowsv').append(row); frm.add_qty.value = ''; frm.add_name.value = ''; } function removeRow(rnum) { jQuery('#rowNum'+rnum).remove(); } </script>
Now I have to fetch the values of extra fields appeared by clicking the plus button and send them to database.
How to fetch the values multiple array's genereated? heres my sql query insert statement.
$mp = "INSERT INTO pm_manr(name,item_nm)VALUES ('$add_qty','$item_name')";
$updata = mysql_query($mp);
How to get values in $add_qty,$item_name ? Some one pls help me.
Lets asume you have something like this:
line 1 <input name="foo[]" /> <input name="bar[]" />
line 2 <input name="foo[]" /> <input name="bar[]" />
line Y <input name="foo[]" /> <input name="bar[]" />
line Z <input name="foo[]" /> <input name="bar[]" />
You can loop trough them both by using the key from a foreach on the other values:
foreach($_POST['foo'] as $key =>$value){
echo $_POST['foo'][$key]; // the same as echo $value
echo $_POST['bar'][$key]; // the corresponding value of $_POST['bar']
// This is where you add your query
}
Use array_combine() which creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
Try this:
$arr = array_combine($_POST['m_name'],$_POST['mi_name']); // combines both arrays
foreach($arr as $key => $value){
$mp = "INSERT INTO pm_manr(name,item_nm)VALUES ('$key','$value')";
$updata = mysql_query($mp);
}