LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2004, 11:27 AM   #1
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
PHP : $_POST['choice$i'] doesnt work


Hey,

Ok I've got an html page that has about 100 variables to be passed to a php script which emails all of these variables to me. All of them are named choice1,choice2,choice3,etc..

Instead of writing like 100 lines or more of code, I thought I'd use a for loop, only I'm not sure how to do this... I have tried :

$post_array[$i] = $_POST['choice' . $i];
and
$post_array[$i] = $_POST['choice$i'];
and
$post_array[$i] = $_POST['choice'$i];

I'm really lost. Any ideas?

Thanks,

WB
 
Old 12-17-2004, 11:35 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
I would name the field like this :
<input type="text" name="choice[0]" value="" />
<input type="checkbox" name="choice[1]" checked="true" />
<input type="radio" name="choice[2]" />
...

Then :
PHP Code:
if(isset($_POST["choice"])) {
    
$post_array $_POST["choice"];
}
// checkin'
print_r($post_array); 

Last edited by Cedrik; 12-17-2004 at 11:39 AM.
 
Old 12-17-2004, 12:00 PM   #3
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Umm...sorry. I dont follow, I'm trying to ..but I dont quite understand how I could use that with like a for loop, if say I had 100 variables being passed. The answers probably right there...but I'm not that great at PHP, only been at it a few months.
 
Old 12-17-2004, 12:09 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
If you name the fields like choice[0], choice[1]... no need to loop to assign the array $_POST["choice"] to the $post_array variable

It is equivalent to do :
PHP Code:
foreach($_POST["choice"] as $choice) {
    
$post_array[] = $choice;

and :
PHP Code:
    $post_array $_POST["choice"]; 
 
Old 12-17-2004, 12:42 PM   #5
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Holy crap, thats so cool! Thanks 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 0,000,000
Cedrik. I understand now, that really helps alot. Thanks!
 
Old 12-17-2004, 08:42 PM   #6
cormander
Member
 
Registered: Dec 2004
Location: Hawaii
Distribution: Fedora & CentOS
Posts: 72

Rep: Reputation: 15
Re: PHP : $_POST['choice$i'] doesnt work

Quote:
Originally posted by WindowsBurner

$post_array[$i] = $_POST['choice' . $i];
and
$post_array[$i] = $_POST['choice$i'];
and
$post_array[$i] = $_POST['choice'$i];
As you already know, those won't work, but this will:

Code:
$key = "choice" . $i;

$post_array[$i] = $_POST[$key];
Hope that helps.

-Corey
 
Old 12-17-2004, 09:14 PM   #7
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
Why not change the variable names in the HTML to something useful and then do away with all that nonsense by just emailing yourself with something like this:

Code:
$recipient = "you@your.com";
$headers = "From: WebServer on " . $_SERVER['HTTP_HOST'] . " <you@your.com>\n";
$subject = "A really cool subject\n";

$redirectHeader = "Location: http://" . $_SERVER['HTTP_HOST'] . "/thankYou.php";

$body = "The form was filled out with the following:\n\n";

while(list($key, $val) = each($_POST)) {
        $body .= $key . ":     " . $val . "\n";
}

mail($recipient, $subject, $body, $headers);
header($redirectHeader);
You already have $post_array in $_POST. Reassigning all the values into another array is just a waste of CPU cycles and memory.

Last edited by sigsegv; 12-17-2004 at 09:19 PM.
 
Old 12-18-2004, 08:06 AM   #8
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Thanks corcommander, I knew I was close...but I hadn't thought of that.


sigsegv, your right I could do it that way. I hadn't thought of that either, what I have is a form for a survey and
there is literally 100 or more options. And I wanted a way for it to send me the info using as few lines of code as possible.

So, thanks a lot guys. I hadn't expected my post to get all these wonderful posts, I figured I'd quite one or two quick ways around the problem. Thanks alot.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP if(isset($_POST[\'submit\'])) wont evaluate true ALInux Programming 1 11-07-2005 04:22 AM
Why doesnt my USB mouse doesnt work? barkha Linux - Hardware 2 08-16-2005 11:31 AM
Large file upload - $_POST empty in PHP PsychosisNode Linux - Software 1 01-23-2005 07:32 AM
Best modem choice to work with Mandrake 9.2 duffboygrim Linux - Hardware 2 12-07-2003 03:22 AM
need some advice on language choice(Perl vs PHP) coolman0stress Programming 8 11-17-2003 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration