Thread: PHP, HTML Forms & PostgreSQL

PHP, HTML Forms & PostgreSQL

From
Leif Jensen
Date:
    Hi all,

  I'm doing some intranet pages using PHP & PostgreSQL. (Our corporate
database is 'of course' using PostgreSQL ;-). I like this interface and
have had great help in looking at PgAdmin, which I have been using from
time to time.

  I have some problems with HTML forms and how they interface to PHP, and
I haven't been able to locate any description of this (neither in the HTML
manuals or in the PHP manuals). From testing and looking at PgAdmin, I
figured out how to name variables and assign values.

  However, my specific problem is using checkboxes:

1) If a checkbox is unchecked, I'm not getting any values,

2) If a checkbox is checked, I'm getting the value 'on' where I need 't'
in the update sql statement.

  Is there any way to make sure the variable (checkbox) will appear in the
php action when the value changes (or every time)? Is there a way to make
the form 'return' 't' in stead of 'on'?


  I'm using

PHP 4.1.1
PostgreSQL 7.2.1
Apache 1.3.23


    Please help,

 Leif


Re: PHP, HTML Forms & PostgreSQL

From
Martijn van Oosterhout
Date:
On Mon, Apr 08, 2002 at 11:26:47AM +0200, Leif Jensen wrote:
>
>     Hi all,
>
>   I'm doing some intranet pages using PHP & PostgreSQL. (Our corporate
> database is 'of course' using PostgreSQL ;-). I like this interface and
> have had great help in looking at PgAdmin, which I have been using from
> time to time.
>
>   I have some problems with HTML forms and how they interface to PHP, and
> I haven't been able to locate any description of this (neither in the HTML
> manuals or in the PHP manuals). From testing and looking at PgAdmin, I
> figured out how to name variables and assign values.

Lookup the HTML specs, I think they say something about this. It is defined
somewhere.

>   However, my specific problem is using checkboxes:
>
> 1) If a checkbox is unchecked, I'm not getting any values,
>
> 2) If a checkbox is checked, I'm getting the value 'on' where I need 't'
> in the update sql statement.
>
>   Is there any way to make sure the variable (checkbox) will appear in the
> php action when the value changes (or every time)? Is there a way to make
> the form 'return' 't' in stead of 'on'?

Nope, HTML just works that way. It works like that on every browser i've
seen. Remember, those values come from the browser, not anything you can
control.

Note that the value= part of the checkbox controls the value returned when
ticked.

Something like:

if( $box != "t" )
  $box = "f"

all done.

HTH,
--
Martijn van Oosterhout <kleptog@svana.org>   http://svana.org/kleptog/
> Ignorance continues to thrive when intelligent people choose to do
> nothing.  Speaking out against censorship and ignorance is the imperative
> of all intelligent people.

Re: PHP, HTML Forms & PostgreSQL

From
Leif Jensen
Date:
   Hi Martijn,

  Thx for your quick reply.



On Mon, 8 Apr 2002, Martijn van Oosterhout wrote:

> On Mon, Apr 08, 2002 at 11:26:47AM +0200, Leif Jensen wrote:
> >
> >     Hi all,
> >
> >   I'm doing some intranet pages using PHP & PostgreSQL. (Our corporate
> > database is 'of course' using PostgreSQL ;-). I like this interface and
> > have had great help in looking at PgAdmin, which I have been using from
> > time to time.
> >
> >   I have some problems with HTML forms and how they interface to PHP, and
> > I haven't been able to locate any description of this (neither in the HTML
> > manuals or in the PHP manuals). From testing and looking at PgAdmin, I
> > figured out how to name variables and assign values.
>
> Lookup the HTML specs, I think they say something about this. It is defined
> somewhere.

   I have looked in the HTML 4.0 recommendation, which talks about how
form data is being handled, but it doesn't say anything about how PHP
treats the result.

>
> >   However, my specific problem is using checkboxes:
> >
> > 1) If a checkbox is unchecked, I'm not getting any values,
> >
> > 2) If a checkbox is checked, I'm getting the value 'on' where I need 't'
> > in the update sql statement.
> >
> >   Is there any way to make sure the variable (checkbox) will appear in the
> > php action when the value changes (or every time)? Is there a way to make
> > the form 'return' 't' in stead of 'on'?
>
> Nope, HTML just works that way. It works like that on every browser i've
> seen. Remember, those values come from the browser, not anything you can
> control.
>
> Note that the value= part of the checkbox controls the value returned when
> ticked.
>
> Something like:
>
> if( $box != "t" )
>   $box = "f"
>
> all done.

   That's a possibility if you on forehand know the name(s) of checkboxes
in your form. I was trying to make a general sql construction of an update
statement independent of what might be in the form. !?


   Greetings,

 Leif


Re: PHP, HTML Forms & PostgreSQL

From
Martijn van Oosterhout
Date:
On Mon, Apr 08, 2002 at 12:52:48PM +0200, Leif Jensen wrote:
>    I have looked in the HTML 4.0 recommendation, which talks about how
> form data is being handled, but it doesn't say anything about how PHP
> treats the result.

Hmm, I'm not sure then, but this is the way I've always known it to work.

>    That's a possibility if you on forehand know the name(s) of checkboxes
> in your form. I was trying to make a general sql construction of an update
> statement independent of what might be in the form. !?

Well, I don't know if you can get the variables in a hash, like in perl.
Another thing that sometimes works is:

<input type=hidden   name=box value=f>
<input type=checkbox name=box value=t>

When the tickbox is ticked, the browser will return two values named "box",
which *may* cancel out to give you your t/f combo.

Note this is extremely system specific and not guarenteed and not
recommended. But I have used it successfully on occasion.

--
Martijn van Oosterhout <kleptog@svana.org>   http://svana.org/kleptog/
> Ignorance continues to thrive when intelligent people choose to do
> nothing.  Speaking out against censorship and ignorance is the imperative
> of all intelligent people.

Re: PHP, HTML Forms & PostgreSQL

From
Leif Jensen
Date:
   Hi again,


On Mon, 8 Apr 2002, Martijn van Oosterhout wrote:

> On Mon, Apr 08, 2002 at 12:52:48PM +0200, Leif Jensen wrote:
> >    I have looked in the HTML 4.0 recommendation, which talks about how
> > form data is being handled, but it doesn't say anything about how PHP
> > treats the result.
>
> Hmm, I'm not sure then, but this is the way I've always known it to work.
>
> >    That's a possibility if you on forehand know the name(s) of checkboxes
> > in your form. I was trying to make a general sql construction of an update
> > statement independent of what might be in the form. !?
>
> Well, I don't know if you can get the variables in a hash, like in perl.

   I have noticed that all the 'successful controls' are listed in the
_GET array or the _POST array ! But their type is unknown. From here I can
scan and generate my sql update statement!

> Another thing that sometimes works is:
>
> <input type=hidden   name=box value=f>
> <input type=checkbox name=box value=t>
>
> When the tickbox is ticked, the browser will return two values named "box",
> which *may* cancel out to give you your t/f combo.
>
> Note this is extremely system specific and not guarenteed and not
> recommended. But I have used it successfully on occasion.
>

   This might be worth a try, but as you say non-standard. In the tests I
have done, PHP (or maybe it's rather the user agent!?) puts the controls
in corresponding variables in the order they are listed in the form. And
what happens to the value when the user toggles the checkbox ?

   Thanks,

 Leif


Re: PHP, HTML Forms & PostgreSQL

From
"Nigel J. Andrews"
Date:
On Mon, 8 Apr 2002, Martijn van Oosterhout wrote:

> On Mon, Apr 08, 2002 at 12:52:48PM +0200, Leif Jensen wrote:
> >    I have looked in the HTML 4.0 recommendation, which talks about how
> > form data is being handled, but it doesn't say anything about how PHP
> > treats the result.
>
> Hmm, I'm not sure then, but this is the way I've always known it to work.

I've never used PHP but surely the HTML specs. not having anything to say about
PHP should not be a surprise? Isn't PHP a scripting language whose only
connection with HTML is that it is embedded within HTML?

>
> >    That's a possibility if you on forehand know the name(s) of checkboxes
> > in your form. I was trying to make a general sql construction of an update
> > statement independent of what might be in the form. !?
>
> Well, I don't know if you can get the variables in a hash, like in perl.
> Another thing that sometimes works is:
>
> <input type=hidden   name=box value=f>
> <input type=checkbox name=box value=t>
>
> When the tickbox is ticked, the browser will return two values named "box",
> which *may* cancel out to give you your t/f combo.
>
> Note this is extremely system specific and not guarenteed and not
> recommended. But I have used it successfully on occasion.

As Martijn says, this is all browser dependent stuff. The whole web thing works
with servers not assuming anything about the client and you don't really want
to start doing so.

As for the general form handling issue how about having a table(s) holding
definitions of forms which the form display page and the form target page can
retrieve and do stuff with? Although generally aren't you going to want
specialisations for most form handlers to implement their own validy checks
etc. which will require knowledge in the PHP code somewhere about the form's
details? Although for that I suppose it could be done client side in javascript
held in the form definition table(s) but then you're back to the form handler
making assumptions about the client.


--
Nigel J. Andrews
Director

---
Logictree Systems Limited
Computer Consultants


Re: PHP, HTML Forms & PostgreSQL

From
Frank Hilliard
Date:
In HTML, unchecked checkboxes have no value. So you have to give them a
value in the issuing page with hidden form fields or on the action page
by setting a default variable value before your SGL insert query.

Frank Hilliard
http://frankhilliard.com



Re: PHP, HTML Forms & PostgreSQL

From
"S Dawalt"
Date:
>
> On Mon, 8 Apr 2002, Martijn van Oosterhout wrote:
>
> > On Mon, Apr 08, 2002 at 12:52:48PM +0200, Leif Jensen wrote:
> > >    I have looked in the HTML 4.0 recommendation, which talks about
how
> > > form data is being handled, but it doesn't say anything about how PHP
> > > treats the result.
> >
> > Hmm, I'm not sure then, but this is the way I've always known it to
work.
>
> I've never used PHP but surely the HTML specs. not having anything to say
about
> PHP should not be a surprise? Isn't PHP a scripting language whose only
> connection with HTML is that it is embedded within HTML?

  True, PHP is a completely different beast from HTML. The HTML spec will
not talk about it. Go to www.php.net and look at the manual. There's lots
of good info there.

  Insofar as return values, you can use the "value=" clause of the checkbox
to return whatever value you want when it has been "checked". If you want
the checkbox to return something when it hasn't been checked, then prior to
defining the checkbox, define a hidden type of the same name as your
checkbox, but place the "unchecked" value in it. If the checkbox isn't
checked then your default value gets returned by the browser, otherwise the
value specified within the checkbox definition is returned.  I think I
found this on the PHP website several months ago.  It works quite well.

  Shane




Re: PHP, HTML Forms & PostgreSQL

From
"Nigel J. Andrews"
Date:
On Mon, 8 Apr 2002, S Dawalt wrote:
>
>   Insofar as return values, you can use the "value=" clause of the checkbox
> to return whatever value you want when it has been "checked". If you want
> the checkbox to return something when it hasn't been checked, then prior to
> defining the checkbox, define a hidden type of the same name as your
> checkbox, but place the "unchecked" value in it. If the checkbox isn't
> checked then your default value gets returned by the browser, otherwise the
> value specified within the checkbox definition is returned.  I think I
> found this on the PHP website several months ago.  It works quite well.

Ok, but once again this is surely relying on browsers 'playing ball'. What
happens if I use a browser that returns the hidden parameter with it's value
whether or not the checkbox is checked, as it should? I'm guessing that since
you mention the order in the form this is actually relying on the browser
returning parameters in the order they occur within the form and the server
providing only the latest definition to the application, the PHP action
page. So what happens if I use a browser which returns parameters in some other
order as one is perfectly entitled to do? What happens if the web server
implements a 'first come first served' parameter setting scheme?

Any, back to the original desire and to keep some what on topic, how about
using a function installed in the DB server to perform the data validation? Or
is the problem that PHP can't provide a null parameter to a function if there
isn't a variable, from the form parameter, defined at the time of the
call? Another or: isn't this also not helping towards the general form handling
idea that is the real aim?


--
Nigel J. Andrews
Director

---
Logictree Systems Limited
Computer Consultants