test if $_POST variable is set [message #175831] |
Sat, 29 October 2011 01:40 |
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 |
|
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 |
|
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 |
|
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 |
|
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 #175837 is a reply to message #175831] |
Sat, 29 October 2011 14:39 |
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.
|
|
|