FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » test if $_POST variable is set
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
test if $_POST variable is set [message #175831] Sat, 29 October 2011 05:40 Go to next message
cerr is currently offline  cerr
Messages: 33
Registered: September 2010
Karma: 0
Member
Hi There,

I have a form with various checkboxes, they got names i have from a db
to identify them.
Now I would like to check what has been posted by the form. For that I
would like to read all the field names from the db and check if they
have been set something like this:

$result = mysql_query("SELECT * FROM specialty");
while($row = mysql_fetch_array($result)) {
if (isset($_POST[$row['name']])) {
var_dump($POST);
session_unset();
}

But this doesn't seem to work the way I expected it to? I.e. I would
liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
name from the database. How do I do this best, the easiest way?

Thanks,
Ron
Re: test if $_POST variable is set [message #175832 is a reply to message #175831] Sat, 29 October 2011 08:38 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
cerr wrote:
> Hi There,
>
> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
> $result = mysql_query("SELECT * FROM specialty");
> while($row = mysql_fetch_array($result)) {
> if (isset($_POST[$row['name']])) {
> var_dump($POST);
> session_unset();
> }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?
>
> Thanks,
> Ron
To extract column names from the database is non trivial.


I woudl suggets another approach.

Like set up an array of mysql field names and use that to both do the
select of the data, and to scan through to see if it exists
Re: test if $_POST variable is set [message #175833 is a reply to message #175831] Sat, 29 October 2011 13:04 Go to previous messageGo to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
On 10/29/11 1:40 AM, cerr wrote:
> Hi There,
>
> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
> $result = mysql_query("SELECT * FROM specialty");
> while($row = mysql_fetch_array($result)) {
> if (isset($_POST[$row['name']])) {
> var_dump($POST);
> session_unset();
> }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?
>
> Thanks,
> Ron

have you looked at using fetch_assoc / foreach?

is in:

while($row - mysql_fetch_assoc($result){
foreach($row as $key => $value){
//..
}
}
Re: test if $_POST variable is set [message #175834 is a reply to message #175831] Sat, 29 October 2011 13:09 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 28 Oct 2011 22:40:31 -0700, cerr wrote:

> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
> $result = mysql_query("SELECT * FROM specialty"); while($row =
> mysql_fetch_array($result)) {
> if (isset($_POST[$row['name']])) {
> var_dump($POST);
> session_unset();
> }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?

A checkbox name element is only defined (set) in the $_POST array if it
is checked when the form is submitted. The value of the element will be
"on" unless a value is defined for the form element, in which case that
value will be used.

So if you want an unchecked box to have a value in $_POST, you have to
give it one yourself:

<?php
print_r($_POST);
// set values in $_POST for checkboxes
// if checked, set $_POST[checkbox] = true
// if not checked, set $_POST[checkbox] = false
$result = mysql_query("SELECT * FROM specialty");

while($row = mysql_fetch_array($result)) if (!isset($_POST[$row
['name']])) $_POST[$row['name']] = 0;

print_r($_POST);
?>

Rgds

Denis McMahon
Re: test if $_POST variable is set [message #175835 is a reply to message #175833] Sat, 29 October 2011 13:16 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sat, 29 Oct 2011 09:04:27 -0400, Richard Damon wrote:

> have you looked at using fetch_assoc / foreach?

Doh, missed that, yeah, he's not even fetching the array with column
names.

I figured his problem was that he was expecting and wasn't finding
unchecked checkboxes in his $_POST array.

Rgds

Denis McMahon
Re: test if $_POST variable is set [message #175836 is a reply to message #175831] Sat, 29 October 2011 13:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 10/29/2011 1:40 AM, cerr wrote:
> Hi There,
>
> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
> $result = mysql_query("SELECT * FROM specialty");
> while($row = mysql_fetch_array($result)) {
> if (isset($_POST[$row['name']])) {
> var_dump($POST);
> session_unset();
> }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?
>
> Thanks,
> Ron

What do you mean it "doesn't seem to work the way I expected it to"?
What do you expect to happen?

A couple of things:

First, you're not checking to see if your query worked. Always check
the return value from mysql_query() (and other external calls). There
could be any number of reasons why a query will fail; you're assuming it
worked but it might be failing.

Second, don't use "SELECT * .." when fetching from a database. Specify
the columns you wish (and only the columns you wish). "SELECT * ..."
will fetch unnecessary columns, especially if there is a change to the
database later (i.e. what happens if you add a 2M/row BLOB column to the
database later?).

Or what could be even worse - you're looking for a column in the
database called 'name'. What happens if later someone changed the
column to 'option'? Your 'SELECT *' will still work, but you'll no
longer fetch a value $row['name']. Depending on your code and error
message settings, this can be a very difficult problem to find. If you
specified 'SELECT name...' instead, the problem will be obvious.

As for the rest of your code - for every checkbox found checked you will
print the entire $_POST array, then unset all values in your $_SESSION
variable. Is this what you want?

Other than those comments, without knowing exactly what you expect, it's
hard to say what's not meeting your expectations.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: test if $_POST variable is set [message #175837 is a reply to message #175831] Sat, 29 October 2011 18:39 Go to previous message
cerr is currently offline  cerr
Messages: 33
Registered: September 2010
Karma: 0
Member
On Oct 28, 10:40 pm, cerr <ron.egg...@gmail.com> wrote:
> Hi There,
>
> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
>           $result = mysql_query("SELECT * FROM specialty");
>           while($row = mysql_fetch_array($result)) {
>             if (isset($_POST[$row['name']])) {
>               var_dump($POST);
>               session_unset();
>             }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?
>

Thanks for all the replies you guys, I actually got it resolved that I
posted the values with an integer index and to receive i would just
loop with a for loop through all the results and then i can fetch data
from the db that are indexed the with the same identifier.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: `********AMAZING HOT PHOTOS & VIDEOS ********
Next Topic: Suche Buch zur OOA/OOD
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 13:22:03 GMT 2024

Total time taken to generate the page: 0.02320 seconds