/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] validating input array element

I’m validating a form that has a couple input elements where the name is an array because you can enter multiple colors. When I’m checking for blanks, this works to pass the value back to the form when errors are displayed my form for non-array fields.

[code=html]<input type=”text” name=”vendor” id=”textfield” value=”<?=@$_POST[‘vendor’]?>” />[/code]

this does not work

[code=html]<input type=”text” name=”color[]” value=”<?=@$_POST[‘color[0]’]?>”/>[/code]

and this gives me the value of Array

[code=html]<input type=”text” name=”color[]” cols=”33″ rows=”10″ value=”<?=@$_POST[‘color’]?>” />[/code]

i tried using foreach inside the <? ?> on value but i get an error of improper use of foreach

here is my validation code in case that helps. any help is very much appreciated.

[CODE]function check_for_blanks() {
global $errors;
if(!$_POST[‘vendor’] ||$_POST[‘vendor’] == “” || strlen($_POST[‘vendor’] > 40)) {
$errors[] = “<font color=’red’>Enter a vendor</font>”;
}
$arr_color = $_POST[‘color’];
foreach($arr_color as $color) {
if(!$color ||$color == “” || strlen($color > 40)) {
$errors[] = “<font color=’red’>Enter a color</font>”;
}
}
$arr_desc = $_POST[‘desc’];
foreach($arr_desc as $desc) {
if(!$desc ||$desc == “” || strlen($desc > 40)) {
$errors[] = “<font color=’red’>Enter a description</font>”;
}
}
if(!isset($_POST[‘drop’])) {
$errors[] = “<font color=’red’>Did you check if this vendor drop ships?</font>”;
}
}//closes function check_for_blanks()
[/CODE]

and here is my form

[code=html]<div id=”form”>
<form action=”<?php $self ?>” method=”post” enctype=”multipart/form-data” name=”VendorForm”>
<div id=”itemInput”>Enter a Vendor Name: <input type=”text” name=”vendor” id=”textfield” value=”<?=@$_POST[‘vendor’]?>” /><br /><br />
Does this Vendor Offer Drop Shipment? <input type=”radio” name=”drop” value=”Yes” />Yes <input type=”radio” name=”drop” value=”No” />No<br /><br /></div>
<div id=”dynamicAdd”>What colors does this vendor carry? (1)<input type=”text” name=”color[]” value=”<?=@$_POST[‘color’] ?>” />Add Color Swatch: <input type=”file” name=”colorFile[]” /></div>
<div id=”button”>
<input name=”addColor” type=”button” value=”Add Color Field” onclick=”addInputField(‘color[]’, ‘dynamic’, ‘color’, ‘text’)” />
<input name=”removeColor” type=”button” value=”Remove Color Field” onclick=”removeInputFields(‘dynamic’)” /><br /></div>
<div id=”dynamic”></div><div id=”dynamicUpload”></div>
Enter a description: <input type=”text” name=”desc[]” cols=”33″ rows=”10″ value=”<?=@$_POST[‘desc’]?>” /><input name=”add_desc” type=”button” value=”Add Description Field” onclick=”addSingle(‘desc[]’, ‘dynamic2’, ‘description’, ‘text’)” /><input name=”submit” type=”button” value=”Remove Description Field” onclick=”removeInputFields(‘dynamic2′)” /><br /><br />
<div id=”dynamic2″></div>
<input type=’hidden’ name=’submit’ ” , “value=1>

<input type=”submit” name=”button” id=”button” value=”Post Vendor” />
</form></div>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 21.2009 — $_POST['color'][0]
Copy linkTweet thisAlerts:
@kaiser0427authorApr 21.2009 — thanks NogDog!
×

Success!

Help @kaiser0427 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...