By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Array Issue - Correspoing checkbox and textfield values to behave

P: n/a
Hello All,

I'm trying to figure out this array problem I have. I don't know if
I'm doing this correctly. Anyone want to take a look.

---------------------
Current Situation.
---------------------
I have a form with checkboxes and corresponding textfields for each
checkbox. Here is the HTML

<input type="checkbox" name="shows[]" value="Morning">Morning</
option># of People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Lunch">Lunch</option># of
People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Evening">Evening</
option># of People<input type="text" name="people[]" value="" />

---------------------
Current Problem
---------------------
If I was to fill out the form and check off the "Evening" checkbox
with 15 people in the textbox and looked at the arrays I would have
the following:

shows Array {
[0] =Evening
}

people Array {
[0] =>
[1] =>
[2] =15
}

I guess I was expecting this

shows Array {
[0] =Evening
}

people Array {
[0] =15
}
---------------------
Attempted Solution
---------------------

// Merge Shows Options
function show_Pairing($s, $n) {
return("$s <brest. $n People,<br><br>");
}

function map_Pairs($s, $n) {
return(array($s =$n));
}

$merged_list = array_map("show_Pairing", $_POST["shows"],
$_POST["people"]);

foreach ($merged_list as $key =$value) {
$shows_list .= ucfirst ($key) ." : ". $value . "<br>";
}
---------------------
Result
---------------------

0. Evening est. people
1. est. people
2. est. 15 people
So what's happening is the checkboxes seem to have their own key
values and the text boxes have their own key values but when I pair
them they don't seem to want to match.
In this example "Evening" should have a key of shows[2] because that's
how it appeared in the list HTML form but that's not happening.
Is there a PHP solution that can deal with this? Do I need to
reconsider how I laid out my HTML?
Looking for a PHP 4.4 solution considering ISP doesn't have PHP 5.

Jul 18 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Rik
On Wed, 18 Jul 2007 03:00:27 +0200, newbie <al*****@gmail.comwrote:
Hello All,

I'm trying to figure out this array problem I have. I don't know if
I'm doing this correctly. Anyone want to take a look.

---------------------
Current Situation.
---------------------
I have a form with checkboxes and corresponding textfields for each
checkbox. Here is the HTML

<input type="checkbox" name="shows[]" value="Morning">Morning</
option># of People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Lunch">Lunch</option># of
People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Evening">Evening</
option># of People<input type="text" name="people[]" value="" />
So, in short, the problem is unchecked checkboxes don't get send, which is
default behaviour);

Solution:

Checkbox name: "shows[]"
Text names: "people[<show_value>]" (so: people[Morning], people[Lunch]
etc.)

(Oh yeah: and get rid of your unopened </options>'s... validate your HTML
:-) )

Receiving end:
foreach($_POST['shows'] as $show){
$num = isset($_POST['people'][$show]) ? intval($_POST['people'][$show])
: 0;
echo "$num people for $show show";
}

However, is there a special need to have both a checkbox AND a textfield?
Surely this is double work for your users, and they can just leave a text
field blank if they don't choose that option?
--
Rik Wasmus
Jul 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.