/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] JSON and CSS questions

In the script below, all works fine.
Now I want to make a change.
I have two questions:

  • 1. How do I make the second <div> tag contents display below the first?
    I think this is a CSS float question associated with the ‘fieldset’ tag, but I don’t know how to clear after the first two fieldset displays. How would I modify this or am I completely wrong with the CSS settings?

    This is a minor problem, so I can jump over to the CSS forum if no answers show up here.

  • More interesting JSON question (at least for me).
    2. I have to use two arrays to check for errors and form the alert display.
    It seems I should be able to somehow read the IDs of the ‘Errs{}’ array
    without resorting to creating the ‘ErrIDS’ array. My knowledge of a correct
    JSON syntax is not up to the task. Any ideas?

    [code=php]
    <html>
    <head>
    <title>Verify with Error message Array</title>
    <style type=”text/css”>
    fieldset { width: 200px; float: left}
    </style>

    <script type=”text/javascript”>

    // JSON version: want to erase ErrIDS and use portion of Errs{} hash
    var ErrIDS = new Array(‘F0′,’F1′,’F2′,’F3’);
    var Errs = {}
    Errs = {‘F0′:’ – Name’,’F1′:’ – Address’,’F2′:’ – Email’,’F3′:’ – Phone’};

    function verify() {
    var errmsg = []
    for (var i=0; i<ErrIDS.length; i++) {
    if (document.getElementById(ErrIDS[i]).value == ”) { errmsg.push(Errs[ErrIDS[i]]); }
    }

    //alert if fields are empty and cancel form submit
    if (errmsg.length > 0) {
    alert(“You are required to complete the following fields:n”+errmsg.join(‘n’)); return false;
    } else {
    alert(‘Success – Error size: ‘+errmsg.length); return false; // used for testing (normally true)
    }
    }

    function CheckEmpty(IDS) {
    var obj = document.getElementById(IDS);
    if (obj.value == ”) { obj.style.backgroundColor = ‘#ffffff’; }
    else { obj.style.backgroundColor = ‘#ffff00′; }
    }

    </script>
    </head>
    <body>
    <form action=”” onSubmit=”return verify();” method=”POST” name=”inform”>

    <div>
    <fieldset><legend><strong>Name *</strong></legend>
    <input type=”text” id=’F0′ name=”name” value=”” size=”24″ onBlur=”CheckEmpty(this.id)”>
    </fieldset>

    <fieldset><legend><strong>Address *</strong></legend>
    <input type=”text” id=’F1′ name=”address” value=”” size=”24″ onBlur=”CheckEmpty(this.id)”>
    </fieldset>
    </div>
    <br />

    <div>
    <fieldset><legend><strong>Email *</strong></legend>
    </strong><input type=”text” id=’F2′ name=”email” value=”” size=”24″ onBlur=”CheckEmpty(this.id)”>
    </fieldset>

    <fieldset><legend><strong>Phone *</strong></legend>
    <input type=”text” id=’F3’ name=”phone” value=”” size=”24″ onBlur=”CheckEmpty(this.id)”>
    </fieldset>
    </div>

    <input value=”Submit” type=”submit”>
    </body>
    </html>
    [/code]

    to post a comment
    JavaScript

    9 Comments(s)

    Copy linkTweet thisAlerts:
    @KorFeb 09.2008 — JSON

    You can circle straight through the [COLOR="DarkGreen"]object[/COLOR]'s [COLOR="Blue"]property[/COLOR]/[COLOR="Red"]value[/COLOR] pairs like this:
    <i>
    </i>var prop;[COLOR="Gray"]//a custom variable, it will refer the object's property[/COLOR]
    for ([COLOR="Blue"]prop[/COLOR] in [COLOR="DarkGreen"]Obj[/COLOR]) {
    alert('property :'+[COLOR="Blue"]prop[/COLOR]+' has the value :'+[COLOR="Red"]Obj[prop][/COLOR])
    }

    Your case (condesed):
    <i>
    </i>var p, v;
    for ([COLOR="Blue"]p[/COLOR] in [COLOR="DarkGreen"]Errs[/COLOR]) {
    v=document.getElementById([COLOR="Blue"]p[/COLOR]).value;
    v==''?errmsg[errmsg.length]=[COLOR="Red"]Errs[p][/COLOR]:null;
    }


    I am confused about your first CSS problem. Can you detail, please?
    Copy linkTweet thisAlerts:
    @JMRKERauthorFeb 09.2008 — Thank you 'Kor'. That fixed my problem with JSON usage of my 'hash' array.

    The CSS problem is again my incomplete understanding of all that happens around me.

    I want:


    ---------------------------------------</H2>
    |--Name---------| |--Address--------|
    <H2>| _________<STRONG>__<EM>_ | | _</EM>__</STRONG>___________ |
    ---------------------------------------</H2>
    <HR>---------------------------------------</HR>
    |--Email<EM>_---------| |--Phone00--------|<br/>
    </EM><H2><EM>| _________<STRONG>__<EM>_ | | _</EM>__</STRONG>___________ |</EM>
    ---------------------------------------</H2><EM><br/>




    I get:

    </EM>
    ---------------------------------------</H2><EM><br/>
    |--Name---------| |--Address--------| ------------------------------- <br/><br/>
    | _________<STRONG>__<EM>_ | | _</EM>__</STRONG>________<STRONG>__<EM>_ | |--Email--------| |--Phone-----|<br/>
    --------------------------------------- | _</EM>__</STRONG>____<STRONG>__<EM>_ | | _</EM>__</STRONG>_______ |<br/>
    --------------------------------- <br/>
    <br/>




    Excuse the crude graphics.

    I assume it has to do with my 'float' assignment, but I'm not sure

    where to put the 'float: clear' assignment (if at all?)



    Thanks again for answering my main question about JSON. ?

    I can code around the CSS problem with a <TABLE>,

    but I am trying to improve my understanding of its usage.

    Copy linkTweet thisAlerts:
    @KorFeb 09.2008 — You can play with the CSS [B]float[/B] and [B]clear[/B] properties. Could be:
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Untitled Document&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
    &lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
    &lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;


    &lt;style type="text/css"&gt;
    .fields fieldset{
    float:left;
    }
    .clear{
    clear:both;
    }
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;form action="" onSubmit="return verify();" method="POST" name="inform"&gt;

    &lt;div class="fields"&gt;
    &lt;fieldset&gt;&lt;legend&gt;&lt;strong&gt;Name *&lt;/strong&gt;&lt;/legend&gt;
    &lt;input type="text" id='F0' name="name" value="" size="24" onBlur="CheckEmpty(this.id)"&gt;
    &lt;/fieldset&gt;

    &lt;fieldset [COLOR="Silver"]class="left"[/COLOR]&gt;&lt;legend&gt;&lt;strong&gt;Address *&lt;/strong&gt;&lt;/legend&gt;
    &lt;input type="text" id='F1' name="address" value="" size="24" onBlur="CheckEmpty(this.id)"&gt;
    &lt;/fieldset&gt;
    &lt;/div&gt;
    &lt;div class="fields clear"&gt;
    &lt;fieldset&gt;&lt;legend&gt;&lt;strong&gt;Email *&lt;/strong&gt;&lt;/legend&gt;
    &lt;/strong&gt;&lt;input type="text" id='F2' name="email" value="" size="24" onBlur="CheckEmpty(this.id)"&gt;
    &lt;/fieldset&gt;

    &lt;fieldset&gt;&lt;legend&gt;&lt;strong&gt;Phone *&lt;/strong&gt;&lt;/legend&gt;
    &lt;input type="text" id='F3' name="phone" value="" size="24" onBlur="CheckEmpty(this.id)"&gt;
    &lt;/fieldset&gt;
    &lt;/div&gt;
    &lt;div class="clear"&gt;
    &lt;input value="Submit" type="submit"&gt;
    &lt;/div&gt;
    &lt;/body&gt;
    &lt;/html&gt;

    Note that I have nested the submit button in a div, in order to clear the float:left of the previous element
    Copy linkTweet thisAlerts:
    @JMRKERauthorFeb 09.2008 — Thanks again 'Kor'. Your changes give me something to analyze.

    I was not aware I could do:

    1. <fieldset class="left"> when there was no definition of 'left';

    2. <div class="fields clear"> as I didn't know you could combine two definitions in one class.

    3. And I'm assuming that this:

    .fields fieldset{

    float:left;

    }

    only effects the <fieldset> within the <div>s with class 'fields'

    Takes me a little longer, but I'm still learning. ?
    Copy linkTweet thisAlerts:
    @KorFeb 09.2008 — eer, sorry :o ... that class="left" is useless and goes nowhere, as I have [I]no[/I] such a class... so that your 1. note has no basis.

    I forgot to remove it. Initially I tried other variant (with a class called left, but I turned to a simpler combination later); as I said, there are several possibilities, not only one... But I forgot to remove that...
    Copy linkTweet thisAlerts:
    @JMRKERauthorFeb 09.2008 — The effort is still very much appreciated.
    Copy linkTweet thisAlerts:
    @KorFeb 09.2008 — Following the modern trend to a complete separation between aspect and content you could also get rid of the <strong> tag on using also the CSS:
    <i>
    </i>.fields legend{
    font-weight:bold;
    }

    Furthermore, it is better to use an [I]external[/I] CSS file when dealing with multiple pages, as you can change a property's value for all of them at once, when needed.
    Copy linkTweet thisAlerts:
    @JMRKERauthorFeb 09.2008 — I agree with you that the CSS (and JS code for that matter) would be better placed in external files. However, I tend to be lazy when I'm in a development phase trying out code to see if it will work as I expect, with plans to 'pretty' it up later. The code I posted was just supposed to be a generic form with required elements to test the concepts. That's also why I returned a 'false' in the verify regardless of the success or failure of the entries. My main focus what in the JSON and CSS elements, which thankfully, you helped me understand a lot better.
    Copy linkTweet thisAlerts:
    @KorFeb 09.2008 — In fact laziness made me (and all of us ? ) using CSS in that way. The client usually changes his mind within hours, so that I learned to put as much as possible attributes (eggs) in a single (or several) CSS external file (basket), in order to change them later within a single move...

    Good luck on your project! ?
    ×

    Success!

    Help @JMRKER 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 4.29,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

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

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