I want to know the best and easiest way to pass associative array in form inside input field. So far Ive done this and it all started to look messy and hard to handle.
$dish_result = $obj->getAll_dish();// this returns array all the rows in dish table
<form action='order_process.php' method='post'>
foreach ($dish_result as $dish){
echo '<input id="" name="dish_name[]" type="checkbox" value="'. $dish['dish_name'].'">';
echo '<input id="" name="dish_number[]" type="checkbox" value="'. $dish['dish_number'].'">';
}
</form>
Now on the order_process.php I have
foreach($_POST['dish_name'] as $dish){
echo $dish;
}
foreach($_POST['dish_number'] as $num){
echo $num;
}
What I wanted is an associative array, but how can I associate it the form dynamically. in other words I wanted to achieve this on the order_process.php.
$dishes = array(
//'dish_name' => dish_number
'chickencurry' => '70',
'onionbhajis' => '22'
// and so on.
);
Thank you very much in advance.
Create a grouping name first, then to get that kind of structure, make the dish name as key, then the value attribute holds the number. The basic idea is this:
name="dish[dishname]" value="dish_number"
It'll be like this:
echo '<input id="" name="dish['.$dish['dish_name'].']" type="checkbox" value="'. $dish['dish_number'].'" />';
When you submit it with all the checkbox checked, it should be something like:
Array
(
[chickencurry] => 1
[onionbhajis] => 2
)
On the order_process.php page, just call it just like you normally do:
$dishes = $_POST['dish'];
foreach($dishes as $dish_name => $dish_number) {
}
Sample Output
You can add an index to array postdata. Try this:
foreach ($dish_result as $dish){
echo '<input id="" name="dishes['.$dish['dish_name'].']" type="checkbox" value="'. $dish['dish_number'].'">';
}
Your data will then be an associative array of the checked elements when posted back.
Related
How can I show only the specific index? For example, here's my UI.
As you can see in the picture I have 3 checkboxes and 3 textboxes with array value.
Let's say these are the name of the element.
<input type="checkbox" name="check[]">
<input type="text" name="textbox[]">
Then print the array:
$check = $_POST['check'];
$total_rec = $_POST['textbox'];
echo 'Check Array<br>';
print_r($check);
echo '<br<br><br><br>';
echo 'TextBox Array<br>';
print_r($textbox);
Result:
Check Array
Array ( [0] => 2 )
TextBox Array
Array ( [0] => [1] => 2 [2] => )
As you can see in textbox array all index showed, all I want is to show only the specific index with value and that is the 1 => 2 only.
use empty(), return true if contain value, example :
//iterate each $total_rec's member
foreach($total_rec as each){
//if $each not empty, do something
if(!empty($each)){
echo $each;
}
}
You are using $total_rec for post values of both checkbox and text. You can filter the array of text input like this:
$total_rec_text = $_POST['textbox'];
$total_rec_text = array_filter($total_rec_text, function($arr){
return !empty($arr) ? true : false;
});
You need to loop over $_POST array and check if checkbox is checked.
If checked, then only, get/print value.
Also, in HTML, you need to add specific counters to both checboxes and
textboxes.
As only checked checboxes get posted and texboxes get posted by
default.
<input type="checkbox" name="check[0]">
<input type="text" name="textbox[0]">
<input type="checkbox" name="check[1]">
<input type="text" name="textbox[1]">
<input type="checkbox" name="check[2]">
<input type="text" name="textbox[2]">
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $idx => $value) {
echo "<br/>" . $_POST['check'][$idx] . ' ' . $_POST['textbox'][$idx];
}
}
I want to be able to post an array containing...
$food = array (
'food_1' => 'ice cream ',
'food_2' => 'pizza'
);
<input type="text" id="in_foods[]" value="<?=$food;?>" />
to another page but it does not seem to be working. what am I doing wrong?
A cleaner solution will be to:
1- convert the array into a string using the implode function:
$foods = implode(',',$foods);
2- Place it in the input text field to be submitted:
<input type="text" id="foods" value="<?=$food;?>" />
3- On the other page convert the string back to an array:
$foods = explode(',',$_POST['foods'])
Try ;)
You can't just post a data structure like that. You should be getting a notice saying you're trying to convert an array to string.
Consider putting your array into a session instead. That way it stays as an array between calls.
session_start();
$_SESSION['food'] = $food;
Try with this code :
<?php
$food = array (
'food_1' => 'ice cream ',
'food_2' => 'pizza'
);
$food = implode(",", $food);
?>
<input type="text" id="in_foods[]" value="<?=$food;?>" />
As $food is an array.
<input type="text" id="in_foods[]" value="<?
if (isset($food)) { echo $food['food_1'].', '.$food['food_2']; } ?>"
/>
If size of $food array is not defined then you can use implode function to make it as single string and can echo it.
I guess you wan't to tick foods you like out of a predefined list of foods or something like this. In this case an <input type="text"> field is not the right choice. Use <input type="checkbox"> instead (or a <select multiple>).
$foodOptions = array('burger', 'vergtables', 'berries')
foreach($foodOptions as $foodOpt) {
$checked = in_array($foodOpt, $foods)?' checked':null;
echo '<input type="checkbox" value="' , $foodOpt , '" name="in_foods[]"', $checked ,'><br>';
}
I have issues with creating an array and storing the info, I have a table with data that could be infinite in its number, a user will then select some options which will determine which of these values they can select (which again is an infinite number), these choices are then presented into a checkbox where i use this code
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details" value="'.$invoice_details.'"/>';
}
?>
So this should search through the options they previous choose, and sorts them into an array and then into checkboxs, but when i store the information is just saves the last box checked, I cant change the value of each input EG
echo '<input type="checkbox" name="invoice_details[value1]"
echo '<input type="checkbox" name="invoice_details[value2]"
Because I don't know how many values/checkboxes there will be.
I have also tried this
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details[] = $detailsline->details_line;
echo $invoice_details[];
echo '<input type="checkbox" name="invoice_details[]" value="'.$invoice_details.'"/>';
}
?>
Changing the
$invoice_details
to
$invoice_details[]
but this will just store a value "Array" in my database and not the actual values.
Please can anyone help me?
Ian
Okey I think You should Try this:
<?php
$i=0;
foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details[$i]" value="'.$invoice_details.'"/>';
$i++;
}
?>
Try this
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details[]" value="'.$invoice_details.'"/>';
}
?>
Use <input type="checkbox" name="invoice_details[]">
In your code you can use serialize($invoice_details) for saving into DB and when using - unserialize($field_from_db).
First function returns string, so you can save it as string field, second function get the string and returns the whole array, so you can work with it.
but this will just store a value "Array" in my database and not the actual values.
think you tried to store Array as string so got that value
So may be needed to use implode/serialize/json_encode (Array) to store data?
Literally saying code $invoice_details[] = $detailsline->details_line; means take property details_line of an object $detailsline and insert its value as new element in array $invoice_details. Are you sure it is what you want?) Also I suppose that $detailsline is an array, but not the object (you trying to operate it as an object)
I am trying to send data from multiple checkboxes (id[]) and create an array "info" in php to allow me to run a script for each value (however the quantity of values may change each time) however first I am trying to display the content of each array value. I am not quite sure how to put my array populating line to save all the content to the array.
HTML
echo("<input name='id[]' type='checkbox' value='".$shopnumb."'>");
my hopeful processing code currently is -
$info=$_POST['id[]'];
Echo(array_values($info));
what do I need to do to make the content sent by post from the form checkboxes populate the array info
any help is greatly appreciated
edited for clarification.
Change
$info=$_POST['id[]'];
to
$info=$_POST['id'];
by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.
You should get the array like in $_POST['id']. So you should be able to do this:
foreach ($_POST['id'] as $key => $value) {
echo $value . "<br />";
}
Input names should be same:
<input name='id[]' type='checkbox' value='1'>
<input name='id[]' type='checkbox' value='2'>
...
On the form page, field names must look like this
<input name="id[]" type="checkbox" value="x">
<input name="id[]" type="checkbox" value="y">
<input name="id[]" type="checkbox" value="z">
On the destination page, $_POST['id'] is your array variable
$id = implode(",", $_POST['id']);
echo $id; //Should print "1,2,3"
You cannot echo an array directly, because it will just print out "Array". If you wanna print out the array values use print_r.
print_r($_POST['id']);
I don't know if I understand your question, but maybe:
foreach ($_POST as $id=>$value)
if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];
would help
That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...
How can I display this value in the loop ?
echo '<input type="hidden" name="eg_payamt_'.$i.'" id="amount_post_'.$i.'" value="">';
this code is being passed into next code by POST
On the next page,
Could it be like this ?
foreach($_POST["eg_payamt_"] as $key => $payamt)
{
echo "eg_payamt_$key => $payamt\n <br>";
}
I don't see any results,
Do you guys have any ideas ??
You have to change the name of the input element -- all input elements like this will have the same name:
<input type="hidden" name="eg_payamt[]" value="whatever" />
And then when you access $_POST['eg_payamt'] it will already be an array, so your code will work almost as-is (you need to lose a couple of underscores).