Looping an array from form element - php

hope you can help me on this..
i have this simple code :
foreach ($by_sellers as $seller_id => $info) {
print_r($info);
echo 'AMOUNT : ' . $info['total'] .'; STORE : '. $info['name'];
}
and the output is this :
Array(
['total'] => 100
['name'] => 'Store Name'
)
AMOUNT : ; STORE :
its really weird coz i cant read the values of the variable if done individually but it display on var_dump & print_r.. those data comes from input forms..
<input name="by_sellers[1]['total']" value="100">
<input name="by_sellers[1]['name']" value="Store Name">
hope someone find the light on this problem.. thanx..

found the solution and its a simple one..
i just edit the html code from this
<input name="by_sellers[1]['total']" value="100">
<input name="by_sellers[1]['name']" value="Store Name">
to this (removing the quotes of the string name)
<input name="by_sellers[1][total]" value="100">
<input name="by_sellers[1][name]" value="Store Name">

Related

Loop through 3 arrays at once and save values in the database

I have three inputs type text in an HTML page and a button which if clicked duplicate each text box (Javascript) making them 6.
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
<button>Add more</button>
Which generate same inputs again:
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
A piece of code in Cakephp I have been trying:
$data = $this->request->data;
foreach($data['category'] as $index => $value){
$this->ModelName->save($value);
}
Trying to get two rows inserted at once with quantity, category and amount as columns. But it is not inserting and not giving any error.
Is there a way I can achieve this?
Thanks.
I'm not sure how your model works in cakephp, but you should be able to get a complete grouping of data like:
foreach($data['category'] as $index => $value){
$category = $value
$quantity = $data['quantity'][$index];
$amount = $data['amount'][$index];
// use the above 3 variables however you need to to persist the model
//$this->ModelName->save($value);
}
On a side note, you may want to consider reordering your html inputs to be like:
<input type="text" name="item[0][category]">
<input type="text" name="item[0][quantity]">
<input type="text" name="item[0][amount]">
And then maintain the next index, incrementing the numeric index of item for each additional group
This will allow you to iterate like:
foreach($data['item'] as $index => $group){
//$group['category'];
//$group['quantity'];
//$group['amount'];
}

How to save a textbox in php as an array?

Is there anyway to save a textboxs as an array similar to how multiple checkboxs can be saved as an array? When you save multiple checkboxs to an array the value only comes up in the array when the checkbox has a check in it. Is this the same with textboxs? Do they only have value when they're not empty?
I'm trying to save these textboxs as an array (I only took the part of the form that you all need to see)
<form action="formemail.php" method="POST" name="rcr">
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="submit" name="sendwork" id="sendwork" value="Send Work Order"></td>
</form>
In the next page I'm trying to take these values and just output them in a simple way (later I'm going to be adding them to an email using foreach if I can)..... below is formemail.php
<?php
$description = $_POST["desc"];
echo $description[0];
echo $description[1];
?>
I can't get anything to echo for the life of me. And I'm not sure what I'm doing wrong.
tried ?
if( isset( $_POST["desc[]"] ) ) echo "is set ; )";
btw. same multiple id's can cause you problems if you work with JS/JQuery.
Try to use foreach and test submitted values:
foreach($_POST['desc'] as $post)
{
echo !empty($post) ? $post.'<br />' : 'An empty value.<br />';
}

PHP Form is returning a Cascading Array

Quick backstory. I have a portion of a simple form that allows the user to add/remove a row of three inputs. I'd thought originally that I would just use jQuery to show/hide static fields, but I poked around and found a solution that seemed to work really well.
(Reference: http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery/comment-page-1#comment-6093)
As far as the array is concerned, I was working from this:
Submitting a multidimensional array via POST with php
HTML
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field">
<button type="button" class="remove-field" title="Remove Product">×</button>
<input type="text" name="part[][number]" class="part-number-input" placeholder="">
<input type="number" name="part[][quantity]" placeholder="">
<input type="number" name="part[][tolerance]" class="" placeholder="">
</div>
</div>
<button type="button" class="add-field" title="Add Product">+</button>
</div>
PHP
if(isset($_POST['part']))
{
$message .='<table>';
$message .='<tr><td><b>Part #</b></td><td><b>Quantity</b></td><td><b>Tolerance</b></td></tr>';
foreach ($_POST['part'] as $parts)
{
$message .='<tr>';
$message .='<td> '.$parts['number'].' </td>';
$message .='<td> '.$parts['quantity'].' </td>';
$message .='<td> '.$parts['tolerance'].' </td>';
$message .='</tr>';
}
$message .='</table>';
}
OUTPUT TO EMAIL
Part # Quantity Tolerance
part1#
part1qty
part1tol
part2#
part2qty
part2tol
I've only ever messed with extremely simple forms, so any help would be greatly appreciated!
UPDATE: Adding the jQ that clones the fields.
JS
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).fadeIn(300).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
Again, I really appreciate the help.
Generally speaking, the array[] syntax makes an assignment to a new index in the array. In other words, if you don't specify an index, it will create one for you.
What's happening in your code is that you're assigning a new index to your array every time. For example,
part[][number] => part[0][number]
part[][quantity] => part[1][quantity]
part[][tolerance] => part[2][tolerance]
The quick solution to your problem is specifying the index instead of creating a new one. For example,
<input type="text" name="part[0][number]">
<input type="number" name="part[0][quantity]">
<input type="number" name="part[0][tolerance]">
<input type="text" name="part[1][number]">
<input type="number" name="part[1][quantity]">
<input type="number" name="part[1][tolerance]">
Depending on what you're trying to do, it may be easier to submit JSON data instead of multi-dimensional form data. Particularly if this is being generated through javascript anyway.
Here the problem is that you are not specifying the first index in the multidimensional array, so PHP has not way to tie the items together.
What you are getting is something like this:
Array(
0 => Array(
'number' => ...
),
1 => Array(
'quantity' = > ...
),
2 => Array(
'tolerance' = > ...
),
3 => Array(
'number' => ...
),
4 => Array(
'quantity' = > ...
),
5 => Array(
'tolerance' = > ...
),
...
)
You see you get a new element in the outer array each time you set an inner array value.
You need to change your markup to something like:
<input type="text" name="part[0][number]">
<input type="number" name="part[0][quantity]">
<input type="number" name="part[0][tolerance]">
<input type="text" name="part[1][number]">
<input type="number" name="part[1][quantity]">
<input type="number" name="part[1][tolerance]">
This will ensure the inner array items are grouped to the proper outer array positions.
Thanks to those who helped me identify the issue.
Here's how I fixed my particular problem. It's probably not the most beautiful of solutions, but I didn't want to complicate the JS I was using to duplicate this array.
First I changed the HTML:
<input type="text" name="part_number[]" class="part-number-input">
<input type="number" name="quantity[]">
<input type="number" name="tolerance[]">
Then the problem was getting it to come out right on the other end.
Following the solution from this question, How to get form input array into PHP array, I amended my PHP:
$part_number = $_POST['part_number'];
$quantity = $_POST['quantity'];
$tolerance = $_POST['tolerance'];
if(isset($_POST['part_number']))
{
$message .='<table>';
$message .='<tr><td><b>Part #</b></td><td><b>Quantity</b></td><td><b>Tolerance</b></td></tr>';
foreach ( $part_number as $key => $part)
{
$message .='<tr>';
$message .='<td> '.$part.' </td>';
$message .='<td> '.$quantity[$key].' </td>';
$message .='<td> '.$tolerance[$key].' </td>';
$message .='</tr>';
}
$message .='</table>';
}
Works exactly the way I intended it to. I'm sure if spent the time doing something like this, How to add (clone) form fields using jQuery and increment ids and names, I could have kept my PHP the way it was. I just didn't want to spoil the simplicity of the JS I already had that functioned well.
Thanks again!

How to submit the values of checkbox? [duplicate]

This question already has answers here:
How to pass an array of checked/unchecked checkbox values to PHP email generator?
(5 answers)
Closed 9 years ago.
On my form I have this part :
<input type="checkbox" name="city" value="Nicosia" class="choosecity">Nicosia<br>
<input type="checkbox" name="city" value="Limassol" class="choosecity">Limassol<br>
<input type="checkbox" name="city" value="Larnaca" class="choosecity">Larnaca<br>
and on the results page where I use the mail function, I want to get thechecked cities.
I used this without result:
foreach($_POST['city'] as $checkbox){
echo $checkbox . ' ';
}
What am I missing here?
Use name="city[]". Otherwise you will only be able to submit one city. You may also want to use
$cities = isset($_POST['city']) ? $_POST['city'] : array();
foreach ($cities as $city)
You need to name your inputs as an array name="city[]"
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]" you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]"></p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]"></p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]"></p>
Will give you all entered values inside of the $_POST['education'] array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1"></p>
You just just have to add this [] to input name this will create an array starting with [0]. The result will look so:
array(
[0] => 'Nicosia',
[1] => 'Limassol',
[2] => 'Larnaca',
)
The HTML:
<input type="checkbox" name="city[]" value="Nicosia" class="choosecity" />Nicosia<br>
<input type="checkbox" name="city[]" value="Limassol" class="choosecity" />Limassol<br>
<input type="checkbox" name="city[]" value="Larnaca" class="choosecity" />Larnaca<br>
The PHP:
if( isset($_POST[city]) && is_array($_POST[city]) ){
foreach($_POST[city] as $checkbox){
echo $checkbox . ' ';
}
}

How do I join fields of a form with codeigniter?

I have this snippet of code
<input type="text" id="name1" name="name1">
<input type="text" id="name2" name="name2">
<input type="text" id="name3" name="name3">
And I want to concat them inside a variable (something like $variable = name1.name2.name3), so I could set that value inside an array (in the controller) like this:
$form_data = array(
'Name' => set_value($variable),
'Sexo' => $this->input->post('optionsRadios')
);
I don't know if the above code is possible, but that's the idea for this thing that I want to do.
Why not something like this?
$variable = $this->input->post('name1')
. $this->input->post('name2')
. $this->input->post('name3');

Categories