Using null key in html form array - php

In an html form, it is possible to send data as an array.
<!-- This is an associative array -->
<input name="table[key1]">
<input name="table[key2]">
<!-- This is an index-based array -->
<input name="table[]">
<input name="table[]">
Is it possible to store a value into an associative array, using an empty string as a key?
So that php could access the data with $_POST["table"][null]

null is not an empty string, that would be a '' or a "".
However, PHP will convert it to an empty string and use that as a key (Look under Example 1 at https://www.php.net/manual/en/language.types.array.php)
So these are equivalent:
$a = array('' => 'hello');
$b = array(null => 'hello');
From your example, the data would be in $_POST["table"][""]

Related

PHP _GET values in an array

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.

count() returning wrong value

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; ?>">
}

Put JSON data into html form input checkbox with PHP

I want to put json data into html form input checkbox with laravel blade.
I have multiple input checkbox values as test[],
Then I try use htmlspecialchars to print values into input.
If my frontend check this input, backend use print_r is like this
Array
(
[0] => {"value1":"tool_ad_id","value2":"\u65e5\u4ed8"}
[1] => {"value1":"ad_group1","value2":"\u30c4\u30fc\u30eb\u5e83\u544aID"}
)
but I use return $request->test['0']['value1']; can't get a value.
I want to get 'value1' and 'value2'.
PHP Laravel
#foreach($as as $key => $value)
<div class="col s6 m4 l3 blue-grey lighten-5">
<?php
$data = ['value1' => $value['en'] ,'value2' => $value['jp'] ];
$data_total = htmlspecialchars(json_encode($data));
?>
<input type="checkbox" id="test5{{ $key }}" value="{{$data_total}}" name="test[]" />
<label for="test5{{ $key }}">{{$value['jp']}}</label>
</div>
#endforeach
Laravel Controller
return $request->test['0']['value1'];
Error message
Illegal string offset 'value1'
[0] => {"value1":"tool_ad_id","value2":"\u65e5\u4ed8"}
Index => String
PHP does not parse JSON, you are receiving the JSON as normal string. Therefore, in order to convert it to a PHP object with properties correspongind to the keys, you need to use json_decode().
Try $test = json_decode($request->test['0'], true), and then access values off the $test variable.
$value1 = $test['value1'];
$value2 = $test['value2'];

Preserve array when submitting a form to a JSON file

I have a form that takes in data and writes to a JSON form in PHP.
I needed to submit an array as a numeric input but it keeps giving me a string. Is it possible to enable the form to submit as an array via text input box?
Form example:
<input type="text" name="arraytobepushed[]" placeholder="EG: 1000,2000,3000" />
The output is:
{
"obj": [{
"arraytobepushed": ["1000,2000,3000"]
}]
}
You could turn the text into an array by using explode() So you would have something like this:
<?PHP
$myArray = explode(',', $_POST['arraytobepushed[]']);
?>
The explode() function splits everything separated by the first argument (in this case a comma) you pass and puts it into an array.
So if your inputted was 1000, 2000, 3000 your $myArray would look like:
index 0 = "1000" ($myArray[0])
index 1 = "2000" ($myArray[1])
index 2 = "3000" ($myArray[2])
Keep in mind that the values are still strings, not integers. If you want to make them integers you can do this:
$myArray = array_map('intval', explode(',', $_POST['arraytobepushed[]']));
This makes all your elements into integers like so:
index 0 = 1000 ($myArray[0])
index 1 = 2000 ($myArray[1])
index 2 = 3000 ($myArray[2])
No. Forms submit text.
PHP special cases fields with [] in the name as fields to be expressed in an array. It has no special case feature to treat a field as a number instead of a string. You need to convert the data explicitly.

Post array into next page

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;

Categories