/    Sign up×
Community /Pin to ProfileBookmark

Collecting Multiple Form values

Im a bit new to php syntax, and im not always sure how to make it interface with the html. I need to collect the values of a set of checkboxes into an array, in order that im able to make the values ‘sticky’ once selected.

i saw this method while googleing, but this did not help,

print(“<tr><td><input type=”checkbox” name=”plaza[]””);

any insight?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@GarySJun 08.2006 — I'll let you form this into a print statement.... the end result needs to look like this:

<input type="checkbox" name="plaza[box1]" />

<input type="checkbox" name="plaza[box2]" />

<input type="checkbox" name="plaza[box3]" />

That should return gather you checkboxes together in a nice array.


*****

The next job will be to add the conditional stuff for "checked"...
Copy linkTweet thisAlerts:
@matt1776authorJun 08.2006 — ive just tried that and it wont work, i need to generate these checkboxes dynamically, i tried implementing a counter to account for these name="plaza[box1]"

this might help, here is more of my code:


function plazaCreator($plazaArray, $plaza) {

// SOME CONDITONAL STATEMENT HERE TO CHECK FOR INCOMING SELECTED VALUES VIA $plazaArray

// NEED TO MAKE FUNCTION SO THAT MULTIPLE VALUES REMAIN "STICKY"

$db_plaza = array();

$sql = "SELECT plaza FROM plaza";

$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))

{

array_push($db_plaza, $row[plaza]);

}

print("<script type="text/javascript" language="JavaScript" src="includes/checkAll.js" />n");

print("<table class="content" border="1" cellspacing="0" cellpadding="3">n");

print("<tr><td colspan="2" align="center">Plaza</td></tr>n");

print("<tr><td><input type="checkbox" name="allbox" value="on" onclick="checkAll();"></td><td>ALL</td></tr>n");

//print("<tr><td><input type="checkbox" name="plaza" value="on" onclick="checkAlll(document.form.plaza);"></td><td>ALL</td></tr>n");


foreach($db_plaza as $ind) {

print("<tr><td><input type="checkbox" name="plaza[box$ctr]"");

foreach($plaza as $knd) {

if ($ind == $knd) {

print(" checked="checked"");

}

}

print(" value="$ind"></td><td>$ind</td></tr>n");

$ctr++;

}

print("</table>");

}
[/QUOTE]


and here is where i call the function:

<?php $plaza = array(); plazaCreator($plazaArray, $plaza); ?>
Copy linkTweet thisAlerts:
@GarySJun 08.2006 — I made a mistake in my previous post - this is what you're aiming for:


<input type="checkbox" name="plaza[]" value="box1" />

<input type="checkbox" name="plaza[]" value="box2" />

<input type="checkbox" name="plaza[]" value="box3" />

I took your code, faked some (partial) database results, removed the checked text (because I'm lazy)... and wrapped the whole thing in a form:

[code=php]
print_r($_POST);


$db_plaza = array();

//Fake ome db values
$db_plaza[]=array('plaza'=>'one');
$db_plaza[]=array('plaza'=>'two');
$db_plaza[]=array('plaza'=>'three');


echo '<form method="post">';

print("<script type="text/javascript" language="JavaScript" src="includes/checkAll.js" />n");
print("<table class="content" border="1" cellspacing="0" cellpadding="3">n");
print("<tr><td colspan="2" align="center">Plaza</td></tr>n");
print("<tr><td><input type="checkbox" name="allbox" value="on" onclick="checkAll();"></td><td>ALL</td></tr>n");
//print("<tr><td><input type="checkbox" name="plaza" value="on" onclick="checkAlll(document.form.plaza);"></td><td>ALL</td></tr>n");


foreach($db_plaza as $name=>$ind) {
print("<tr><td><input type="checkbox" name="plaza[]" value="$ind[plaza]" ");
//foreach($plaza as $knd) {
//if ($ind == $knd) {
//print(" checked="checked"");
//}
//}
print(" value="$ind"></td><td>$ind[plaza]</td></tr>n");
//$ctr++;
}

print("</table>");

echo '<input type="submit" />';
echo '</form>';
[/code]


This should (a) work! and (b) let you see if the array it's producing is what you're looking for.
Copy linkTweet thisAlerts:
@matt1776authorJun 08.2006 — yes this works great thank you ?

however i tried to do this with more than one set of checkboxes on the same page. the $_POST seems to only be able to hold one array at a time, so when it holds one array, the other set of checkboxes throws an exception, claiming that its expecting an array when its not getting any values, and visa versa when the other set has its way with the $_POST

is there any way to store multiple arrays?
Copy linkTweet thisAlerts:
@GarySJun 08.2006 — There shouldn't be a conflict - I've just used a similar approach on this monster of a form: ##LINK REMOVED## - the only thing you should have to test for is the [b]lack[/b] of values.

Easiest to see it in action:

[code=php]
echo '<pre>';
print_r($_POST);
echo '</pre>';

$db_plaza = array();

//Fake ome db values
$db_plaza[]=array('plaza'=>'one');
$db_plaza[]=array('plaza'=>'two');
$db_plaza[]=array('plaza'=>'three');


echo '<form method="post">';

print("<script type="text/javascript" language="JavaScript" src="includes/checkAll.js" />n");
print("<table class="content" border="1" cellspacing="0" cellpadding="3">n");
print("<tr><td colspan="2" align="center">Plaza</td></tr>n");
print("<tr><td><input type="checkbox" name="allbox" value="on" onclick="checkAll();"></td><td>ALL</td></tr>n");
//print("<tr><td><input type="checkbox" name="plaza" value="on" onclick="checkAlll(document.form.plaza);"></td><td>ALL</td></tr>n");


foreach($db_plaza as $name=>$ind) {
print("<tr><td><input type="checkbox" name="plaza[]" value="$ind[plaza]" ");
print(" value="$ind"></td><td>$ind[plaza]</td></tr>n");

}

foreach($db_plaza as $name=>$ind) {
print("<tr><td><input type="checkbox" name="another_plaza[]" value="$ind[plaza]" ");
print(" value="$ind"></td><td>another $ind[plaza]</td></tr>n");
}


print("</table>");

echo '<input type="submit" />';
echo '</form>';
[/code]


Tick nothing and you get (for $_POST)

[code=html]Array
(
)
[/code]

Tick one of the sets and you get (for example)
[code=html]
Array
(
[plaza] => Array
(
[0] => one
[1] => three
)

)
[/code]


Tick at leat one from both sets and you get:
[code=html]Array
(
[plaza] => Array
(
[0] => one
)

[another_plaza] => Array
(
[0] => one
[1] => two
[2] => three
)

)[/code]
Copy linkTweet thisAlerts:
@matt1776authorJun 08.2006 — got it! ? thanks you just helped me major with my php knowledge, i appreciate it
Copy linkTweet thisAlerts:
@GarySJun 08.2006 — Glad to help.
×

Success!

Help @matt1776 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...