473,287 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Capturing HTML form field names even when they are blank

JDS
Hi, all. I'd like to do the following, preferably *without* resorting to
javascript:

I have a long, dynamically-generated form questionnaire. Not all of the
form fields are dynamically generated, though.

I'd like to capture the NAME of every HTML form field element on the
server, even if that element is submitted blank. The trouble is, with,
say, radio buttons or checkboxes for example, a *blank* element does not
get submitted at all.

Example:
<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
</form>

text.php
<?php
print_r($_REQUEST);
?>

If the form is submitted completely blank, text.php prints out:

Array
(
[thetextbox] =>
)

ONLY if I click a value on one of the radio buttons do I get the field
"firstbutton", e.g.:

Array
(
[thetextbox] =>
[firstbutton] => 1
)

How can I get the names of all of the fields in the HTML form even if they
are sent blank? I am considering using JavaScript onSubmit() to put fake
values in for blank fields, but using JS is not desirable. I had been
putting hidden fields in with the field name "field_names[nameoffield]"
for each form field but this does not scale well for any non-dynamic form
fields.

Any other ideas?

--
JDS | je*****@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #1
7 3175
DH
JDS wrote:
Hi, all. I'd like to do the following, preferably *without* resorting to
javascript:

I have a long, dynamically-generated form questionnaire. Not all of the
form fields are dynamically generated, though.

I'd like to capture the NAME of every HTML form field element on the
server, even if that element is submitted blank. The trouble is, with,
say, radio buttons or checkboxes for example, a *blank* element does not
get submitted at all.

Example:
<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
</form>

text.php
<?php
print_r($_REQUEST);
?>

If the form is submitted completely blank, text.php prints out:

Array
(
[thetextbox] =>
)

ONLY if I click a value on one of the radio buttons do I get the field
"firstbutton", e.g.:

Array
(
[thetextbox] =>
[firstbutton] => 1
)

How can I get the names of all of the fields in the HTML form even if they
are sent blank? I am considering using JavaScript onSubmit() to put fake
values in for blank fields, but using JS is not desirable. I had been
putting hidden fields in with the field name "field_names[nameoffield]"
for each form field but this does not scale well for any non-dynamic form
fields.

Any other ideas?


Perhaps this will help, or give you some ideas:

$buffer = '';

while(list($key, $val) = each($_POST)){
if(!is_array($val)){
$val = stripslashes(trim($val));
$buffer .= $key.': '.$val."\n";
}else{
foreach(array_keys($val) as $key){
$val[$key] = stripslashes(trim($val));
$buffer .= $val[$key].': '.$val."\n";
}
}
}

echo "\n".nl2br($buffer);
Jul 17 '05 #2

JDS wrote:

[snip]
Example:
<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
</form>


To do what you want, you have to initialize the fields using
"type=hidden". For example, using your example code:
<form action="text.php" method="POST">
<input type="hidden" name="firstbutton" value="0">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="3">
<input type="text" name="thetextbox">
</form>

Now when someone submits the form without selecting anything, you will
get a value of "0" in the variable $_POST['firstbutton'].

You should be able to extend this technique to your real form.

Ken

Jul 17 '05 #3
JDS wrote:
I'd like to capture the NAME of every HTML form field element on the
server, even if that element is submitted blank. The trouble is, with,
say, radio buttons or checkboxes for example, a *blank* element does not
get submitted at all.
Well sorry, you can't.
Example:
<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
</form>


<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
<input type="hidden" name="FIELDS" value="thetextbox,firstbutton">
</form>
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jul 17 '05 #4
On Mon, 07 Feb 2005 14:50:51 -0500, JDS <je*****@go.away.com> wrote:
Hi, all. I'd like to do the following, preferably *without* resorting to
javascript:

I have a long, dynamically-generated form questionnaire. Not all of the
form fields are dynamically generated, though.

I'd like to capture the NAME of every HTML form field element on the
server, even if that element is submitted blank. The trouble is, with,
say, radio buttons or checkboxes for example, a *blank* element does not
get submitted at all.
This behaviour is required by the HTML standard:

http://www.w3.org/TR/html401/interac...html#h-17.13.2
http://www.w3.org/TR/html401/interact/forms.html#radio
Example:
<form action="text.php">
<input type="radio" name="firstbutton" value="1">
<input type="radio" name="firstbutton" value="2">
<input type="radio" name="firstbutton" value="2">
<input type="text" name="thetextbox">
</form>

If the form is submitted completely blank, text.php prints out:

Array
(
[thetextbox] =>
)

ONLY if I click a value on one of the radio buttons do I get the field
"firstbutton", e.g.:

Array
(
[thetextbox] =>
[firstbutton] => 1
)

How can I get the names of all of the fields in the HTML form even if they
are sent blank? I am considering using JavaScript onSubmit() to put fake
values in for blank fields, but using JS is not desirable. I had been
putting hidden fields in with the field name "field_names[nameoffield]"
for each form field but this does not scale well for any non-dynamic form
fields.


Adding extra elements is AFAIK the only method, given that HTML requires that
non-selected radio buttons are not successful.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5
JDS
On Mon, 07 Feb 2005 20:50:37 +0000, Toby Inkster wrote:
Well sorry, you can't.


Allright. Thanks all. I had been using hidden fields, anyways, which was
fine for the dynamically-generated form fields because I can just have the
hidden fields dynamically generated as well.

However, manually putting in hiddens for the other fields does not scale
well and is prone to errors. Oh well, I'll figure something out.

Yum, coffee.

--
JDS | je*****@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #6
JDS wrote:
On Mon, 07 Feb 2005 20:50:37 +0000, Toby Inkster wrote:

Well sorry, you can't.

Allright. Thanks all. I had been using hidden fields, anyways, which was
fine for the dynamically-generated form fields because I can just have the
hidden fields dynamically generated as well.

However, manually putting in hiddens for the other fields does not scale
well and is prone to errors. Oh well, I'll figure something out.


Read the HTML spec. For radio buttons, one option must *always*
be selected. Therefore, the case you site should not happen,
there should always be one selected and so one should always be
submitted.

<URL:http://www.w3.org/TR/html401/interact/forms.html#radio>

radio buttons

Radio buttons are like checkboxes except that when several share
the same control name , they are mutually exclusive: when one is
switched "on", all others with the same name are switched "off".
The INPUT element is used to create a radio button control.

If no radio button in a set sharing the same control name is
initially "on", user agent behavior for choosing which control
is initially "on" is undefined. Note. Since existing
implementations handle this case differently, the current
specification differs from RFC 1866 ( [RFC1866] section
8.1.2.4), which states:

At all times, exactly one of the radio buttons in a set is
checked. If none of the <INPUT> elements of a set of radio
buttons specifies `CHECKED', then the user agent must check
the first radio button of the set initially.

Since user agent behavior differs, authors should ensure that in
each set of radio buttons that one is initially "on".

*Note the last sentence.*
--
Fred
Jul 17 '05 #7
JDS
On Wed, 09 Feb 2005 01:49:27 +1000, Fred Oz wrote:
Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".

*Note the last sentence.*


Great! Thanks for the info! This is very enlightening.

--
JDS | je*****@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
4
by: Andrew Williams | last post by:
Does anyone know of a way to quickly validate an HTML web form? I need to make sure that certain fields on my form actually contain data and are not submitted blank. I have tried using...
7
by: JDS | last post by:
Hi, all. I'd like to do the following, preferably *without* resorting to JavaScript: I have a long, dynamically-generated form questionnaire. Not all of the form fields are dynamically...
33
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if...
4
by: kathy | last post by:
Working in a form, I have an Iif statement where if a date is not filled in, another field will show as a blank. If the date IS filled in, the 2nd field will show its actual value. The following...
2
by: Chris Windsor | last post by:
I hope the following describe what I'm trying to do: I have created a tool to be used by product analysts when studying different cell phone designs. Part of the tool is a set of 11 forms on a...
0
by: lawrenceS59 | last post by:
Hi all, I'm fairly new to web development so bare with me. The html page that i've created isn't working and i can't figure out why. I'm guessing there are some rules that need to be followed...
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.