I am working on an application which, in the end, will produce a .CSV file. I have already found how to turn an array or arrays into a .CSV file, but I am having trouble figuring out the preceding step.
I have a bunch of html inputs in a form that will be sent to PHP, I am hoping to have one array consisting of all the input names (input name="example") and another array that consists of the values put into those fields by the end user.
In the end I was the CSV to be a product of both arrays. The first row being things like Height, Width, Depth, and the second row being 10 mm,20 mm,9 mm, etc etc.
I am wondering if a mixture of html classes and a foreach could be used, but I'm really not sure. Any help would be great! Thanks!!
using the square bracket notation will convert these named fields into arrays, here are a few examples
<input name="array[]">
<input name="array[]">
<input name="array[]">
<input name="array[foo]">
<input name="array[bar]">
<input name="array[0][foo]">
<input name="array[0][bar]">
<input name="array[1][foo]">
<input name="array[1][bar]">
Then to get a quick preview of your data use var_dump() or print_r()
var_dump($_REQUEST['array']);
Related
for explanation i need this information for a project to handle Datamatrixcode - till this moment just everytime a number.
Okay i have a table with some numbers (unique_codes) and i take this numbers via SELECT these from a table (MySQL) and put them to an array ($row).
Scenario: the worker scans a number (and this numbers MUST BE a number from this $row list - then i $_POST this $row to other site to take it for the next step) otherwise i want a Error Exception.
My idea goes to us pattern like:
<form action="next site.php" method="post">
<input pattern="<?php $row['unique_code']; ?>"/>
<button class="btn btn-success">Send</button>
</form>
But this doesn´t work.
How can i solve this? Try and catch as an alternativ ?
EDIT - with a solution:
I let the input field to that what a input form does: receive input.
So i $_POST a number to the second page and proof this to my values.
I think you should either put print or echo in front of $row['unique_code'] or replace <?php with <?=, but then you have to enable short_open_tag in php.ini
The page I'm currently working on has an "add another" option, which inserts a fieldset containing a title and a list of 3 input boxes. Each of these input boxes, for form submission sake, needs to be given a name with an incremented value. ("field1_1", "field1_2" etc)
I know that I could add the fields by dynamically creating elements with $('<input/>', {...}); but for more than a few elements this makes for code that's hard to read and difficult to maintain.
I'm currently using jQuery's .load() function to pull in the file but through searching I can't find a way to pass variables to the response, and therefore can only use preset name attribute values.
Is this possible, or is the method above the only way?
Put your template into a dummy script, with unknown type to avoid errors:
<script id="template1" type="text/template">
<h2>{title}</h2>
<input type="text" name="field{id}_1">
<input type="text" name="field{id}_2">
<input type="text" name="field{id}_3">
</script>
Then use replace (with "g" global option as replace usually replaces first only) to insert your values.
var template = $('#template1').html();
template = Template.replace(/\{title\}/g, mytitle);
template = Template.replace(/\{id\}/g, nextId);
$('#somewhere').append(template);
You can of course concatenate the replaces, but going for readable here.
This method allows complex HTML without messing up the code with loads of string manipulation. Your template looks like the final result so no mental translation required. Very low-maintenance technique.
The placeholders can just be simple names (without braces) to avoid regex delimiters, so long as the names will not match anything else in the template. I just use braces so the placeholders stand out in the template (again for maintenance purposes).
When this monster is sent via POST
<input name="arrayname[index][string[name]]" value="321" />
this is what i get:
print_r($POST['arrayname']);
>> Array ( [index] => Array ( [string[name] => 321) )
Where is the secons bracket?
I can access it via:
echo $val['string[name'];
but thats just horrible.
The structure of the name should be kept if possible.
Its part of a large generic method and only in some cases theese names get generated. If its not possible to work with names like theese properly (it is but ugly like i mentioned above), i will have to change the whole form generating.
Incorrect : arrayname[index][string[name]]
Correct : arrayname[index][string][name]
You have to choose:
Let PHP convert your POST data to arrays using the square bracket syntax
Parse POST data yourself from raw post data
You just cannot have both at the same time.
<input name="arrayname[index][string][name]" value="321" />
So, I have this HTML form:
<form id="search_form" class="form_wrapp"
accept-charset="utf-8" method="get" action="http://testing.com/results">
<input class="inputbox" type="text" name="search_query">
<input class="ic_search" type="submit" value="">
<input type="checkbox" value="checkbox1" name="search_filter[]">
<label for="Checkbox1">Checkbox1</label>
<input type="checkbox" value="checkbox2" name="search_filter[]">
<label for="Checkbox2">Checkbox2</label>
</form>
and it redirects to this URL upon submit with the 2 checkboxes checked
results?search_query=dreams&search_filter[]=checkbox1&search_filter[]=checkbox2
It works like this (inside codeigniter I get the data with $this->input->get('search_filter')), but my question is: I am doing something wrong inside the form, or this is how it's supposed to work?
And I mean about: &search_filter[]=checkbox1&search_filter[]=checkbox2. Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ? And if not, how can I make it work like that?
If you want it in the comma format you can do the following:
$filters = (array) $this->input->get('search_filter');
$filters = implode(',',$filters);
If you want to alter the format in which the form is submitted, assuming jquery for js:
$('#search_form').submit(function() {
var $hidden = $('<input type="hidden" name="search_filter" />').appendTo($(this)),
$filters = $('input[name^=search_filter]'),
value = '';
//loop through the filters check if there checked and add them to the value
$hidden.val(value);
$filters.remove();
});
Of course if the user doesn't have js enabled it will submit natively
Am I doing something wrong inside the form, or this is how it's supposed to work?
That's how it's supposed to work. At least if you need to read query string with PHP, those brackets need to be there to read the whole query string without each search_filter value being overwritten by the next one.
And if not, how can I make it work like that?
If you have to, you can use a POST request instead, process the submission, and redirect to the URL of your choice with whatever query string you want.
From your comment:
I wanted to make the url like this &search_filter[]=checkbox1,checkbox2 just to make it a bit more "beautiful"
Don't worry about that, seriously. The only time this matters is when you're doing extreme SEO and you don't want two URLs that point to the same place. It's common practice in those cases to remove all unused keys and alphabetize them so that all URLs with query strings are consistent, but mangling them into something custom still isn't a part of that.
Besides that, don't fight against the behavior - work with it - it's not "broken" and making it "beautiful" won't matter to anyone, plus you'll have to guess/remember which pages process query strings the correct way, and which ones use your "custom" method.
I am doing something wrong inside the form, or this is how it's supposed to work?
That is how it is supposed to work
Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ?
Then you couldn't tell the difference between two items and one item that had a comma in it.
And if not, how can I make it work like that?
Obtrusive JavaScript. Don't do that. Forms work well the way they work.
That's perfectly normal. form data is always sent in key=value pairs, with one single value. Submitting key=value,value is not part of the HTTP spec, and would have the values treated as a single monolithic string, not two separate comma-separated values.
You can certainly use some JS to rebuild your form on the fly to use the value,value format, but then you'll have to mod your server-side scripts to accept that new format as well. PHP won't auto-split the values before you, because it's not a standard representation.
&search_filter[]=checkbox1,checkbox2
Why you need this?
Use this like:
<?php
$searchFilter = $this->input->get('search_filter');
foreach($searchFilter as $filter)
// some actions with filters.
You search_filter[] is simple array with values from checkbox inputs.
I am curious know if and how it is possible to combine 2 values of an array together instead of overriding the other. I will show you an example:
I have a form that is mapping fields to a database from a CSV file. The problem I am running into is say for example there are 2 address fields that need to be merged into 1 address field in my database. (IE: photo below)
So my problem comes when I look at the $_POST[] array. It will show that there are 2 HOME ADDRESSES Selected and import into my database with the LAST selected home-address.
How can I merge the information into 1. I hope this gives you enough information on my problem, please let me know if you need something specific.
I am dealing with all arrays, and when I post into my database it requires an Array to loop through, as I use a reflection class. I hope this makes sense...
Any light would be appreciated on this matter.
Cheers,
I appreciate the quite comments back, the problem that I have with your responses is that I can't create my inputs to be address[] as that will be dynamic and I won't know which one will be set to address and which would perhaps be set to 'phone'... I hope this new picture helps a bit in understanding.
Some of the code (Shortened):
<select name="Home_Address_1"> // name is dynamically generated from the CSV headings
<option>...</option>
</select>
<select name="Home_Address_2"> // name is dynamically generated from the CSV headings
<option>...</option>
</select>
Example of using two posted values in a single array:
<!-- HTML -->
<input name="address[]" type="text" value="111" />
<input name="address[]" type="text" value="222" />
Notice the name attributes.
// PHP
$address = $_POST['address'][0] . ' ' . $_POST['address'][1];
echo $address; // prints "111 222"
UPDATE
Before your script loops through the $_POST array, merge the fields, like so:
$preformat = $_POST['Home_Address_1'];
$preformat .= ' ' . $_POST['Home_Address_2'];
$preformat .= ' ' . $_POST['Home_Address_3'];
$_POST['Home_Address_3'] = trim($preformat);
Then the last Home Address field contains all three.
Try array_merge()...http://php.net/manual/en/function.array-merge.php
Try array_merge with shuffle
$merged = array_merge($arr1,$arr2);
shuffle($merged);
with regards
Wazzy