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 :: Unread Messages :: Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
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 01:40 Go to next message
cerr is currently offline  cerr
Messages: 33
Registered: September 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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 04: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
add to buddy list
ignore all messages by this user
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 09:04 Go to previous messageGo to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
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 09:09 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
remove from buddy list
ignore all messages by this user
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 09:16 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
remove from buddy list
ignore all messages by this user
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
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Re: test if $_POST variable is set [message #175837 is a reply to message #175831] Sat, 29 October 2011 14:39 Go to previous message
cerr is currently offline  cerr
Messages: 33
Registered: September 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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.
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
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: Wed Oct 09 05:02:35 EDT 2024

Total time taken to generate the page: 0.04866 seconds