HTML forms: how to get the value of an unchecked checkbox?
I made many HTML forms with Zope but now, I need checkboxes. If I try: <FORM action="handleForm"> <INPUT TYPE="checkbox" NAME="sugar" CHECKED>Sugar? <INPUT TYPE="checkbox" NAME="milk">Milk?<BR> <INPUT TYPE="submit" NAME="submit" VALUE="Order"> </FORM> The Python Script handleForm cannot run if Milk is unchecked. This is because HTML browsers only send the value of the checkbox if it is checked. The URL sent looks like: http://progress.netaktiv.com:9673/Tests/handleForm?sugar=on&submit=Order (do note there is no Milk) and the error message is: --------- Site Error An error was encountered while publishing this resource. Invalid request The parameter, milk, was omitted from the request. Make sure to specify all required parameters, and try the request again. --------- Of course, milk is in the parameters of the Python Script, but how can I say that it is optional and should default to false? Using "milk:boolean" as its name changes nothing. Zope 2.3.2 (source release, python 1.5.2, linux2)
Hi Stephane, one solution is to let zope handle this. Include the following into your form: <input type="hidden" name="sugar:default" value="no"> <input type="hidden" name="milk:default" value="no"> HTH Tino Wildenhain --On Montag, 7. Mai 2001 15:48 +0200 Stephane Bortzmeyer <bortzmeyer@netaktiv.com> wrote:
I made many HTML forms with Zope but now, I need checkboxes. If I try:
<FORM action="handleForm"> <INPUT TYPE="checkbox" NAME="sugar" CHECKED>Sugar? <INPUT TYPE="checkbox" NAME="milk">Milk?<BR> <INPUT TYPE="submit" NAME="submit" VALUE="Order"> </FORM>
The Python Script handleForm cannot run if Milk is unchecked. This is because HTML browsers only send the value of the checkbox if it is checked. The URL sent looks like:
http://progress.netaktiv.com:9673/Tests/handleForm?sugar=on&submit=Order
(do note there is no Milk)
and the error message is:
--------- Site Error
An error was encountered while publishing this resource.
Invalid request The parameter, milk, was omitted from the request.
Make sure to specify all required parameters, and try the request again. ---------
Of course, milk is in the parameters of the Python Script, but how can I say that it is optional and should default to false? Using "milk:boolean" as its name changes nothing.
Zope 2.3.2 (source release, python 1.5.2, linux2)
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Mon, May 07, 2001 at 04:08:47PM +0200, Tino Wildenhain <tino@wildenhain.de> wrote a message of 64 lines which said:
one solution is to let zope handle this. Include the following into your form:
<input type="hidden" name="sugar:default" value="no">
It works, thanks a lot. Where is it documented? ":default" is mentioned in Chapter 8 "Advanced Zope Scripting" of the Zope Book, but, even now I know the trick, I cannot find it in the book. My final solution, with conversion to boolean: <INPUT TYPE="checkbox" NAME="milk:boolean">Milk?<BR> <INPUT type="hidden" name="milk:default" value=""> and the script: ##parameters=beverage,sugar,milk,RESPONSE ##title= ## if milk: milk = " with milk" else: milk = "" ... return "<P>You asked " + beverage + sugar + milk
...or you could try: parameters=beverage="",sugar=0,milk=0,RESPONSE in your python script.
On Mon, May 07, 2001 at 10:43:10AM -0400, marc lindahl <marc@bowery.com> wrote a message of 9 lines which said:
parameters=beverage="",sugar=0,milk=0,RESPONSE
I already tried (it would be better to have a normal HTML form, so that any HTML author can edit it, and to keep all Zope-specific stuff in the Python script) but: SyntaxError: non-default argument follows default argument
parameters=beverage="",sugar=0,milk=0,RESPONSE
I already tried (it would be better to have a normal HTML form, so that any HTML author can edit it, and to keep all Zope-specific stuff in the Python script) but:
SyntaxError: non-default argument follows default argument
Right... parameters= RESPONSE, bevarage="", sugar=0, milk=0 just an ordering problem, I think...
On Mon, 7 May 2001, Tino Wildenhain wrote:
Hi Stephane,
one solution is to let zope handle this. Include the following into your form:
<input type="hidden" name="sugar:default" value="no"> <input type="hidden" name="milk:default" value="no">
Something like this perhaps? <input type="radio" name="sugar:int" value="1"> Yes <input type="radio" name="sugar:int" value="0" checked> No Cheers, Morten
[Stephane Bortzmeyer]
<FORM action="handleForm"> <INPUT TYPE="checkbox" NAME="sugar" CHECKED>Sugar? <INPUT TYPE="checkbox" NAME="milk">Milk?<BR> <INPUT TYPE="submit" NAME="submit" VALUE="Order"> </FORM>
The Python Script handleForm cannot run if Milk is unchecked. This is because HTML browsers only send the value of the checkbox if it is checked. The URL sent looks like:
This has been discussed in this list in the last week or two - look in the archives. There are several approaches: 1) Your dtml or script can check to see if that key is in the form, and act appropriately if it is not there. 2) You can add a hidden input to the form that contains a default value for "milk", like 'no'. Your code checks for the presence of the value you will get when the box is checked. If you use method 2), name the hidden field "milk:default", then its value will be used as the default value if the checkbox is not checked. Cheers, Tom P
On Mon, May 07, 2001 at 10:22:44AM -0400, Thomas B. Passin <tpassin@mitretek.org> wrote a message of 36 lines which said:
1) Your dtml or script can check to see if that key is in the form, and act appropriately if it is not there.
My script cannot check because parameters are apparently tested by Zope before the first line of the script is executed.
[Stephane Bortzmeyer]
Thomas B. Passin <tpassin@mitretek.org> wrote a message of 36 lines which said:
1) Your dtml or script can check to see if that key is in the form, and act appropriately if it is not there.
My script cannot check because parameters are apparently tested by Zope before the first line of the script is executed.
I don't quite know what you are referring to here. The form data is contained in the REQUEST object (i.e., REQUEST.form). I assure you that all the form data from the checkboxes and so on will be in the REQUEST. Zope won't remove any of it. You have to make sure that the REQUEST gets passed to your script, then the script can access it. I haven't used Python scripts so far, if that's what you are using, but I think that the REQUEST comes with the "context" parameter. If not, you should be able to pass it directly to the script. If you were using an external method, you would pass it like so: <dtml-var "yourExternalMethod(REQUEST)">. You should, I think, make sure that you know how to send the REQUEST object to your script, then everything else should easy. Cheers, Tom P
On Tue, May 08, 2001 at 10:07:30AM -0400, Thomas B. Passin <tpassin@mitretek.org> wrote a message of 38 lines which said:
My script cannot check because parameters are apparently tested by Zope before the first line of the script is executed.
I don't quite know what you are referring to here.
When you declare parameters to a Python Script, the existence of these parameters is tested by Zope before your script gets executed. So, you cannot check them in your script, they have to be right in the first place. Here is the declaration: ## Script (Python) "handleForm" ##bind container=container ##bind context=context ##bind namespace=REQUEST ##bind script=script ##bind subpath=traverse_subpath ##parameters=beverage,sugar,milk,RESPONSE ##title= ## RESPONSE.setHeader('Content-Type', 'text/html') if milk: milk = " with milk" else: ...
The form data is contained in the REQUEST object (i.e., REQUEST.form). I assure you that all the form data from the checkboxes and so on will be in the REQUEST. Zope
OK, the examples in the Zope Book (is there a better source of information?) do not mention it, they use parameters of the script. I cannot find REQUEST.form in the Zope API at the end of the Zope book. But, now that I know it exists, a search in zope.org produced severl examples. I have now a better version, where the form is 100% standard HTML form, and the script is: ## Script (Python) "handleForm" ##bind container=container ##bind context=context ##bind namespace=REQUEST ##bind script=script ##bind subpath=traverse_subpath ##parameters=beverage,RESPONSE,REQUEST ##title= ## RESPONSE.setHeader('Content-Type', 'text/html') if REQUEST.form.has_key('milk'): milk = " with milk" else: milk = "" if REQUEST.form.has_key('sugar'): sugar = " with sugar" else: sugar = "" return "<P>You asked for " + beverage + sugar + milk Funny there are so many ways to do something so simple. Thanks a lot, I find it much better now.
[Stephane Bortzmeyer]
Thomas B. Passin <tpassin@mitretek.org> wrote
The form data is contained in the REQUEST object (i.e., REQUEST.form). I assure you that all the form data from the checkboxes and so on will be in the REQUEST. Zope ...
I have now a better version, where the form is 100% standard HTML form, and the script is: ... if REQUEST.form.has_key('milk'): milk = " with milk" else: milk = "" if REQUEST.form.has_key('sugar'): sugar = " with sugar" else: sugar = "" return "<P>You asked for " + beverage + sugar + milk
[Tom] OK, you've got it. Well done. Cheers, TomP
* Stephane Bortzmeyer <bortzmeyer@netaktiv.com> [2001-05-08 22:10]:
OK, the examples in the Zope Book (is there a better source of information?) do not mention it, they use parameters of the script.
In the final edition of the Zope Book, you can read this on page 127 (Chapter 8, "Advanced Zope Scripting"): |In addition to form variables, you can specify any request variables as |script parameters. For example, to get access to the request and |response objects just include REQUEST and RESPONSE in your list of |parameters. Request variables are detailed more fully in Appendix B.
##bind namespace=REQUEST
It's probably a bad idea to call the DTML namespace "REQUEST", because...
##bind script=script ##bind subpath=traverse_subpath ##parameters=beverage,RESPONSE,REQUEST
... your REQUEST is coming from here. Obviously you are not using the DTML name space object in your Python Script because it's overwritten (Correct English?) by your parameter with the same name. So you can leave the namespace line empty. Cheers, Nils
participants (6)
-
marc lindahl -
Morten W. Petersen -
Nils Kassube -
Stephane Bortzmeyer -
Thomas B. Passin -
Tino Wildenhain