Form Arrays support across browsers - php

I'm not even sure if form arrays is the proper term, but it looks a bit like this:
<input name='element[]' type='text' />
<input name='element[]' type='text' />
Which is then retrieved in PHP as an array stored in $_POST['element'] -- in this case with 2 values.
I've tested it in the browsers I have available to me, but I've never seen this before and I'm wondering is this supported pretty much in all browsers? Or is this something very old that I've just not run into?
Thanks!

It is the server side language that turns this into an array (most languages don't require the name to end in [], that is an oddity of PHP).
As far as the browser is concerned, it is just a bunch of inputs with the same name, that get serialized using the standard rules for submitting form data. i.e.
element[]=value&element[]=value
No browser has any problems with this. It has worked this way since HTML first got a form element.

Related

Passing in the correct values in an MVC Pattern

I am trying to use an HTML form to submit the url index.php?page1&name2=value2&name3=Value3. The closest I've come is index.php?page1=&name2=value2...
I can't figure out how to get a clean page1 and I'm certain there's a simple answer. I'm not sure where to search but the MVC pattern I implemented (John Squibb's) assumes that format, I have to assume it's possible. Any information is appreciated.
What I have so far:
<form id="page1" action="index.php" method="get">
<input type="hidden" name="page1"/>
...
A clean page1 variable does not make much sense in this context.
When you submit a FORM, the browser decides how to encode your form variables into a query string (the part after the ?) that the browser then presents to the HTTP server. I could not find a standards document in a quick google search, but the Wikipedia page is quite good.
So the browser is choosing to encode page1, which has no value as page1=. This is what I would expect any browser to do. When your query string is processed on the server side, just about every language (PHP, Ruby, Python, etc...) should be able to handle this and will create a page1 variable with either a null or empty string value. If you're processing the query string on the client side (say with Javascript), you should find a library capable of parsing the query string.
What are you trying to accomplish? Why do you require the equal sign to be missing?

Can a !empty input be part of a function - PHP

What I'm trying to achieve is, if there is an non empty input + another input will show up.
This is how the input looks like.
<input type="file" name="image[]" />
What I'm trying to do is something like this
if (!empty($input)){
//Add another input
}
I think that you can get the idea, I am just wondering is this achievable, since I am new in php I don't know if this is possible.
And I'm sorry if I made any grammatical mistakes English is not my native language.
Sure! By looking at $_POST and $_FILES you can get an idea whether the input contained something or not. Then you can make that if and render an additional <input> as necessary.
Note though that PHP is server side code. It runs when the form is submitted in the browser and thus the browser makes a request to the server. If you want the additional input to appear immediately, as soon as the first input is filled, you'll need to use JavaScript. That's also quite possible.
My guess is, you want interactivity. In which case, you will need to use JavaScript, which is a client side language.
You hook an event to the input, and check each time it's changed to see if it's empty. If it is, you display another input.
You can see if the file was empty by using:
if ($_FILES['image']['size'] > 0)

PHP and post arrays

Is there anything bad in using post arrays for post variables?
<input type="text" id="stuff" name="stuff[text]"/>
instead of
<input type="text" id="stuff" name="stuff"/>
Tips when to use them?
No, there is no reason not to do this.
However, PHP is pretty much the only language which allows to create arrays like this - so if you ever change your backend to a different language you might have to change things.
It isn't a bad way. Typically programmers use array in such manner to post array with non-determined lenght. Still. Don't know for certain, but when You want to change method to GET then on IE <= 8 is a limit to 2048 chars in address lenght. And dynamic generated array can easy depleat this limit. On other browsers limit is much higher or there is none.
Another drawback of this method is that PHP will preceed correctly, but other server side languages may not. This isn't specified in official HTML docs, as far I know.
So it is more convinient to put it in a single cell in post array, that do subarray. If You want to do some namespacing, then You can write name in such way:
name="styff.text"
as do some of forum engines (for certain Vanilla 2 does).
If it has no diffrence to You I would stay to single variable name in html names. Mostly because of backend.
For tips how to use them I could recommend to use such array to cover dynamic generated content on site. Still it can be handled with normal names, but it is pretty ugly. If we have a case that You want to do a picture adding system, that I would name each input file with "pic[]" and on server iterate whole table.
The same thing for generating documents on client side. I would then do names like "content[][name]" "content[][type]" "content[][value]" and so on. Whathever I woud have in document part class I would throw in this kind of naming, and on server just check is set and do certain things for certain block of document.
This could be talked for a long time since every programmer have own technics and they tend to stick with it. For example I throw a in every form I have on site, and each action is parsed by a generall controller, and then passed by do specific controllers.
Nothing bad. If it is useful four you, use it.

How to create a PHP script that can handle multiple forms

Everytime I create a website for a client, I write the form HTML and then write the php script to handle that data. Sometimes the forms are long, sometimes there are multiple forms - it can get messy.
Before I begin to try and write my dynamic php form handler I'd really like some best practice advice and tips.
I thought of gathering all of the posted variables into an array to handle them. But then how do I know which values were supposed to be required or what they mean?
Maybe something already exists to fix this problem!
Thanks a lot,
Henry
Just a bit more info, what I have in mind is a php script which is flexible enough to work with any form built for it with any amount of inputs. I guess I see it as one file that sits on the server and multiple forms will be sending Ajax requests to it, which it can then satisfy
I don't think you should go with arrays, since the $_POST is already an array anyway.
But what about some sort of naming convention in your code?
ex:
<input type="text" name="txt_username" /> //prefix txt or whatever seems fitting.
Then use regular expressions to find what type of data you expect and act accordingly. You could for example write a class that handles different sorts of input and depending on the prefix in the name-property pass the data to the correct function.
Change the name of you submit button according to your form name. Then in php use a conditional statement to determine which form is posted and get your php working aas you wanted for different forms.
something like this
<input type="submit" name="signupform"/>
in php
if(isset($_POST['signupform']))
{
//Do this
}
elseif(isset($_POST['loginform']))
{
//do this
}
known issue:
You will need to keep those submit names unique and there should not be any other for element in any form being posted to that php file.
You can also add a hidden field
<input type="hidden" name='action' value='signup'/>
and then use a switch case with $_POST['action'] key.
I find it helps to seperate form parts by assigning them to an array e.g.
<input type="text" name="Users[username]" id="Users_username" />
That way, I can easily pick out sections by accessing the array key in php e.g.
$_POST['Users']; // returns username from above example

PHP : checkboxes and name issue

I'm writing, for the first time in PHP, a script to process a POSTed form that contains checkboxes.
As you know, checkboxes are grouped by using the same name attribute, and when multiple values occur, $_POST["some_name"] should be storing those in an array.
I tested but instead of the expected array I got one single value -- that of the last checked item.
At this point, somewhat puzzled, I look at some doc to discover that with PHP the checkbox name should end with []. Is that so? What crazy relation is there expected to exist between an HTML form and a script that processes it? Suppose I don't have access to the HTML field names? Suppose, as it is my case, I have every good reason not to alter my naming scheme just for some obscure nonsense.
Now, it seems there exists at least one solution to the issue: PHP: Retrieving value of checkboxes when name doesn't have array
I'd like to know if there's a really simple way to do without those crazy brackets, and, incidentally, your opinion on this point? Am I the only one shocked?
EDIT : OK, I can certainly work my way around this issue, but I hoped to be raising some (minor yet) fundamental point regarding some sort of "non-separation of concerns" between HTML and PHP. Obviously, that's none of PHP's business how HTML code should be written.
In your html code for the checkboxes:
name="mycheckboxname[]"
You should try with
<input type="checkbox" name="mycheckbox[]" value="Male" />
<input type="checkbox" name="mycheckbox[]" value="Female" />
In php side,
$myCheckBoxCollection = $_REQUEST['mycheckbox'];
What is your good reason for not incorporating [] in your naming scheme ? I don't think this is particularly shocking. As far as PHP is concerned, maybe you actually were expecting to only get the value from the last element with that particular name.
There are some alternatives such as the one you included in your question, or fetching the values with some javascript trickery, but I don't think you will find any that's easier than simply adding brackets to the name of your checkboxes.

Categories