I have a hidden input with array:
<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->name.'" />
How do I post the value of this array into the next page?
I tried this method: $a = $_POST['item_name']; but then it gave me the following error:
Array to string conversion in C:\xampp\htdocs\BSSecureTech\payment.php on line 6
Array
Then I tried $a = $_POST['item_name'][0];. It works but then I wouldn't know how many values are in the array. How do I kind of loop the [0] to let it post all the values in the array?
At first i suggest you to change in your HTML like this if your file extension is .php
<input type="hidden" name="item_name[<?=$cart_items?>]" value="<?=$obj->name?>" />
On PHP end check your post array like print_r($_POST);. Then catch your desired value by array index like this
$values = $_POST;
$item = $_POST['item_name']['your index'];
echo $item;
Related
I have 3 inputs like this:
<input type="text" name="product[1][name]" >
<input type="text" name="product[1][cost]" >
<input type="text" name="product[1][qty]" >
... which goes until n products
<input type="text" name="product[n][name]" >
<input type="text" name="product[n][cost]" >
<input type="text" name="product[n][qty]" >
and based on the input value a JSON string like this:
{"product[1][name]": "Prod1", "product[1][cost]": "100$", "product[1][qty]": "3", "product[2][name]": "Prod2", ... }
how can I retrieve their name and value because neither json_decode function can take their values because of the array-like name
$decodedObject=json_decode($array['data']); // this data contains the json string
echo $decodedObject->product; // does not work
echo $decodedObject->product[1][qty]; // does not work
is there any simple way?
Or the only way is to cut the name like product[, take the inside elements , and the value after the : and so on?
But how can I even take the object from it without letting PHP know that's not an array ?
Firstly I found an alternative. The problem is that this:
$decodedObject=json_decode($array['data']); // will create an object
will not transform it in an array, instead I have used this:
$decodedObject=json_decode($array['data'], true); // will create an array
after that I had checked how many objects exist, and then just add them to another array:
$i=1;
while(isset($decodedObject["product[$i][qty]"])) // while any element exists
{
$newProducs= array();
$newProducs['name'] = $decodedObject["product[$i][name]"];
$newProducs['cost'] = $decodedObject["product[$i][cost]"];
$newProducs['qty'] = $decodedObject["product[$i][qty]"];
array_push($products, $newProducs); // optional: add to the main array (products) the new-created array (newProducs)
$i++; //increment to search another product
}
The problem was that I was not fully creating an array -_-
While this is not the full answer I still hope somebody will find a better solution and not a change.
I have list of items, after selecting them and pressing a submit button
there's kind of a query in the url bar as such :
adrese-id=7&food-id=1&food-id=2&food-id=3&food-id=4
Trying to get all of the food IDs in an array but no luck so far, tried doing:
$ids = $_GET['food-id'];
but that just has the last value, which is 4...
How do I get those values in an array?
You have to name your field to indicate it's an "array". So, instead of food-id, append brackets to the end to make it food-id[]
For example:
<input type="checkbox" name="food-id[]" value="1"> Pizza
<input type="checkbox" name="food-id[]" value="2"> Cheese
<input type="checkbox" name="food-id[]" value="3"> Pepperonis
Accessing it in PHP will be the same, $_GET['food-id'] (but it will be an array this time).
In php the $_GET array has $key => $value pairs. The 'food-id' in this case is the $key.
Because all your values (1,2,3,4) have the same key: 'food-id' the array looks like this:
$_GET = [
'food-id' => 1,
'food-id' => 2,
'food-id' => 3,
'food-id' => 4,
]
This will always be parsed with the last $key => $value pair being used:
$_GET = [
'food-id' => 4
]
The solution to this is always using unique keys in your arrays.
You really need to provide the HTML fragment that generates the values. However if you look at your GET request the values for food-id are not being submitted as an array which is presumably what you want. Your GET request should look more like:
adrese-id=7&food-id[]=1&food-id[]=2&food-id[]=3&food-id[]=4
which should give you a clue as to how your HTML form values should be named.
I am using the following code:
$row_arr=$_POST['row_driver'];
print_r($row_arr);
returns:
Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )
but
echo count($row_arr);
is returning me a value of
1
Any reason why?
Here row_driver is an array being received through a form from a previous PHP page using hidden element property of HTML form. Also,
foreach($row_arr as $driver)
{
//code here
}
is returning:
Warning: Invalid argument supplied for foreach() in
D:\XAMPP\htdocs\Carpool\booking_feed.php on line 36
The issue you are facing is with the fact that $_POST['row_driver'] is not an array.
If you have one hidden HTML input:
<input type="hidden" name="row_driver" value="<?php print_r($rows); ?>">
...then $_POST['row_driver'] would be a string, e.g.:
$_POST['row_driver'] = "Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )";
, and therefore, your count() function results in 1.
This would also explain the second issue you are facing, with foreach(), where the function expects an array, but you are providing a string.
A solution would be to use a foreach loop for your hidden HTML inputs like this:
<?php foreach($rows as $row_driver){?>
<input type="hidden" name="row_driver[]" value="<?php echo $row_driver; ?>"/>
<?php }?>
This would then turn your $_POST['row_driver'] into an array.
You might just store the count value in some variable :
$row_arr=Array('d1','d2','d3','d4');
print_r($row_arr);
$count = count($row_arr);
echo 'Your Count is:- '.$count;
PHP document:
expression
The expression to be printed. return
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will
return the information rather than print it.
Return Values
If given a string, integer or float, the value itself will be printed.
If given an array, values will be presented in a format that shows
keys and elements. Similar notation is used for objects.
When the return parameter is TRUE, this function will return a string.
Otherwise, the return value is TRUE.
print_r() can use as special printing method to display all values in arrays and for associative arrays(more helpful for this).
Associative array:
Associative arrays are arrays that use named keys that you assign to them.
If you use echo you have print it with a array index. As a example $row_arr[0] or if you use for associative array instead of index, key is used. it may be string.
The problem lies with the hidden field
foreach ($rows as $value){
<input type="hidden" name="row_driver[]" value="<?php echo $value; ?>">
}
I know there are some similar questions but none of them solves my issue.
I have a simple form:
<form method="post">
Import data: <textarea type="text" name="import"></textarea>
<input type="submit">
</form>
Then I get data from the "import" field:
$current = my_data();
$import = $_POST['import'];
$merge = array_merge($current,$import);
The problem is, even if I paste:
array('foo' => 'bar')
I get:
Warning: array_merge(): Argument #2 is not an array in
(address)
on line (line)
I can't change the HTML markup and I have to paste arrays there. Any ideas how to fix it? I've been reading about serialize() but not sure if there's anything to serialize is array() is not array() for PHP. Why is that? Any solutions? Thanks a lot!
UPDATE
$current hold an array of options for my theme.
$merge is supposed to hold the same keys with different values (around 30-50 of them, not multidimensional but might be in the future), but of course users might add new ones so in order to ignore them I'm actually using:
$imported_options = array_merge($current_options , array_intersect_key($_POST["import"], $current_options ));
(simplified this one as it's just an example)
So after all I want to load an array from the form and update the other array with it.
PHP will not create arrays in $_GET/$_POST unless you tell it to:
Import data: <textarea type="text" name="import[]"></textarea>
^^---- need these
Without the [], PHP will treat any duplicate field names as strings to be overwritten. With [] in the name, PHP will treat them as new elements in an array.
You can do
$current['import'] = $import;
Or you can change your html this way:
<textarea type="text" name="myarray['import']"></textarea>
And in php:
$import = $_POST['myarray'];
The second argument is not an array.
$_POST['import'] = value received from the form.
With that said, try:
$current [] = $_POST['import'];
What are you trying to get from $_POST['import'] ?
you are using a textarea to get an array?
if it's just a single variable then use array_push
http://php.net/manual/es/function.array-push.php
for array_merge you need to have 2 arrays.
i am Reading the file contents and passed it in explod function("=",$string) ,it gives me two array parts[0].parts[1] seprated by = .parts[1] array displays all the values of the variable .now how can i use these values one by one to pass in the text box .The variable value comes in this way (value1
value2
value3
value4...)
my code also throws the undefined offset :1 notice when i prints the parts[1]arrray
if you have array values iterate over the array like
<?
foreach($array1 as $arr)
{
?>
<input type=test value= />
<?
}
?>