Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
GET, POST, Multi-Selects and the PHP nightmare
Old 06-09-2005, 09:49 AM GET, POST, Multi-Selects and the PHP nightmare
Junior Talker

Posts: 1
Trades: 0
When the HTML of a form has a field name 'choices' and has five checkboxes with that name, each with a different value, like this:
Code:
<form name="testform" action="processor.php" method="post">
<input type="checkbox" name="choices" value="One" checked="checked">One
<input type="checkbox" name="choices" value="Two" checked="checked">Two
<input type="checkbox" name="choices" value="Three" checked="checked">Three
<input type="checkbox" name="choices" value="Four" checked="checked">Four
<input type="checkbox" name="choices" value="Five" checked="checked">Five
<input type="submit" value="submit" name="submit">
</form>
In PHP, without altering the HTML at all (assume that I have no control over the HTML because there will be remote forms already coded by others that submit to this script) if I look at the values through the PHP-approved method (assuming that every checkbox was checked), I see:
PHP Code:
<?php
$choicevalues
=$_POST['choices'];
print(
$choicevalues); //prints 'One'
?>
Where did the rest of my selections disappear to? How can I get at them? This is the question that is plaguing me.

As a minor additional frustration, what if I don't give a durn what action the form used?
Is there no axiomatic way to access the form variable regardless of method (without turning on the globals option)?
theauthor is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-09-2005, 11:21 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Ok, dunno where u got this "PHP-approved method" thing from, it's complete rubbish.

PHP parses checkboxes through as a one dimensional array. Which mean u MUST alter the HTML, but only slightly. Rather than having the checkboxes called "choices" it must now be "choices[]", this ensures it's parsed as an array.

In the php itself, u can post the array like this:

PHP Code:
foreach($chocies as $value) {
    print 
$value;
                print 
"<BR>";

As for your question on "if u don't give a darn about what method is used to submit", if ur server has autoglobals on, then it does not matter, u can simply use the name of the HTML form element as your variable in PHP.

If globals is off however, it's a different story. You're going to have to do something like this to get the value of "choices"

PHP Code:
$Request_Method $_SERVER['REQUEST_METHOD'];

$choices = ${_.$Request_Method}['chocies'
or this may work too:

PHP Code:
$choices = ${_.$_SERVER['REQUEST_METHOD']}['a'
but i dont think it'll work
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 06-09-2005, 11:36 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Well if you cannot access that html page then your going to be scratching your head.

The page you have will collect all the values for choices but sadly (in your case) you are overwritting choices -1 with choices each time.

To get past this you will need to either name your checkboxes individualy or make them into an html array,

Using your form and my code will only print F i v e while a html array would print
one
two
etc

If you cannot change the html you will not be able to grab each value for choices.

Code:
<form name="testform" method="post">
<input type="checkbox" name="choices[]" value="One" checked="checked">One
<input type="checkbox" name="choices[]" value="Two" checked="checked">Two
<input type="checkbox" name="choices[]" value="Three" checked="checked">Three
<input type="checkbox" name="choices[]" value="Four" checked="checked">Four
<input type="checkbox" name="choices[]" value="Five" checked="checked">Five
<input type="submit" value="submit" name="submit">
</form>
then

PHP Code:
    <?php

if(isset($_POST['choices'])){

    foreach (
$_POST as $field => $val) {
        if(
is_array($val)) {
            foreach(
$val as $key => $value) {
                
$this->postvars[$field][$key] = $value;
                
            }
        }else {
            
$this->postvars[$field] = trim($val);
        }
    }
    
    if(!empty(
$this->postvars['choices'][0])){
         echo 
$this->postvars['choices'][0] . "<br />";
    }
    if(!empty(
$this->postvars['choices'][1])){
         echo 
$this->postvars['choices'][1] . "<br />";
    }
    if(!empty(
$this->postvars['choices'][2])){
         echo 
$this->postvars['choices'][2] . "<br />";
    }
    if(!empty(
$this->postvars['choices'][3])){
         echo 
$this->postvars['choices'][3] . "<br />";
    }
    if(!empty(
$this->postvars['choices'][4])){
         echo 
$this->postvars['choices'][4] . "<br />";
    }
    if(!empty(
$this->postvars['choices'][5])){
         echo 
$this->postvars['choices'][5] . "<br />";
    }
    
    
// or display like, but greater access above
    
    /*foreach($this->postvars['choices'] as $val){
        echo $val ."<br />";
    }*/
}
?>

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

Last edited by ibbo; 06-09-2005 at 11:43 AM..
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 06-09-2005, 11:42 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Quote:
Originally Posted by leavethisplace
Ok, dunno where u got this "PHP-approved method" thing from, it's complete rubbish.

PHP parses checkboxes through as a one dimensional array. Which mean u MUST alter the HTML, but only slightly. Rather than having the checkboxes called "choices" it must now be "choices[]", this ensures it's parsed as an array.

In the php itself, u can post the array like this:

PHP Code:
foreach($chocies as $value) {
    print 
$value;
                print 
"<BR>";

As for your question on "if u don't give a darn about what method is used to submit", if ur server has autoglobals on, then it does not matter, u can simply use the name of the HTML form element as your variable in PHP.

If globals is off however, it's a different story. You're going to have to do something like this to get the value of "choices"

PHP Code:
$Request_Method $_SERVER['REQUEST_METHOD'];

$choices = ${_.$Request_Method}['chocies'
or this may work too:

PHP Code:
$choices = ${_.$_SERVER['REQUEST_METHOD']}['a'
but i dont think it'll work

Good assumption mate, your method will print F i v e.

The choices is not defined as an array so each time you grab a new value it is overwritting the last one. However your foreach splits the string Five into elements and bangs them out 1 char a time.

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 06-09-2005, 01:13 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Works fine for me. Does not seem to do what you said at all... watever though
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 06-09-2005, 01:23 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
yep, even just check it using the below code (or check out www.festerband.com/test.php ):

PHP Code:
<?php

foreach($selitems as $val) { 

    print 
$val
    print 
"<BR>"



?>

<form name="test.php" action="test.php" method="post">
<input type="checkbox" name="selitems[]" value="One" checked="checked">One
<input type="checkbox" name="selitems[]" value="Two" checked="checked">Two
<input type="checkbox" name="selitems[]" value="Three" checked="checked">Three
<input type="checkbox" name="selitems[]" value="Four" checked="checked">Four
<input type="checkbox" name="selitems[]" value="Five" checked="checked">Five
<input type="submit" value="submit" name="submit">
</form>
U talking rubbish?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 06-13-2005, 04:35 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
<input type="checkbox" name="selitems[]" value="One" checked="checked">One

Works fine now you have html arrays in place!

Quote:
U talking rubbish?
I actually didn't see your choices[] definition hiden away within your paragraph, I therefore assumed you were using choices, this would have yeilded the result I said it would. So no rubbish is not on the menu only logic.

Your second example shows this clearly.

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

Last edited by ibbo; 06-13-2005 at 04:41 AM..
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Reply     « Reply to GET, POST, Multi-Selects and the PHP nightmare
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31297 seconds with 12 queries