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

multi-select box validation

P: n/a
Hi folks. I'm porting a cf site to php, everything's going very well, I
like php much better (this, of course, being the correct forum to make
that statement :).

One problem I have is with validating a form that has a couple multi-
select list boxes. The idea is that when the user submits the form, it
goes to it's process script. The process script validates the responses,
and if all is ok, finishes processing and "includes" a thank you page.
If there's an error, it "includes" the original form, but with some error
vbls defined now. Those variable should mark the field's label in red
(it does) and feed values back into all the fields. Text fields and
single-select boxes work fine. Multi-select boxes only have the last-
selected item selected. It sounds like a vbl that should be an array
somewhere isn't, but I can't figure out where.

Something like this:

form.php
if (empty($error)) $error = "";

if (empty($contactname)) $contactname = "";
if (empty($targetpartlist)) $targetpartlist = "";
include("parttypes.inc");
?>

<form action="validate.php" method="POST">
<?php
if ($error != "")
echo "<font class='errortext'><center><b>Please correct the items in red
and resubmit.</b></center></font><br>";
?>
<?php if (strpos($error,"contactname") > 0) echo "<font
class='errortext'>"; else echo "<font class='normtext'>";
?>
<b>Contact Name:</b></font>
<?php echo "<input type=text name='contactname' value=$contactname>"; ?>
<?php if (strpos($error,"targetpart")) echo "<font class='errortext'>";
else echo "<font class='normtext'>";
?>
<b>What part types do you sell? (use control-click to select multiple
types)</b></font>
<select name = "targetpart[]" size = 5 multiple>
<?php
foreach ($PartTypeList As $PartTypeItem) {
echo "<OPTION ";
if (strpos($targetpartlist,$PartTypeItem)) echo "SELECTED ";
echo "VALUE='$PartTypeItem'> $PartTypeItem</OPTION>";
}
?>
</select>
....

Okay, so the first time through, $error, $contactname, $targetpartlist
are all undefined, so the form shows up normal. PartTypeList is defined
in parttypes.inc. Once the form is submitted, it goes to validate.php:

<?php
// set defaults
$error = "";

// import form variables and initialize locals
if (isset($_POST['contactname'])) $contactname = $_POST['contactname'];
else $contactname = "";
if (isset($_POST['targetpart'])) $targetpart = $_POST['targetpart'];

if (isset($targetpart)) $targetpartlist = implode(",",$targetpart); else
$targetpartlist = "";

// validate
if ($contactname == "") $error=$error.", contactname";
if ($targetpartlist == "") $error=$error.", targetpart";

if ($error == "")
{
// do all the processing here

include("thanks.php");
}
else {
include("form.php");
}
?>

All the error checks show up as expected. If I echo $targetpartlist, I
get the expected comma-separated list with all the items in it. The
problem is that only that last selected item is actually gets the
"SELECTED" tag in my foreach loop. Oh, did I mention I've only been
writing PHP for three days? If anyone sees anything (or has other
recommendations for improvement, I'd appreciate it).

Thanks,
Greg
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Greg Bryant <br**********@yahoo.com> wrote in
news:Xn*********************************@199.45.49 .11:
Hi folks. I'm porting a cf site to php, everything's going very well,
I like php much better (this, of course, being the correct forum to
make that statement :).

One problem I have is with validating a form that has a couple multi-
select list boxes. The idea is that when the user submits the form,
it goes to it's process script. The process script validates the
responses, and if all is ok, finishes processing and "includes" a
thank you page. If there's an error, it "includes" the original form,
but with some error vbls defined now. Those variable should mark the
field's label in red (it does) and feed values back into all the
fields. Text fields and single-select boxes work fine. Multi-select
boxes only have the last- selected item selected. It sounds like a
vbl that should be an array somewhere isn't, but I can't figure out
where.

Something like this:

[snip]

All the error checks show up as expected. If I echo $targetpartlist,
I get the expected comma-separated list with all the items in it. The
problem is that only that last selected item is actually gets the
"SELECTED" tag in my foreach loop. Oh, did I mention I've only been
writing PHP for three days? If anyone sees anything (or has other
recommendations for improvement, I'd appreciate it).

Thanks,
Greg


Ah, that was a mildly subtle one. I managed to miss the distinction
between a non-present string and a zero-based string. This version of
the loop works (but there's got to be a more efficient way to do this):

foreach ($PartTypeList As $PartTypeItem) {
echo "<OPTION ";
$fnd = strpos($targetpartlist,$PartTypeItem);
if (!($fnd === FALSE)) echo "SELECTED ";
echo "VALUE='$PartTypeItem'> $PartTypeItem</OPTION>";
}
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.