POST array along with other variable - php

I am doing a POST to pass checkbox group values using an array. Is it possible to pass another variable that I'm pulling from database and append it into this array? Or are there other ways to pass this particular value along with the array?
echo "<td><input type='checkbox' name='".$checkedIncident."' value='".$incidentID."'/></td></tr>";
This is the array i'm passing to another php on submit. I wish to pass another variable $id too.

Use a hidden input. Assuming $checkedIncident is something like incident[] that is creating an array:
<input type="hidden" name="$checkedIncident" value="$id">

Related

How to fetch the only $_POST value

I'm trying to assign to a variable the unique index that is in $_POST.
I'm trying to do this, because the $_POST value will change for each button I press, and doing them all is like too long, plus, it's like a filer, every time you add a file, 3 new buttons appear, so too long.
The value either starts with 'edit', 'download', or 'delete', followed by the id of the selected file. For example, the 8th file will be deleted with 'delete8'.
I used filter to fetch the file id in my php file, but I can do it, only when I know exactly how the value is named.
Is there a way to select automatically the one and only value available in $_POST ?
array_keys will give you an array of keys in an array.
$name = array_keys($_POST)[0];
you can use,
foreach($_POST as $key => $value){
$posteddata[$key]=$value;
}
for fetching posted data
<input type="text" name="n1">
<input type="text" name="n2">
echo $posteddata['n1'];
echo $posteddata['n2'];

What is it called when you use array syntax for form field names?

When creating forms one can use the PHP [] array syntax to have multiple form fields populate an array when the input is parsed in PHP, like this:-
<input type="text" name="myArray[]">
<input type="text" name="myArray[]">
What would you call these kind of fields? Array input fields, maybe?
Aside: the PHP docs for this type of form input can be found here
It just like a normal input form. But value to send is multiple value, therefore it just set like array type

How to put textbox value into array only if it has value?

I have created many textboxes and I want to put all of the values into array only if they have themselves filled. I need them to work as how checkbox works in HTML (only the checked ones will then put into array). I use PHP language here. How to do that?
This is my simple HTML textbox:
<input name="array[]">
Would really appreciate for any help you give to me. Much thanks!
Firstly, I'd recommend you change your name to something more readable:
<input name="name[]">
Next, you want to get your data, I am assuming your form is using POST. We're going to store the form data into a $names array variable.
$names = $_POST['name'];
Next, we're going to create a new array variable which will store input values that have data.
$namesWithData = [];
We're now going to loop through the $names array. This loop will add any fields with data to the $namesWithData array.
foreach($names as $name) {
if(!empty($name) {
array_push($namesWithData, $name);
}
}
The $namesWithData array has the data ready to use.

posting 2 values to array from single checkbox

I have a situation where I'd like to post two values to a search query string array from a single checkbox. Because of the design, I can't just add another checkbox.
The checkbox in question looks like so:
<input name="wpp_search[property_type][]" value="rental_home" type="checkbox" id="choice_c"/>
<label for="choice_c">For Rent</label>
what I now get in the query string is ...
{url}?wpp_search[property_type][0]=rental_home
but what I need is to tie two values to that one check so I end up with this:
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][1]=building
Any simple solutions? There is only one other checkbox that already feeds that array, so I could force this one to be
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][2]=building
You can seperate values with for example "|" like this value="value1|value2". Then later you can use the function explode: $p = explode("|", $value); and you get 2 values.
Generally it's not possible to send one value as two values.
One way I could imagine is that you reconfigure the ini-setting of arg_separator.input:
arg_separator.input = ";&"
This will allow you to use ; as well to separate values which then would allow you to inject the second value per that value:
<input name="wpp_search[property_type][0]"
value="rental_home;wpp_search[property_type][1]=building"
...
/>
If you make use of the value ; in other form values you naturally need to properly escape them to make this compatible.
Another way is that you insert a hidden field with that value and if your checkbox is checked, you change the name of it to the right name. If unchecked, you change the name of it back to something not right:
<input type="hidden" name="---wpp_search[property_type][1]" value="=building" />
Take the javascript reference of your choice and do the needed DOM manipulation on click of the other checkbox to remove the three --- at the beginning of the name.

Handling unknown number of values in PHP file

The form that will be submitting the data will have somewhere between 10 and 100 values being sent to the PHP file. The number of inputs is stored in a variable called count within my javascript function but I don't know how to transfer its value to my PHP file.
Another idea I had was to have a while loop that detected a null value and to have a counter within the while loop to keep count.
How should I handle this?
No need to submit the count. You can just submit your dynamic number of values as an array. You can use the PHP array notation in your name attributes:
Input 1: <input type="text" name="myvar[]" />
Input 2: <input type="text" name="myvar[]" />
...
Input n: <input type="text" name="myvar[]" />
On the PHP side, $_REQUEST['myvar'] will be an array.
Without knowing more about your implementation, I would suggest the latter of your two options: use PHP to loop through your $_POST array and count the valid values you have. This is the best option as it is more secure and reliable.
I think there might be a more "correct" way to do this, but what I would do (without knowledge of a better way) is have a hidden form element something like
<input id="hidden_count" type="hidden" name="count" value="" />
And then have a function called onsubmit that sets this value and returns true to tell the form to continue
(with jQuery)
function onSubmitFunc(){
$('#hidden_count').val(count);
return true;
}
I'm sure there's a more elegant solution, but that should work for what you need.
If you have access to Javascript, the easiest by far would be to turn your data into JSON on the client side, send it to the server as a single string variable, and use json_decode on the server to get the properly formatted value back. You can react to the submit event on your form to have the Javascript compute the JSON before the data is sent.
Otherwise, you can have the Javascript output the number of fields to a specific hidden variable within your form. The server can then read that value, and look for the data by key in its input.
I guess you use javascript for creating the additional input element, if you just name ascending, you could use count to loop through the $_POST data. Or else use a hidden element form for counting the new input elements and post it, that way you have the correct number, and know how long you should be count.
Maybe I am not getting this, but if count in your JS file corresponds to the number of input elements in the form which is send to your PHP file, why not count() them on the serverside. The data will be in the $_POST array.

Categories