/    Sign up×
Community /Pin to ProfileBookmark

Tricky Form Question

Hey Everyone!

I wanted to ask y’all a tricky question and any help would be greatly appreciated. Let’s say a portion of a form has 4 input boxes. If they’re empty, they’re simply ignored by the form processing script, BUT if there’s data in 1 box, there’s gotta be data in the rest of the boxes, for example if you enter data in the address, the other boxes that says City, State, Zip must have data in those boxes, validation is done with parsley so that’s taken care of. I’m just trying to figure out how to make 3 boxes requiring data IF 1 box has data in there.

CasperGemini

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 07.2014 — Simply check for the values that you require. Isn't that what you mean?

What's parsley - besides a condiment?
Copy linkTweet thisAlerts:
@CasperGeminiauthorAug 07.2014 — No, I'm just looking for something when there's anything in 1 input box, the other 3 input boxes requires data as well.

As for Parsley, it's basically a Javascript Form validation Library. It's being implemented into my project as we speak. Their site is at http://parsleyjs.org/ and it's awesome. I like the newer version where it will validate phone numbers, zip code, email addresses, etc.

Simply check for the values that you require. Isn't that what you mean?

What's parsley - besides a condiment?[/QUOTE]
Copy linkTweet thisAlerts:
@NogDogAug 07.2014 — Nevermind: my logic was flawed. I shall think about it some more....
Copy linkTweet thisAlerts:
@NogDogAug 07.2014 — Okay, how about this?
[code=php]
$requiredGroup = array(
'fld1',
'fld2',
'fld3'
);

$numFilled = 0;
foreach($requiredGroup as $key) {
if(isset($_POST[$key]) and trim($_POST[$key]) !== '') {
$numFilled += 1;
}
}
if($numFilled != 0 and $numFilled != count($requiredGroup)) {
// handle error condtion for all-or-none case
}
[/code]
Copy linkTweet thisAlerts:
@CasperGeminiauthorAug 07.2014 — hmm, not sure how to implement that code, but here's the code...

<div data-row-span="8">

<div data-field-span="1">

<label for="date_From1">From</label>

<input name="date_From1" type="text">

</div>

<div data-field-span="1">
<label for="date_To1">To</label>
<input name="date_To1" type="text">
</div>

<div data-field-span="2">
<label for="address_Ph1">address</label>
<input name="address_Ph1" type="text">
</div>

<div data-field-span="1">
<label for="info_set1">Set 1
<input name="info_set1"type="text">
</div>

<div data-field-span="1">
<label for="info_set2">Set 2</label>
<input name="info_set2"type="text">
</div>

<div data-field-span="2">
<label for="info_set3">Set 3</label>
<input name="info_set3"type="text">
</div>
</div>


So if anyone were to enter anything into the 'from' input box, the boxes in the 'to', 'address', 'set1', 'set2', and 'set3' input boxes must be filled in. However if the input box in the 'from' is empty, the rest is ignored, especially at validation, but i'd need something on-the-fly if possible. I just need to understand the concept/structure on how to set up something like this for not only this form, but future forms just in case.
Copy linkTweet thisAlerts:
@ginerjmAug 07.2014 — I think you need to write some php. Some code of some kind anyway. Html is not going to do it for you, nor are we.
Copy linkTweet thisAlerts:
@NogDogAug 07.2014 — The code I wrote was for server-side validation, and since you posted your question in the PHP forum, I assumed you wanted some PHP code. Yes, I just used some made-up sample names for the form fields, but you should be able to extrapolate from that. If you're looking for some client-side (browser-based) solution, you probably should be posting in the JavaScript forum. Of course, you should always check any required stuff on the server side, as malicious users (or just users who have JavaScript turned off for any reason) would bypass your JS validation.
Copy linkTweet thisAlerts:
@CasperGeminiauthorAug 11.2014 — it's not an HTML form, it's a snippet of one, completely modified but the basic concept is still there.

Yeah i do want to add in some php code somewhere in the HTML form somehow ,but I didn't take javascript into consideration, perhaps I'll try looking into that in my google searches. I don't expect anyone to re-write the code. I just need an example so I can understand the concept and take it from there. Links to tutorials would rock. Thanks for all of your help though in advance!

As for the javascript bypassing the validation, not gonna happen, Parsley.js helps a lot along with masking, in addition the form can't be sent unless a captcha is solved, it's already in the page as well. :-)
Copy linkTweet thisAlerts:
@NogDogAug 11.2014 — If it's JavaScript, it's viewable by anyone who cares enough to look at it, and thus is bypass-able by anyone who knows how to make a cURL request. Whether or not anyone would care to bother depends entirely on what the form actually does. ? Always, [i]always[/i] do server-side validation of any user request data that can possibly be harmful if sent to your server, and [i]never[/i] assume your client-side code will prevent it -- you'll potentially save yourself a lot of time and aggravation (and maybe money) later.
Copy linkTweet thisAlerts:
@CasperGeminiauthorAug 19.2014 — I think I should do this in php instead of Javascript, You're right NogDog, I'm thinking of "if one box is NOT empty, then the data in another box is required, otherwise ignore it" in a kind of sense...

I just need a template on how to do this, basically the structure, and I'll figure the rest...I'm still researching this and trying to figure something out... would be awesome if I could do this on the fly somehow...
×

Success!

Help @CasperGemini 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.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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