I am posting this because I do not really know what I could search for. Please note that I have basic knowledges and am still learning ; also, I might sometimes use wrong terms, sorry about that.
I am setting up a web application through PHP/MySQL, HTML, Bootstrap, and JQuery.
I need to post an array to my database. The problem is, this array might contain several times the same value. Indeed, the array is made out of values inside a html table, to which rows can be added to add more data at the same time thanks to JQuery and DataTable.
So far, I did not even know how to post an array, and found the following solution to be working to do so, as the following code:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="Spain">
<input name="country[]" value="Germany">
<input type="submit">
</form>
$country = $_POST['country'];
print_r $country;
Would contain the following array result:
|country|
|-------|
|France |
|Spain |
|Germany|
Wonderful.
But what I need to do sometimes, is something like this:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input type="submit">
</form>
And I would like this:
$country = $_POST['country'];
print_r $country;
To contain the array:
|country|
|-------|
|France |
|France |
|France |
But instead, the POST contains only
|country|
|-------|
|France |
Whereas Firefox Developer Tools clearly indicates me that the page sent the exact array I need to receive.
Form Data
country[]: [...]
0: France
1: France
2: France
Could you please help me with that?
Thank you very much for your time and attention.
Thank you Daan for asking me how I checked what $_POST['country']; contains.
I modified my original post to include these information. By doing so, I saw that I checked it once I changed it, as I use it again later in a foreach, to display all the rows in another array.
So I checked it the right way.
It turned out my problem was a lot more different, and is related to my use of the foreach.
Indeed, I use the country name as an index for the foreach. As it can only use unique indexes, it uses only once "France".
To scaisEdge: This is data I modified to be anonymous, and country was the easiest way to do so. Imagine products instead of countries, and this is why the user can add several times one unique name.
Related
I have read through many questions and do my searches for hours, but i still cannot find the solution to what i exactly want.
<form method="POST">
<input type="checkbox" name="fruit" value="0" />No Preference
<input type="checkbox" name="fruit" value="1" />Apple
<input type="checkbox" name="fruit" value="2" />Orange
<input type="checkbox" name="fruit" value="3" />Banana
</form>
print_r($_POST);
I already did validation to checkbox value 0 to 1,2,3 so they will inverse and i did it using looping javascript search for the element name. The problem is, i have to use the element name without changing it become array name. (e.g Fruit => Fruit[] ). So i need to use this element name to retrieve all checked information inputted by the customer. I've seen this can be done in ASP, but i could not figure how they do as it's long time ago already.
My question is, could any one figure how to do this without changing the element name into array format (e.g Fruit => Fruit[] ) ? T.T
Any help will be appreciated. Thank you..
As you already figured, when you submit the same name multiple times, the latest one will overwrite former ones. A solution is to set up a hidden field
<input type="hidden" value="" name="foobar_as_array">
and use jQuery or plain JS to concat your values into that field on submit
<form ... onSubmit=$('#foobar_as_array').value(...concatenate them...);">
I changed the validation using this line :
var GenBox = eval("document.forms['SubmitSearchAll']['" + oCheckBox+"[]']");
and it helps me alot. Now it is solved.
Thanks everyone for contributing.
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.
It's more of a wether can someone confirm my theory, as for going short ways, when you add another input which shall have similar name ex. myvar_0, myvar_1 you are supposed to use javascript to generate those inputs, but there is input "array" type, where you crate an input with name myvar[], myvar[], myvar[] and this acts as an array and passes values via post to PHP as an array, but recently i've discovered that for some weird reason this array has limitation of 197 values ( or 196 is the maximum capable index value ) as on chrome for now ( didn't text it on other browser ).
So does anyone else encoutered a similar problem ?
If you're using suhosin, that brings a limit to the max_vars sent via POST.
Default is set to 200, so could be your problem.
See: suhosin.post.max_vars
array format got nothing to do with html or browser , for browser square brackets doesn't mean any special thing it will send all the key , value pairs with same key as opt[] e.x
for
<input type="hidden name="opt[]" value="1"/>
<input type="hidden name="opt[]" value="2"/>
<input type="hidden name="opt[]" value="3"/>
browser will send
opt[]=1
opt[] =2
opt[]=3
as a request to the server;
Its the PHP which is smart enough to interpret this as index array with name of "opt" .
I've just done a simple test (See below) which returns (for me) 200 items in a post array.
<form method="post">
<?php
print count($_POST['opt']);
for($i = 0; $i < 200; $i++){
?><input type="hidden" name="opt[]" value="1" /><?php
}
?>
<input type="submit" />
</form>
I get the impression this is more about the data being sent, or the server itself, than a limitation of PHP.
Well the answer was the Suhoshin security module, after some research i found that acually max_post_vars was set to 200, that was kinda blocking the rest of data from being processed, thanks for all your answer :-)
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
I am creating a order form for a meat locker company that sends the form to their email. There is a lot of where if you select one item you can not select another item; such as t-bone and New York Strip, you can have one or the other, but not both. Here is what my code looks like.
<P>T-bone steak <input type="radio" name="T-bone and NY" id="T-bone steak" />
or New York Strip steak <input type="radio" name="T-bone and NY" id="New York Strip" /> </p>
This does prevent a person from selecting both, but when he views in in his email he sees name="T-bone and NY" so he doesn't know which one they selected. I thought it would display the id="T-bone" or "NY Strip". I'm sure there is a better way of doing this with an if statement.
Second, when they receive the email it has all of the names of the fields people selected and it says on afterwords. For example if someone selected Prime rib the email would say:
Prime Rib: on
Is there a way to send the form exactly as the user sees it. Maybe in an image, word doc, or access data base?? I'm open to anything.
If your want to see it the url is www.spillvillelocker.com/beef.php
Thank you very much:)
Your code should look like this:
<P>
T-bone steak <input type="radio" name="steak" value="T-bone steak" id="tbonesteak" />
or New York Strip steak <input type="radio" name="steak" value="New York Strip" id="nystrip" />
</p>
Notice, I have "name" be the same for both, with different "values". Also, "id" attributes shouldn't contain spaces, so I changed them to something alphanumeric only.
I think you just need to provide a VALUE="" attribute to show which value each radio button specifies... also NAME and ID should be equal.
<P>T-bone steak <input type="radio" name="MEAT1" id="MEAT1" value="TBONE"/>
or New York Strip steak <input type="radio" name="MEAT1" id="MEAT1" value="NYSTRIP"/> </p>
You need to set value attributes of those inputs.
BTW you should not have spaces in name attributes.
In addition to what Mike posted, regarding your second question "why the email says 'Prime Rib: on', it is again because there is no value attribute assigned to the element. The default values for radio buttons are 'on' and 'off'. Without setting an explicit value, you're left with the defaults.
There are two things that you can do for this.
Set an explicit value for the form element, something like
<input type="radio" name="primeRib" value="Yes"> Prime Rib
That will show up as 'primeRib: Yes' in the email.
The second thing you can do is use PHP to parse your responses to only send the data you want. Something along the lines of:
<?php
foreach ($_POST as $key=>$value) {
if ($value=="off") {
continue;
}
else {
$email.="$key\n";
}
}
?>
Something like that would skip over each $_POST variable that is set to "off" and for each variable that isn't, it would add the $key (the data inside the "name" attribute) to the $email variable, or however you add the data to the email to be sent out. There's a number of things that you can do, but at least this would allow you to filter only for variables that are on/checked.