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($contact name)) $contactname = "";
if (empty($targetp artlist)) $targetpartlist = "";
include("partty pes.inc");
?>
<form action="validat e.php" method="POST">
<?php
if ($error != "")
echo "<font class='errortex t'><center><b>P lease correct the items in red
and resubmit.</b></center></font><br>";
?>
<?php if (strpos($error, "contactnam e") > 0) echo "<font
class='errortex t'>"; else echo "<font class='normtext '>";
?>
<b>Contact Name:</b></font>
<?php echo "<input type=text name='contactna me' value=$contactn ame>"; ?>
<?php if (strpos($error, "targetpart ")) echo "<font class='errortex t'>";
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($target partlist,$PartT ypeItem)) echo "SELECTED ";
echo "VALUE='$PartTy peItem'> $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($targetp art)) $targetpartlist = implode(",",$ta rgetpart); else
$targetpartlist = "";
// validate
if ($contactname == "") $error=$error." , contactname";
if ($targetpartlis t == "") $error=$error." , targetpart";
if ($error == "")
{
// do all the processing here
include("thanks .php");
}
else {
include("form.p hp");
}
?>
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
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($contact name)) $contactname = "";
if (empty($targetp artlist)) $targetpartlist = "";
include("partty pes.inc");
?>
<form action="validat e.php" method="POST">
<?php
if ($error != "")
echo "<font class='errortex t'><center><b>P lease correct the items in red
and resubmit.</b></center></font><br>";
?>
<?php if (strpos($error, "contactnam e") > 0) echo "<font
class='errortex t'>"; else echo "<font class='normtext '>";
?>
<b>Contact Name:</b></font>
<?php echo "<input type=text name='contactna me' value=$contactn ame>"; ?>
<?php if (strpos($error, "targetpart ")) echo "<font class='errortex t'>";
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($target partlist,$PartT ypeItem)) echo "SELECTED ";
echo "VALUE='$PartTy peItem'> $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($targetp art)) $targetpartlist = implode(",",$ta rgetpart); else
$targetpartlist = "";
// validate
if ($contactname == "") $error=$error." , contactname";
if ($targetpartlis t == "") $error=$error." , targetpart";
if ($error == "")
{
// do all the processing here
include("thanks .php");
}
else {
include("form.p hp");
}
?>
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
Comment