/    Sign up×
Community /Pin to ProfileBookmark

For loop for checking a form

Hello all.

I am working with a form where there is a text field, two checkboxes, and another text field. Both text fields need to be required, and either of the check boxes have to be checked.

Below is an example of the form that I have created.

[code]
<form name=’merch_frm’ action=’merch.php’ method=’post’>
<input name=”txtOne” type=”text” id=”txtOne”>
<br/>
Check One:
<input name=”chkOne” type=”checkbox” id=”chkOne” value=”checkbox”>
<br/>
Check Two:
<input name=”chkTwo” type=”checkbox” id=”chkTwo” value=”checkbox”>
<br/>
Input:
<input name=”txtTwo” type=”text” id=”txtTwo”><br/>

<input type=”submit” name=”Submit” value=”Submit”>
<form>
[/code]

I’ve seen several ways that forms get evaluated, and I would like my error message to list the errors according to what was incorrect, not one at a time. For example:

*error1 (if applicable)
*
error2 (if applicable)
*error3 (if applicable)

I have tried creating a for loop in PHP to do this, but it’s not doing what I want. Below is what I have:

[code]
<?php
for ($i=0; $i<=3; $i++) {

if ($txtOne==””) {
echo ‘wrong 1’;
}

else if ($chkOne==”” || $chkTwo==””) {
echo ‘wrong2’;
}

else if ($txtTwo==””) {
echo ‘wrong3’;
break;
}

}
[/code]

Can anyone tell me what I’m doing wrong?

Thanks,

Brian

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dreamcatcherNov 26.2003 — Hi Brian,

An if else statement will only return true once. So if the first part of the code is true, then it won`t continue and so on.

You can also lose the for loop. Just try if statements without the else clause:

[code=php]
if ($txtOne=="") {
echo 'wrong 1';
}

if ($chkOne=="" || $chkTwo=="") {
echo 'wrong2';
}

if ($txtTwo=="") {
echo 'wrong3';
[/code]


See if that works!
Copy linkTweet thisAlerts:
@haynbrianauthorNov 27.2003 — That works great, thanks!

Brian
×

Success!

Help @haynbrian 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.19,
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,
)...