php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23746 Default values do not work when $_POST is set
Submitted: 2003-05-21 18:49 UTC Modified: 2003-05-23 04:46 UTC
From: nlenz at ivcf dot org Assigned: mansion (profile)
Status: Closed Package: PEAR related
PHP Version: 4.3.1 OS: Linux
Private report: No CVE-ID: None
 [2003-05-21 18:49 UTC] nlenz at ivcf dot org
Description of problem:
1. $_POST["someUnrelatedVar"] is set.
2. Create a QuickForm object using POST 
3. Call setDefaults()
4. Default values will not show up, even though the element's name was not set in $_POST.

Solution:
In the element.php file, onQuickFormEvent method.

Change:
if (isset($caller->_submitValues) && count($caller->_submitValues) > 0) {

To:
if (isset($caller->_submitValues) && count($caller->_submitValues) > 0 && $this->_findValue($caller->_submitValues) != null) {

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-22 04:45 UTC] mansion@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2003-05-22 17:58 UTC] nlenz at ivcf dot org
The solution I proposed was wrong. If an empty string is submitted to a form, the form will keep the default value instead of using the empty string.

!== should have been used instead of !=.
 [2003-05-23 04:35 UTC] mansion@php.net
!== was used in CVS.
But Alexey Borzov told me he had a better idea so wait and see.
 [2003-05-23 04:46 UTC] avb@php.net
The idea was related to fixing group.php for correct checkbox group behaviour, not to this bug. I think we can close this.
 [2003-07-13 13:30 UTC] peter446 at hotmail dot com
I think the current behaviour of QuickForm checkboxes is still not using default values for checkboxes when it should be.

The following code run on PHP 4.3.1 with version 3.0 of HTML_QuickForm ($Id: checkbox.php,v 1.14 2003/05/16 17:21:10 avb Exp $)

========script1.php========I think the current behaviour of QuickForm checkboxes is still not using default values for checkboxes 

when it should be.

The following code run on PHP 4.3.1 with version 3.0 of HTML_QuickForm ($Id: checkbox.php,v 1.14 

2003/05/16 17:21:10 avb Exp $) results in both checkboxes being displayed unchecked.

To reproduce, open form1.php in browser, submit the form and form2.php will be displayed with both check boxes unticked. Opening script2.php directly (without POST data) will result in the first checkbox being checked (correct behavour).

========form1.php========

<html>
<body>
<form action="form2.php" method="post">
  <input name="foo" value="random data" type="hidden">
  <input name="send" value="Send" type="submit">
</form>
</body>
</html>

========form2.php========

<?php

require_once ("HTML/QuickForm.php");
$form = new HTML_QuickForm('frmTest', 'post');

$form->addElement('checkbox', 'test1', 'Test 1:');
$form->addElement('checkbox', 'test2', 'Test 2:');

$defaultVals['test1']  = true;
$defaultVals['test2']  = false;

$form->setDefaults($defaultVals);
$form->display();

?>

As I understand it a possialbe fix would be to apply the following patch to checkbox.php,v 1.14. As I do not fully understand QuickForm there may be reasons why this patch is a bad idea.

Email me if you wish to dicuss the patch further.

I am very impressed with the quality of HTML_QuickForm and would like to assist you in making it even better.

Peter Jackson

========Proposed Patch========

--- checkbox.php        Sun Jul 13 19:18:11 2003
+++ checkbox.php.mod    Sun Jul 13 19:18:11 2003
@@ -257,9 +257,10 @@
                 // default values are overriden by submitted
                 $value = $this->_findValue($caller->_constantValues);
                 if (null === $value) {
-                    // if no boxes were checked, then there is no value in the array
-                    // yet we don't want to display default value in this case
-                    if (isset($caller->_submitValues) && 0 < count($caller->_submitValues)) {
+                    // use the submitted value only if a value of the
+                    // same name was submitted, otherwise use default
+                    if (isset($caller->_submitValues) && 0 < count($caller->_submitValues)
+                        && $this->_findValue($caller->_submitValues) !== null) {
                         $value = $this->_findValue($caller->_submitValues);
                     } else {
                         $value = $this->_findValue($caller->_defaultValues);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC