|
|
�� 52. PHP and HTML
PHP and HTML interact a lot: PHP can generate HTML, and HTML
can pass information to PHP.
- 1.
What encoding/decoding do I need when I pass a value through a form/URL?
- 2.
I'm trying to use an <input type="image"> tag, but
the $foo.x and $foo.y variables aren't available.
Where are they?
- 3. How do I create arrays in a HTML <form>?
- 4.
How do I get all the results from a select multiple HTML tag?
1.
What encoding/decoding do I need when I pass a value through a form/URL?
There are several stages for which encoding is important. Assuming that
you have a string $data, which contains
the string you want to pass on in a non-encoded way, these are the
relevant stages:
HTML interpretation. In order to specify a random string, you
must include it in double quotes, and
htmlspecialchars the whole value.
URL: A URL consists of several parts. If you want your data to be
interpreted as one item, you must encode it with
urlencode().
���� 52-1. A hidden HTML form element <?php
echo "<input type=hidden value=\"" . htmlspecialchars($data) . "\">\n";
?> |
|
ע:
It is wrong to urlencode()
$data, because it's the browsers responsibility to
urlencode() the data. All popular browsers do that
correctly. Note that this will happen regardless of the method (i.e.,
GET or POST). You'll only notice this in case of GET request though,
because POST requests are usually hidden.
���� 52-2. Data to be edited by the user <?php
echo "<textarea name=mydata>\n";
echo htmlspecialchars($data)."\n";
echo "</textarea>";
?> |
|
ע:
The data is shown in the browser as intended, because the browser will
interpret the HTML escaped symbols.
Upon submitting, either via GET or POST, the data will be urlencoded
by the browser for transferring, and directly urldecoded by PHP. So in
the end, you don't need to do any urlencoding/urldecoding yourself,
everything is handled automagically.
���� 52-3. In an URL <?php
echo "<a href=\"" . htmlspecialchars("/nextpage.php?stage=23&data=" .
urlencode($data)) . "\">\n";
?> |
|
ע:
In fact you are faking a HTML GET request, therefore it's necessary to
manually urlencode() the data.
ע:
You need to htmlspecialchars() the whole URL, because the
URL occurs as value of an HTML-attribute. In this case, the browser
will first un-htmlspecialchars() the value, and then pass
the URL on. PHP will understand the URL correctly, because you
urlencoded() the data.
You'll notice that the & in the URL is replaced
by &. Although most browsers will recover
if you forget this, this isn't always possible. So even if your URL is
not dynamic, you need to
htmlspecialchars() the URL.
2.
I'm trying to use an <input type="image"> tag, but
the $foo.x and $foo.y variables aren't available.
Where are they?
When submitting a form, it is possible to use an image instead of
the standard submit button with a tag like:
<input type="image" src="image.gif" name="foo"> |
When the user clicks somewhere on the image, the accompanying form
will be transmitted to the server with two additional variables:
foo.x and foo.y.
Because $foo.x and $foo.y are invalid variable names in PHP, they are
automagically converted to $foo_x and $foo_y. That is, the periods
are replaced with underscores.
3. How do I create arrays in a HTML <form>?
To get your <form> result sent as an
array to your PHP script
you name the <input>, <select> or <textarea>
elements like this:
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]"> |
Notice the square brackets after the variable name, that's what
makes it an array. You can group the elements into different arrays
by assigning the same name to different elements:
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyOtherArray[]">
<input name="MyOtherArray[]"> |
This produces two arrays, MyArray and MyOtherArray, that gets sent
to the PHP script. It's also possible to assign specific keys
to your arrays:
<input name="AnotherArray[]">
<input name="AnotherArray[]">
<input name="AnotherArray[email]">
<input name="AnotherArray[phone]"> |
The AnotherArray array will now contain the keys 0, 1, email and phone.
ע:
Specifying an arrays key is optional in HTML. If you do not specify
the keys, the array gets filled in the order the elements appear in
the form. Our first example will contain keys 0, 1, 2 and 3.
See also
Array Functions and
Variables from outside PHP.
4.
How do I get all the results from a select multiple HTML tag?
The select multiple tag in an HTML construct allows users to
select multiple items from a list. These items are then passed
to the action handler for the form. The problem is that they
are all passed with the same widget name. ie.
<select name="var" multiple> |
Each selected option will arrive at the action handler as:
var=option1
var=option2
var=option3 |
Each option will overwrite the contents of the previous
$var variable. The solution is to use
PHP's "array from form element" feature. The following
should be used:
<select name="var[]" multiple> |
This tells PHP to treat $var as an array and
each assignment of a value to var[] adds an item to the array.
The first item becomes $var[0], the next
$var[1], etc. The count()
function can be used to determine how many options were selected,
and the sort() function can be used to sort
the option array if necessary.
Note that if you are using JavaScript the []
on the element name might cause you problems when you try to
refer to the element by name. Use it's numerical form element
ID instead, or enclose the variable name in single quotes and
use that as the index to the elements array, for example:
variable = documents.forms[0].elements['var[]']; |
User Contributed Notes PHP and HTML |
|
02-Feb-2002 07:38 |
|
Take care when generating a select from an echo - the following does not
work:
echo "<select name=\"$name[]\" multiple
size=$sz>";
The is because the [] os evaluated as part of an array expression.
Instead do:
echo "<select name=\"".$name."[]\" multiple
size=$sz>";
|
|
06-Mar-2002 09:08 |
|
I didn't think that the '[' & ']' characters were valid for name
attributes.
see
|
|
25-Mar-2002 12:19 |
|
I just did a script that generated an array from a form by creating the
input tags using name=array[x] and it worked fine... if its not supposed
to work... well it does. Just incase you were looking for confirmation.
|
|
29-Mar-2002 10:23 |
|
Remember,
the values of the listbox should be selected before they are transmitted.
Of course, to the casual observer this is trival as hell.
But: if you're working with multiple listboxes let's say for transferring
values from one box to the other (anyone know a better way of updating a
many-to-many relation?) this is easily overlooked.
well.. okay... at least it happened to me.
So: in this situation, make sure that there's a JavaScript that selects
all values once the submit button is pressed.
|
|
23-May-2002 07:12 |
|
Errors in previous posts:
$name[] is not evaluated for presence "[" and "]" but
because of "$" in the front what is absolutely wrong for general
reasons. Don't be surprised. Sipmly, $anything is a variable.
I think "[" and "]" iare valid for name atributes
according to
That's all folks,
use arrays and prosper.
|
|
24-May-2002 02:01 |
|
You can also use this to create multidimensional arrays:
<input type="text" name="bob[]">
<input type="text" name="bob[1][]">
makes an array of:
bob[0] = some text
bob[1] = an array
bob[1][0] = some text
Fun stuff, very helpful in submitting an entire form and putting all the
data into a single array rather than naming each element....
|
|
25-May-2002 09:30 |
|
I think '[' and ']' are valid characters for name attributes.
-> InputType of 'name' attribute is 'CDATA'(not 'NAME' type)
-> about CDATA('name' attribute is not 'NAME' type!)
...CDATA is a sequence of characters from the document character set and
may include character entities...
--> about character entity references in HTML 4
([ - [, ] - ])
|
|
03-Jul-2002 02:19 |
|
I'm using an NT Server, IIS and mySQL.
When processing posted variables, I kept getting the message on my
browser;
Warning: Use of undefined constant name - assumed 'name' in
<script.php>
The warning messages was created by using the code; $name1 = $_POST[name];
To stop this warning message, I used the code; $name1 = $_POST['name'];
|
|
04-Jul-2002 06:07 |
|
How can I pass ALL the values from a multiple select list? Using <select
name="myArray[]" ...> will only send the selected elements.
Is there a way to pass every element? I can do it through javascript by
setting all elements as selected and then submitting. But is there a
better way?
|
|
04-Jul-2002 08:58 |
|
To send all values from the multiple select, you must all select with a
thing like this (javascript) :
<form OnSubmit="
for (i=0; i<SelectArray.length; i++){
SelectArray.options[i].selected = true;
}
">
|
|
04-Jul-2002 09:01 |
|
sorry... i know no better way
only the selected items'll be send
|
|
06-Jul-2002 05:35 |
|
whay don't use <input type=hidden name=name[] value=value>
|
|
12-Jul-2002 09:06 |
|
To the gentleman who was having problems processing POST variables:
$name1 = $_POST[name];
...is always wrong, unless name is some constant you've defined. If
you're looking for the form variable called name, you have to use 'name'.
Read the manual under Basic Language Reference, where it says "Why is
$foo[bar] wrong?"
Incidentally, none of this has much to do with getting arrays from your
POSTDATA, per se.
|
|
16-Jul-2002 04:44 |
|
I've found that a neat trick is to do:
<form onSubmit="selection.name=selection.name + '[]'">
That way you can still do form.selection, instead of
form.elements['selection[]'] and still capture multiple variables.
|
|
19-Jul-2002 12:17 |
|
Hi. I am new to PHP. I try to make PHP work with pws. Here is the problem
I am facing.
I create a text box in an HTML file and give it the name Comment. I type
the comments into the box and call a php script file to insert the
comments into mysql database. Normally, php engine would create a
variable with the name $Comment. However, it seems it doesn't work in my
examples because when I echo the $variable, it shows it is empty. What is
going on? Any suggestion?
|
|
23-Jul-2002 01:35 |
|
is there any software that generates PHP webpages with writind code?
meaning a software where I can design like (MS frontpage)
|
|
23-Jul-2002 07:35 |
|
I am trying to pass a php string $temp
in a applet argument. ex :
DataSet1= <? $temp ?>;
and if I do a view source, it shows this :
DataSet1= ;
And I know for a fact that $temp has indeed info in there... Does anybody
know what's going on?
|
|
|
| |