www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript, form, select, cookie

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Angry javascript, form, select, cookie

    Hello everyone,

    hmmmm....i'll try attaching the source file...failed :-(

    I've spend the better part of a day trying to learn javascript by cobbling together examples.

    I'm almost there for step 1. All I want to do is create a 'Form Select Menu' & when I set it a cookie is saved and the page is reloaded accorded to the selected option in the form.

    The cookie gets set but it seems unable to read it. Also, I'm not sure but <body onLoad="setupState()"> doesn't seem to trigger.

    I'm telling you web page programming is a mess. I'm not sure if I want to learn this or not.

    It looks like this:


    +++++++++++++++++++++++++++++++++++++++++++++++


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <HTML lang="en">
    <head>
    <LINK REL=STYLESHEET HREF="../../utility/main.css" TYPE="text/css">
    <script type="text/javascript" language="JavaScript">
    <!--
    function readCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
    }

    function writeCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
    ( (expires) ? ";expires=" + expires.toGMTString() : "") +
    ( (path) ? ";path=" + path : "") +
    ( (domain) ? ";domain=" + domain : "") +
    ( (secure) ? ";secure" : "");
    }


    function setupState()
    {

    if (stateProfile)
    getValues(stateProfile);
    document.write('In setupForm');
    }

    function getValues(string) {
    getValue(string,"user", document.profileForm.user, "text");
    getValue(string,"email", document.profileForm.email, "text");
    getValue(string,"state",document.profileForm.country,"select");
    getValue(string,"age", document.profileForm.age, "select");
    getValue(string,"sex", document.profileForm.sex, "radio");

    for (var i=0;i<7+1;i++)
    getValue(string,"i"+i, eval("document.stateForm.i"+i), "checkbox");
    }
    function replace(string, text, by)
    {
    // Replaces text with by in string
    var i = string.indexOf(text);
    var newstr = '';

    if ((!i) || (i == -1))
    return string;

    newstr += string.substring(0, i) + by;

    if (i+text.length < string.length)
    newstr += replace(string.substring(i+text.length, string.length), text, by);

    return newstr;
    }

    function getValue(string,elementName,object,elementType) {
    // gets value of elementName from string and populates object of elementType

    var startPos = string.indexOf(elementName + "=")

    if (startPos > -1) {
    startPos = startPos + elementName.length + 1;
    var endPos = string.indexOf("&",startPos);
    if (endPos == -1) endPos = string.length;

    var elementValue = unescape(string.substring(startPos,endPos));

    if (elementType == "text") object.value = elementValue;
    if (elementType == "password") object.value = elementValue;
    if (elementType == "select") object.selectedIndex = elementValue;
    if (elementType == "checkbox") object.checked = onCheck(elementValue);
    if (elementType == "radio") object[elementValue].checked = true;
    }
    }//-->
    </script>
    </head>
    <body onLoad="setupState()">
    <script type="text/javascript" language="javascript">
    <!--
    var today = new Date();
    var expires = new Date(today.getTime() + (56 * 86400000));
    var searchString = replace(self.location.search.substring(1),"+"," ");

    if (searchString.length > 0)
    writeCookie("stateProfile", searchString, expires);

    var stateProfile = readCookie("stateProfile");

    if (!stateProfile)
    {
    document.write('<P>Welcome,<P>According to your records '+stateProfile);
    document.write('<br>you have not set your user profile:');
    }
    else
    {
    document.write('<P>Welcome back,<P>According to your records '+stateProfile);
    document.write('the following settings are held in your profile:');
    }
    //-->
    </script>
    <div align="center">
    <form name="stateForm">
    <select name="state" onChange="this.form.submit();">
    <option value="AZ">Arizona</option>
    <option selected value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DC">District of Columbia</option>
    <option value="DE">Delaware</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="ID">Idaho</option>
    <option value="IA">Iowa</option>
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="MA">Massachusetts</option>
    <option value="MD">Maryland</option>
    <option value="ME">Maine</option>
    <option value="MI">Michigan</option>
    <option value="MN">Minnesota</option>
    <option value="MO">Missouri</option>
    <option value="MT">Montana</option>
    <option value="NC">North Carolina</option>
    <option value="ND">North Dakota</option>
    <option value="NE">Nebraska</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NM">New Mexico</option>
    <option value="NY">New York</option>
    <option value="OH">Ohio</option>
    <option value="OK">Oklahoma</option>
    <option value="OR">Oregon</option>
    <option value="PA">Pennsylvania</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="SD">South Dakota</option>
    <option value="TN">Tennessee</option>
    <option value="TX">Texas</option>
    <option value="VA">Virginia</option>
    <option value="VT">Vermont</option>
    <option value="WA">Washington</option>
    <option value="WI">Wisconsin</option>
    <option value="WV">West Virginia</option>
    </select>
    </div>
    </form>
    <br>
    <script type="text/javascript">
    switch(document.stateForm.state.value)
    {
    case ("AZ"):
    document.write('<script type="text/javascript" src="http://loref.com/Compact/AZ_Customer.php">');
    break;
    case ("CA"):
    document.write('<script type="text/javascript" src="http://loref.com/Compact/CA_Customer.php">');
    break;

    ...deleted because post too long...
    Last edited by clueless00; 03-31-2009 at 08:29 PM. Reason: attachment

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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