www.webdeveloper.com
Results 1 to 11 of 11

Thread: Tricky Form Question

  1. #1
    Join Date
    Feb 2010
    Posts
    22

    Question 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

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    Simply check for the values that you require. Isn't that what you mean?

    What's parsley - besides a condiment?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    Feb 2010
    Posts
    22
    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.

    Quote Originally Posted by ginerjm View Post
    Simply check for the values that you require. Isn't that what you mean?

    What's parsley - besides a condiment?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    Nevermind: my logic was flawed. I shall think about it some more....
    Last edited by NogDog; 08-07-2014 at 01:57 PM. Reason: deleting, my logic was flawed.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    Okay, how about this?
    PHP Code:
    $requiredGroup = array(
        
    'fld1',
        
    'fld2',
        
    'fld3'
    );

    $numFilled 0;
    foreach(
    $requiredGroup as $key) {
        if(isset(
    $_POST[$key]) and trim($_POST[$key]) !== '') {
            
    $numFilled += 1;
        }
    }
    if(
    $numFilled != and $numFilled != count($requiredGroup)) {
        
    // handle error condtion for all-or-none case

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Feb 2010
    Posts
    22
    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.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    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.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Feb 2010
    Posts
    22
    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. :-)
    Last edited by CasperGemini; 08-10-2014 at 07:16 PM.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    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, always do server-side validation of any user request data that can possibly be harmful if sent to your server, and never assume your client-side code will prevent it -- you'll potentially save yourself a lot of time and aggravation (and maybe money) later.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Feb 2010
    Posts
    22
    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...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles