FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Ton of trouble with Mutiple values in form





Phil
First of all, big thank you to those of you who helped with my question on variables.
Now, I've moved on and am working on a survey form. The problem is at one point in the survey, you choose more than one item. At that point the generated result just says "Array".
The page is at:
http://phil.frihost.net/php/working_php_copy.html

I've been trying several dufferent things all afternoon, so I'm sorry if the code is screwy.
hexkid
Add these to your handle_survey.php script

Code:
echo '<pre>';
print_r($_POST);
echo '</pre>';


If you still have questions afterwards, do post them.


Hmmm ... I'll give you a little more help Smile

$_POST['oguard'] is an array
$_POST['oguard'][2] is not an array; and will be set (to 'Jake Kupp') if it was selected by the user, unset if it wasn't selected.

Code:
if (isset($_POST['oguard'][2])) {
  ### user selected "Jake Kupp"
}
kv
you could also try var_dump($_POST) (or any other variable/array/mixed for that matter) to inspect the contents of the variable.
Phil
hexkid wrote:


If you still have questions afterwards, do post them.


Hmmm ... I'll give you a little more help Smile

$_POST['oguard'] is an array
$_POST['oguard'][2] is not an array; and will be set (to 'Jake Kupp') if it was selected by the user, unset if it wasn't selected.

Code:
if (isset($_POST['oguard'][2])) {
  ### user selected "Jake Kupp"
}


Ok, I see what's going on, but I have no idea why. The resulting display reminds me of what I would receive from frommail.php.

What I ultimately want is to display the results in an HTML page, that's updated dynamically.
But right know, I'm think I'm missing something. From what I'm reading, isset($var) has something to do with if the variable has any other value than NULL, but how does that apply to what I'm doing?

Quote:
you could also try var_dump($_POST) (or any other variable/array/mixed for that matter) to inspect the contents of the variable.


Please forgive my ignorance, but how do I use var_dump($_POST) in my code?
hexkid
Phil wrote:
Ok, I see what's going on, but I have no idea why. The resulting display reminds me of what I would receive from frommail.php.

What I ultimately want is to display the results in an HTML page, that's updated dynamically.
But right know, I'm think I'm missing something. From what I'm reading, isset($var) has something to do with if the variable has any other value than NULL, but how does that apply to what I'm doing?


What happens is that the server never even sees the unchecked check boxes.
Suppose you have "regular" names for the check boxes
Code:
<form action="foobar.php" method="post">
<label><input type="checkbox" name="one" value="1" checked>one</label>
<label><input type="checkbox" name="two" value="2">two</label>
<label><input type="checkbox" name="three" value="3" checked>three</label>
<input type="submit">
</form>
When you submit this form, with "two" unchecked, the server only gets $_POST['one'] and $_POST['three']. If you use unindexed arrays, the automatic work PHP does behind the scenes, is probably not what you expect.
Code:
<form action="foobar.php" method="post">
<label><input type="checkbox" name="foo[]" value="1" checked>one</label>
<label><input type="checkbox" name="foo[]" value="2">two</label>
<label><input type="checkbox" name="foo[]" value="3" checked>three</label>
<input type="submit">
</form>
Submitting this form as is, makes it reach the server before PHP takes control with two HTML controls with the same name (foo[] and foo[]) but different values ('1' and '3'). When PHP takes control, but before your script starts, they are changed to foo[0] and foo[1]. So you cannot check if "two" was checked with
Code:
if (isset($_POST['foo'][1])) { /* ... */ }


Here's a foobar.php that displays "You selected <FIELDS SELECTD BY THE USER>."
Code:
<?php
if (count($_POST['foo'])) {
  echo 'You selected ', implode(',', $_POST['foo']), '.';
} else {
  echo 'You didn\'t select any checkbox.';
}
?>
Phil
Quote:

<?php
if (count($_POST['foo'])) {
echo 'You selected ', implode(',', $_POST['foo']), '.';
} else {
echo 'You didn't select any checkbox.';
}
?>

When I put this is my code, I just get a parse error.



Quote:

echo '<pre>';
print_r($_POST);
echo '</pre>';


It displays the variables with this code, but how can I get it to display as HTML?

Just to let you know, I want to continue this discussion, but I'll be out of town for a job the next few days. No computers to get internet, so I'll just get back with you when I get back.

Thanks.
hexkid
Phil wrote:
Quote:

echo 'You didn't select any checkbox.';
?>

When I put this is my code, I just get a parse error.
Hmmm, sorry. I edited my post above to correct the bug. There was a missing backslash to "escape" the quote in "didn't".
Code:
### ERROR: "didn't" must be "didn\'t"
echo 'You didn\'t select any checkbox';


Phil wrote:
how can I get it to display as HTML?
Try it now. Instead of implode try a foreach construct
Code:
foreach ($_POST['foo'] as $val) {
  echo "<b>$val</b>";
}
manum
in case u need any more help just pm me and don't forget to include the php code that u have written
Phil
This is kind of a "BUMP". I finally got back and will be getting back into the PHP learning soon.
What is getting me is you guys are throwing these "commands" (for lack of a better term) like :

Quote:
foreach ($_POST['foo'] as $val) {
echo "<b>$val</b>";

}

and

i
Quote:
f (count($_POST['foo'])) {
echo 'You selected ', implode(',', $_POST['foo']), '.';
} else {
echo 'You didn\'t select any checkbox.';


and I really have no idea what they are for or what they do. I'm just going by the afore mentioned book, see? And the book doesn't deal with what you guys are telling me, so I'm having to back up and look at what the book says and what you're telling me.
It's just taking me a little more effort to catch on, that's all.

If someone could point me to a resource that explains---- in basic terms---- what you've told me, I sure would appreciate it.
hexkid
Phil wrote:
If someone could point me to a resource that explains---- in basic terms---- what you've told me, I sure would appreciate it.
I like php.net. You can look for almost anything just by building the URL with whatever you're interested in. Just type it in the address bar preceded by "php.net":
Phil
Quote:
I like php.net.


Thanks hexkid. I think that site will help.
There seems to be some great gaps in this book I have. It is about 3 years old and I just found out there is a second edition of the same title out anytime now.
Related topics
How To : Secure Your PHP Website
writing COM components in Perl
Looking for easy FormMail Script
The Perfect Audio Rips
Computer jokes (funny!)
"Why I published those cartoons" [mocking fascism]
A debate of religion, science, and more
how do I register my domain in your registration form
PHP script trouble
neep help making email form that accepts html tags
File upload problem :'(
Changing internal data with scripting.
transfering mysql values form one table to another with php
Storing A Lot of Values Using Sessions
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.