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
How to post an array of checkbox values in PHP?
Old 05-24-2012, 06:08 PM How to post an array of checkbox values in PHP?
Ultra Talker

Posts: 267
Name: Patric
Trades: 0
Hello, so I am getting closer to finally completing my form.

However, I need it so that the US states in my form are able to retrieve the VALUE section of it.

http://bayardclient.com/fastaff/shor...layout_new.php

For example, in my html code, I have each set up as follows:

<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="[states]" id="Arkansas" value="AR"/> Arkansas
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="[states]" id="California" value="CA"/> California

and in the PHP, I have it set up like:
<?php session_start(); {
if(isset($_POST['Submit']))
$states = $_POST['states'];


but it doesn't seem to be working. That is how i have set up the other fields on my form, with the

$name = $_POST['name'];

Can some one please possibly steer me in the right direction?? Would be very appreciative.
Reckoner is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-24-2012, 06:20 PM Re: How to post an array of checkbox values in PHP?
chrishirst's Avatar
Defies a Status

Posts: 43,963
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Give them all the same name, without the [] and the checked values will be submitted as a comma seperated string.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-24-2012, 06:21 PM Re: How to post an array of checkbox values in PHP?
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Edit: Chris got there before me :P

Edit2: Although, I believe you can also end the name with empty brackets to have php automatically create an array with the selected boxes' values? As in:
HTML Code:
<input type="checkbox" name="states[]" ... />
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 05-24-2012 at 06:25 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-24-2012, 07:27 PM Re: How to post an array of checkbox values in PHP?
Ultra Talker

Posts: 267
Name: Patric
Trades: 0
Hey Guys, thanks for the reply, but that still doesn't seem to be working, using Chris' suggestion only takes 1 state, and it's the last one the user clicked on. Also tried lizciz' suggestion, but no luck there either.

This is how they are set up now in the HTML

<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states" id="Alabama" value="AR"/> Alabama
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states" id="Alaska" value="AK"/> Alaska
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states" id="Arizona" value="AZ"/> Arizona
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states" id="Arkansas" value="AR"/> Arkansas
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states" id="California" value="CA"/> California

In PHP:

$states = $_POST['states'];



States they have an active license:

'.$states.'
Reckoner is offline
Reply With Quote
View Public Profile
 
Old 05-24-2012, 11:19 PM Re: How to post an array of checkbox values in PHP?
Ultra Talker

Posts: 267
Name: Patric
Trades: 0
ok, update... so when I use lizciz's method

<input type="checkbox" name="states[]" ... />

<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="states[]" id="NewYork" value="NY"/> New York

when I push submit on the form, in the email that comes through, the output is

"Array" for the states. Really not sure what I am doing wrong, I feel like I am so close...

IN the mailer_new.php file, the way that it's set up is like this.

$states = $_POST['states']; .... I think I might need something else so that it shows the array values??

Sorry, been doing a ton of googling tonight for this but it doesn't seem like it's as easy as I would have thought. Any ideas?

Thanks a lot for the help earlier, much appreciated.
Reckoner is offline
Reply With Quote
View Public Profile
 
Old 05-25-2012, 01:08 AM Re: How to post an array of checkbox values in PHP?
Ultra Talker

Posts: 267
Name: Patric
Trades: 0
FIGURED THIS OUT.

needed to put $states .= implode(', ', $_POST['states']); instead of that code I mentioned in the post before.

phew.

Thanks for the help guys, definitely got the ball rolling.


Not gunna lie, a lot of this stuff doesn't make a whole lof of sense to me. How do you know of all these crazy parts of php? I would have never thought to put that on my own... I guess what I'm asking, is what is the best way to learn that aspect of php? Really need to know how it all works.
Reckoner is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to post an array of checkbox values in PHP?
 

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.23183 seconds with 11 queries