JavaScript - Use Form Input To Write Cookie For Redirection
Hello. My goal is for the user's input on their first visit to redirect their landing page in the future. I am a novice and any help is very much appreciated.
The form's drop down menu includes three cities (Calgary, Toronto, Vancouver). When the user presses "submit form", the script should create a cookie to store the user's city selection. Next time the user visits the site, they should be redirected to their city's site, ie. "/vancouver.html" Right now, the code is returning an error from the onload("redirect();") function. The error indicates I am trying to redirect to "/[object HTMLSelectElement].html", but I am not sure if the trouble is in reading or writing the cookie. For simplicity, I've removed most of the other content from this page (my cookie.js is attached as a .doc): Code: <title>Site Title</title> <script type="text/javascript" src="cookie.js"></script> <script language="JavaScript" type="text/javascript"> function redirect() { userCity = readCookie ("user_city"); if (userCity) { window.location.replace(userCity + ".html"); } else { window.location.replace("index.html"); } } function setCity(box) { { var userCity =(document.welcomeSignUp.userCity); for (i=0; i<userCity.length; i++) { if (userCity[i].name != box.name) { userCity[i].selectedIndex = 0; } } } writeCookie("user_city", userCity, 5 * 365); } </script> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <form id="welcomeSignUp" action="" method="get" name="welcomeSignUp" onload="redirect();"> Your Email address: <input type="text" name="email" value="" size ="20" /> <br /> Choose Your City: <select name="userCity" size="20"> <option value="one">Calgary</option> <option value="two">Toronto</option> <option value="three">Vancouver</option> </select> <a href="index.html"><button class="rounded" onclick="setCity(this);"> <span>submit form</span> </button></a> </form> </body> </html> Similar TutorialsHi, I want to write a javascript program which will redirect the user from a welcome page to an an intro page when a cookie is found. If there are not cookies found, the welcome page is loaded but a cookie is written so that next time the user visits the page, he will be redirected to the intro page automatically. This program would allow only new visitor to view the welcome page and other visitors to skip the welcome page to be brought to the intro page right away. I am a newbie and here is the code which I have though of so far. Can someone expand on it? Thanks The Apprentice Code: window.onload = pageInit; function pageInit(){ var visited = ""; if (document.cookie !=""){ window.location = "introPage.htm"; } else{ setCookie(); } function setCookie(){ document.getElementById("click").onclick = cookie; var expireDate = new Date(); expireDate.setMonth(expireDate.getMonth() + 6); hello recently i finally managed to make a working shopping cart system, in that i can add as many items as i want and it will work, all using cookies. Then i have another page, the cart which reads the cookie entry made with each demo purchase, then using a while loop lists these items, the problem i am having, is that when i add the textbox which would hold the value of the quantity, it is put out on the next line down, instead of next to the text which i find quite annoying. Also i would like to know how i can add a button next to the textbox, for updating the quantity, also ide like to know how i could access that button, or have it do something, since it wouldnt be prewritten. any help would be greatly apreciated. here is a zip file containing the html files of the customizable purchase page, and the new shopping cart. be sure to refresh the page before trying to use the first page, so it establishes the counter cookie. how would you make when you press a button it writes somthing in a input box.
this had to do wiht HTML and javascirp thow would you make a function that wirtes somthing in a input box(sorry about messy ness i wasint looking)
I'm a non-web programmer being forced to do some web programming. My immediate problem is that, on submission of a form, I need to be able to open a url (that I control) with the filled-in form values appended to the new url. In other words, I need to be able to do the equivalent of : Code: <form> <input name="key1" type="text"/> <input name="key2" type="text"/> <input type="button" value="Submit" onclick="window.open('http://new.domain.com/newpage.html?key1=val1&key2=val2')"/> </form> I would really like to do this without server-side code. Pointers? TIA Hello: I have two input boxes on a form which receive the first and last name of a user. I want to be able to use charAt() to get the first letter in each box and pass it to a third box on the same form. Is this doable? May I request some help? I was thinking something like the function below would be the starting point --I stand correct, as it is not working. Code: <script type=\"text/javascript\"> var str=\"Mossa\"; <!--document.write(str.charAt(0));--> document.write('<INPUT TYPE=text size=2 VALUE=\"str.charAt(0);\">'); var str=\"Barandao\"; document.write('<INPUT TYPE=text VALUE=\"str.charAt(0)\">'); <!--document.write(str.charAt(0));--> </script> Any thoughts! Mossa Hello all! As it could not be otherwise, in my first post here, I have a horribly simple problem... I'm currently working on a homepage for my browser, where I have many frequently used links, and I'm trying to implement some search engines too. The one I'm working on is simply, a predefined URL, and the input text string at the end. For example: "http://www.example.com/search/(textinput)" This is the code I have so far: Code: <script type="text/javascript"> function gosearch() { var srchstring = document.getElementById('string').value; var resulturl = 'http://www.example.com/search/' + srchstring; window.location.href = resulturl; } </script> <form name="searchform" onsubmit="gosearch();"> <input id="string" size="31" maxlength="255" value="" /> <input type="submit" value="Search!" /> </form> But what happens by using this code, is that, to the current URL, a "?" gets added, as if the code tried to send some info using GET. I could do this quite easily using PHP, but as I want to use this locally, I cannot use any server-side code... Besides the fact that I want to learn some JScript too. Thanks for your help. hello, I am trying to figure out how to hide the direction page in this code: I want to hide: www.ipage.com/mail/index.bml <INPUT TYPE="submit" NAME="Log In" VALUE="Check Mail" onclick="javascript:login.action='http://www.ipage.com/mail/index.bml?s=1';javascript:login.destination.value = '/mailadmin/index.bml?check=1';" > </TD> <TD> <INPUT TYPE="submit" NAME="Log In" VALUE="Manage Mail" onclick="javascript:login.action='https://secure.ipage.com/mailLogin';javascript:login.destination.value = '/mailadmin/index.bml?manage=1';"> any help is appreciated.... I have a form in an iframe that submits nicely. Here is one of the hidden fields that gives the page to redirect to after submitting the form: Code: <input type="hidden" name="rtrn" value="http://www.domain.com/thanks.html" target="_parent"> The parent target doesn't work, does anyone know how I get the thanks page to replace the page containing the iframe? Hi every one Following is the situation I want a solution for, -I have a form whose contents are forwarded to a mailer program hosted on a server which sends out emails. So basically the form data is submitted to one Mailer page (I have no control over the mailer program). -After the form has been submitted the mailer program redirects to a default page or the "return " page specified by me..again redirects no matter what I have no control over it... -Now instead of being redirected to a page I want the form contents to be submitted to another php page which will appear after the form has been submitted to the mailer page and display the contents of the form originally submitted. Basically I want to submit to 2 different pages I know thats possible but I do not know how to get around the redirection from mailer page. I thought of having the second php page to be the redirect page with all form data sent in query string however data fields are too many to send over get. Is there anyway around this in javascript? Its very complex to explain..I hope some one can help I hope following crude visualisation helps currently submit form to------------->Mailer page (It redirects to any page specified) form to | | |------->Page2.php (Want this to appear after form has been submitted to mailer page but mailer page redirects..) Regards, Aditya I am thinking this should be fairly easy but yet I am not getting far. I want to have a form with a single text imput field for a zip code. Depending on which zip code the user enters will determine which url they will be sent to. If they enter a zip code which is not in the script, they would be sent to a default url. I am also assuming this can be accomplished with javascript. Any help is greatly appreciated. Hi, i want to set an input value to the same value as the input selected from another input on the same form so when the user selects input 1 i want the hidden input2 to get the same value im guessing i use onsubmit because its hidden so there wont be a focus or blur this would be part of the input Code: onsubmit="(this.value = this.othervalue)" othervalue being the other input name="othervalue" is that the correct syntax I have a Cisco ASA that I use as a proxy server as such. I have a internal site that it will not pass credentials to so I was looking for a way to do that. Not being a site developer the only way I can come up with is to maybe pass it by using a cookie to store the password and ID. Below is the code I am trying to use. I can create the cookie with the code below but it will not change the PATH or the Domain for some reason. Also I do not think it is reading from the cookie not sure why. Basically I need it to set the cookie with the user info and then grab the info from the cookie to auto log the user in to the internal site. <script type="text/javascript" language="JavaScript"> <!-- function writeCookie( ) { var username = document.form.username.value; // Get the user's name var password = document.form.password.value; document.cookie = "UserIDPW=" + username + "=" + password; // Create the cookie } //--> <form name="form" id="unicorn_form" method="POST" onsubmit="writeCookie()" onsubmit="disableButton()" action="/+webvpn+/index.html" onsubmit="return onSubmit();"> <td nowrap id="username_field"> <p align="right"> <span style="font-size: 8pt; font-weight: 700"> <font face="Trebuchet, arial" color="#FFFFFF"> <b>User ID:</b> </font></span></font></td> <td valign="middle"> <input class="formData" size="20" type="password" id="password_input" name="username" value=""> </td> </tr> <tr> <!-- password prompt --> <td nowrap id="password_field"> <p align="right"> <span style="font-size: 8pt; font-weight: 700"> <font face="Trebuchet, arial" color="#FFFFFF"> <b>Password:</b> </font></span></font></td> <td valign="middle"> <input class="formData" size="20" autocomplete="off" id="password_input" name="password" type="password"> </td> </tr> <tr> <td> </td> <td align="center"> <!-- "log in" button --> <input type="submit" name="Login" value="Log In" class="button"> Please assist, I have the following code to remember form inormation but it does not seem to work. Probably something stupid. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Contact Page</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" href="yorkie.css" type="text/css" /> <script type="text/javascript"> /* <![CDATA[ */ // Confirm resetting of forms. function confirmReset() { var resetForm = confirm("Are you sure you want to reset the form?"); if (resetForm == true) return true; return false; } // If box clicked value within disappears function Focus(element) { if (element.value == element.defaultValue) { element.value = ''; } } // If nothing typed and box onblur the default value is restored within function Blank(element) { if (element.value == '') { element.value = element.defaultValue; } } // Submission of Contact Form Verified function verifySubmission() { var retValue = true; if (document.forms[2].email.value == "Enter Email Address" || document.forms[2].firstname.value == "Enter First Name" || document.forms[2].lastname.value == "Enter Last Name" || document.forms[2].telephone.value == "Enter Contact number") { window.alert("You did not fill in one of the following required fields: First Name, Last Name, Email Address, or Telephone Number."); retValue = false; } return retValue; } // Email address verified as a valid email address function validateEmail(formObject) { var email = formObject.value; var emailCheck = /^[_\w\-]+(\.[_\w\-]+)*@[\w\-]+(\.[\w\-]+)*(\.[\D]{2,3})$/; if (emailCheck.test(email) == false) { window.alert("The e-mail address you entered does not appear to be valid."); formObject.value = formObject.defaultValue; return false; } formObject.value = email.toLowerCase(); return true; } // Telephone number verified as a valid 10 digit number with no alpha characters function verifyPhone(number) { var phone = number.value; var phoneCheck = /^(\d{10})$/; if(phoneCheck.test(phone) == false) { window.alert("The telephone number you entered does not appear to be valid."); number.value = number.defaultValue; return false; } return true; } // Show additional contact form details onclick of yes radio button function showDiv() { document.getElementById('breeders').style.visibility = 'visible'; } // Hide certain contact form info onclick of No radio button function hideDiv() { document.getElementById('breeders').style.visibility = 'hidden'; } // SAVE FORM INFORMATION FOR THE NEXT VISIT function saveSelections(frm) { var setvalue; var fieldType; var index; var formname = frm.name; // CookieExpiry in 30 days. var today = new Date(); var exp = new Date(today.getTime()+30*24*60*60*1000); var string = "formname=" + formname + "|"; var cookieName = formname; var n = frm.length; for (i = 0; i < n; i++) { e = frm[i].name; fieldValue = frm[i].value; fieldType = frm[i].type; // Radio Buttons if (fieldType == "radio") { for (x=0; x < frm.elements[e].length; x++) { if (frm.elements[e][x].checked) { index = x } } string = string + index + "\|"; } // Text, Textarea, and dropdowns if ((fieldType == "text") || (fieldType == "textarea") || (fieldType == "select")) { string = string + frm.elements[e].value + "\|"; } // Checkboxes if (fieldType == "checkbox") { if (frm.elements[e].checked==true) { var setvalue = "1"; } if (frm.elements[e].checked==false) { var setvalue = "0"; } string = string + setvalue + "\|"; } // Hidden field if (fieldType == "hidden") { string = string + frm.elements[e].value + "\|"; } } setCookie(cookieName, string, exp); } // Load Form Fields from saved cookie function loadSelections(frm) { var e; var z; var x; var cookieName; var fieldArray; var fieldValues; var fieldValue; var formname = frm.id; // Retrieve form elements from cookie and split into array. cookieName = formname; fieldValues = getCookie(cookieName); fieldArray = fieldValues.split("\|"); var n = frm.length; for (i = 0; i < n; i++) { e = frm[i].name; z = i; z++; var fieldType = frm[i].type; var fieldValue = fieldArray[z]; // Radio Buttons if (fieldType == "radio") { x = fieldValue; frm.elements[e][x].checked = true; } // Text, Textarea, and dropdowns if ((fieldType == "text") || (fieldType == "textarea") || (fieldType == "select")) { frm.elements[e].value = fieldValue; } // Checkboxes if (fieldType == "checkbox") { var fld_checkbox = fieldValue; if (fld_checkbox == "1") { frm.elements[e].checked = true; } } // Hidden fields if (fieldType == "hidden") { frm.elements[e].value = fieldValue; } } } // COOKIE FUNCTIONS function setCookie(name, value, expires) { document.cookie= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : ""); } function getCookie(name) { var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else { begin += 2; } var end = document.cookie.indexOf(";", begin); if (end == -1) { end = dc.length; } return unescape(dc.substring(begin + prefix.length, end)); } /* ]]> */ </script> </head> <body class="contact" onload="var begin=setInterval('changeBanner()',9000); currentClock(); currentDate(); document.getElementById('breeders').style.visibility = 'hidden'; " onunload="saveSelections(document.forms[0])"> <table width="92%"> <form action="mailto:lynette@sayorkies.co.za, subject=Contact Form" method="post" enctype="text/plain" onsubmit="return verifySubmission()" onreset="return confirmReset();"> <table class="center" cellpadding="5" border="1"> <!-- This enters a horizontal line --> <tr><td><hr /></td></tr> <!-- This is for the personal information --> <tr><td><strong>Tell us who you a </strong></td></tr> <tr><td><input type="text" name="firstname" size="25" maxlength="35" value="Enter First Name" onfocus="Focus(this);" onblur="Blank(this);" /> * <input type="text" name="lastname" style="margin-left: 52px;" size="25" maxlength="35" value="Enter Last Name" onfocus="Focus(this);" onblur="Blank(this);" /> *</td></tr> <!-- This is for the contact information --> <tr><td><strong>Your Contact details:</strong></td></tr> <tr><td><input type="text" name="email" size="30" maxlength="60" value="Enter Email Address" onfocus="Focus(this);" onblur="Blank(this); validateEmail(this)" /> * <input type="text" name="telephone" style="margin-left: 20px;" size="20" maxlength="10" value="Enter Contact number" onfocus="Focus(this);" onblur="Blank(this); verifyPhone(this)" /> * </td></tr> <!-- This is where you enter your message also select what type of message--> <tr><td><strong>Your Message:</strong></td></tr> <tr><td align="left">What is your message about?<br /> <input type="radio" name="type" value="compliment" /> Compliment <input type="radio" name="type" value="complaint" /> Complaint <input type="radio" name="type" value="general" checked="checked" /> General <input type="radio" name="type" value="enquiry" /> Enquiry <input type="radio" name="type" value="feedback" /> Feedback</td></tr> <tr><td align="center"><textarea name="message" rows="6" cols="45" onfocus="Focus(this);" onblur="Blank(this)">Enter your message here...</textarea></td></tr> <!-- Additional information --> <tr><td align="left">Are you looking to purchase a Yorkie puppy?<br /> <input type="radio" name="purchase" value="yes" /> Yes <input type="radio" name="purchase" value="no" checked="checked" /> No</td></tr> <tr><td align="left">Do you own a Yorkie?<br /> <input type="radio" name="owner" value="yes" onclick="showDiv()" /> Yes <input type="radio" name="owner" value="no" checked="checked" onclick="hideDiv()" /> No<br /> <!-- If answer is no above the below questions are hidden, if yes then they appear --> <div id="breeders"> How many? <select name="qnty" size="1"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5+</option></select> Are you a breeder? <input type="radio" name="breeder" value="yes" /> Yes <input type="radio" name="breeder" value="no" checked="checked" /> No</div></td></tr> <!-- Subscriptions --> <tr><td align="left">Do you wish to subscribe to the following?<br /> <input type="checkbox" name="subscribe" value="letter" checked="checked" /> Monthly Newsletter<br /> <input type="checkbox" name="subscribe" value="pups" /> Notification of a new litter<br /> <input type="checkbox" name="subscribe" value="updates" /> Notification of site updates<br /> <input type="checkbox" name="subscribe" value="register" /> Registrations</td></tr> <!-- Add a picture file --> <tr><td align="left">Upload your favourite Yorkie photo for the Picture of the Month.<br /> <input type="file" name="upload" accept="image/gif, image/jpeg" size="30" style="margin-left: 15px; margin-top:5px;" /></td></tr> <!-- This enters a horizontal line --> <tr><td><hr /></td></tr> <tr><td align="center">Required fields marked with *</td></tr> <!-- Buttons to clear the form or to submit the form --> <tr><td align="center"><input type="reset" value="Clear the Form" /> <input type="submit" value="Send your message" /></td></tr> </table></form<script type="text/javascript">loadSelections(document.forms[0]);</script> </body> </html> Console Error: Unable to get value of the property 'split': object is null or undefined for this line: Code: fieldArray = fieldValues.split("\|"); I am hitting a brick wall, dont know what to do? Using the url test.html?a=john&b=doe I am using the following code: <script type="text/javascript" language="javascript"> function getUrlVars() { var map = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi; function(m,key,value) { map[key] = value; }); return map; } var john=getUrlVars(map[a]); alert (getUrlVars(map[a])) alert (john) </script> I can't seem to get an alert to show me anything. The eventual result is not an alert, but a document.write to post the url contents to various form fields for firstname= and lastname= etc. I am really having a hard time using the document.write function, and am not sure how to employ it, whether in the same script statement or in a separate script in the body. I'm having a heck of a time getting a code to populate several hidden fields in the following form that I'm extracting from the url. It is posting to the appropriate CGI, however the hidden variables are not submitted. Code: <script type="text/javascript"> str=document.URL; temp = str.split('//'); temp1 = temp[1]; temp2 = temp1.split('.'); temp3 = str.split('/'); domain = ('http://' + temp3[2]); returnthanks = (domain); association = temp2[0]; poibegin = (str.lastIndexOf('/')+1); poiend = (str.lastIndexOf('.')); poi = (str.substring(poibegin)); </script><script language="JavaScript" type="text/javascript"> function doSubmit(){ document.w2lForm('00N30000004C6Rg').value=association; document.w2lForm.('00N30000004C2u2').value=poi; document.w2lForm.('retURL').value=returnthanks; document.w2lForm.submit(); } </script> <h6 style="text-align: center">Submit your information and a professional will contact you.</h6> <form method="post" action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" name="w2lForm"> <input type="hidden" name="oid" value="005040020320220B3eR" /> <label for="first_name">First Name</label> <input id="first_name" maxlength="40" name="first_name" type="text" /><br /> <label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" type="text" /><br /> <label for="email">Email</label><input id="email" maxlength="80" name="email" type="text" /><br /> Best Time to Call:<select id="00N30000004NaDm" title="Best Time to Call" multiple="multiple" name="00N30000004NaDm"> <option value="(select at least one)">(select at least one)</option> <option value="Before 9AM (EST)">Before 9AM (EST)</option> <option value="9AM to 1PM">9AM to 1PM</option> <option value="1PM to 6PM">1PM to 6PM</option> <option value="After 6pm">After 6pm</option> </select><br /> <input id="00N30000004C2u2" type="hidden" name="00N30000004C2u2" /> <input id="00N30000004C6Rg" type="hidden" name="00N30000004C6Rg" /> <input type="submit" name="submit" value="Submit Request Information" /> </form> Hi ! i sometimes are able to alter simple javascript, but i need help with the following. MaxMind.com has a javascript or geotargetting. It displays the country where ur at. I need this output in a form, so i can store it in a database. this is the script i use: <script language="JavaScript" src="http://j.maxmind.com/app/country.js"></script> <br>Country Code: <script language="JavaScript">document.write(geoip_country _code());</script> <br>Country Name: <script language="JavaScript">document.write(geoip_country _name());</script> How can i get the country name to be displayed in an input field ? something like: <form name="xx" action="somepage.htm" method="post"> <input type="Text" size="50" name="country" value="country name"> </form> Hi everyone, I am using a jQuery cookie script to set the cookie of some elements on my website. One of the problems is that I need the cookie to not expire after one day, I need it to expire after a while (I'm going to start off with a year). Here's my script, the red part is what I've been editing. Code: /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ /** * Create a cookie with the given name and value and other optional parameters. * * @example $.cookie('the_cookie', 'the_value'); * @desc Set the value of a cookie. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secu true }); * @desc Create a cookie with all available options. * @example $.cookie('the_cookie', 'the_value'); * @desc Create a session cookie. * @example $.cookie('the_cookie', null); * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain * used when the cookie was set. * * @param String name The name of the cookie. * @param String value The value of the cookie. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. * If set to null or omitted, the cookie will be a session cookie and will not be retained * when the the browser exits. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will * require a secure protocol (like HTTPS). * @type undefined * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ /** * Get the value of a cookie with the given name. * * @example $.cookie('the_cookie'); * @desc Get the value of a cookie. * * @param String name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000 * 365)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; Hey there, I'm new to the magical world of jscript- have been introduced to it as I'm creating a fictional retail company for a business assignment. The code below is for my 'products' page where I've put a ''checkout' form at the bottom of. There are a few products inside a form...it currently uses jscript to work out a total value of all the product quantities chosen by the user. What I'm trying to do though is get the code to show which products the user has chosen (and possibly how many of each) in the 'Checkout' form at the bottom of the page. Everytime I try anything, the code calculate falls apart Would really appreciate any help or advice you could provide - cheers studentnet.kingston.ac.uk/~k0602276/Products.html Code: <head> <!-- Cart Script --> <script language="JavaScript" type="text/javascript"> function CalculateTotal(frm) { var order_total = 0 for (var i=0; i < frm.elements.length; ++i) { form_field = frm.elements[i] form_name = form_field.name if (form_name.substring(0,4) == "PROD") { item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1)) item_quantity = parseInt(form_field.value) if (item_quantity >= 0) { order_total += item_quantity * item_price } } } frm.TOTAL.value = round_decimals(order_total, 2) } function round_decimals(original_number, decimals) { var result1 = original_number * Math.pow(10, decimals) var result2 = Math.round(result1) var result3 = result2 / Math.pow(10, decimals) return pad_with_zeros(result3, decimals) } function pad_with_zeros(rounded_value, decimal_places) { // Convert the number to a string var value_string = rounded_value.toString() // Locate the decimal point var decimal_location = value_string.indexOf(".") // Is there a decimal point? if (decimal_location == -1) { decimal_part_length = 0 value_string += decimal_places > 0 ? "." : "" } else { decimal_part_length = value_string.length - decimal_location - 1 } var pad_total = decimal_places - decimal_part_length if (pad_total > 0) { for (var counter = 1; counter <= pad_total; counter++) value_string += "0" } return value_string } </script> </head> <body> <!-- Products --> <div class="Products"> <table title="Products" id="Products" border="1" table width="100%" border=0 cellpadding=0 cellspacing=0 bgcolor="#777777"> <form method="post" action="order.php"> <TD COLSPAN=3 ALIGN="CENTER"><h3>Premium Acoustic Guitars</h3></TD> <TR><TD ALIGN="CENTER"><b>Quantity:</b></TD> <TD ALIGN="CENTER"><b>Description</b></TD><TD ALIGN="CENTER"><b>Price</b> <i>(each)</i></B></TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G1_259.99" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G1- Fnd Tim Armstrong Hellcat.jpg"><b>Model Name:</b> Tim Armstrong Hellcat Acoustic <br><b>Colours:</b> Natural <br><b>Body:</b> Solid Mahogany <br><b>Neck:</b> Maple <br><b>Fingerboard:</b> Rosewood <br><b>No. of Frets:</b> 20 <br><b>Unique Features:</b> 4-Ply Tortoise Shell Pickguard, Hellcat Position Inlays, Double Skull Inlays at 12th Fret, Tim Armstrong, Signature on Truss Rod Cover, <br><b>Strings:</b> D'Addario EXP-26 Phosphor Bronze, Gauges .011 to .052 <br><b>Accessories:</b> Tim Armstrong "Let's Go" Guitar Strap</TD> <TD ALIGN="RIGHT">£259.99</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G2_129.99" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G2- Fnd GDO-200.jpg"><b>Model Name:</b> GDO-200<br> <b>Colours:</b> Green, Blue, Maroon<br> <b>Body:</b> Maple<br> <b>Neck:</b> Mahogany<br> <b>Fingerboard:</b> Rosewood<br> <b>No. of Frets:</b> 20<br> <b>Unique Features:</b> Fender "F" Inlay at 12th Fret, Side Dot Position Inlays, Ivory Body and Neck Binding Strings: D'Addario EXP-26 Phosphor Bronze, Gauges .011 to .052<br> <b>Accessories:</b> None</TD><TD ALIGN="RIGHT">£129.99</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G3_2099" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G3- Gib Hummingbird.jpg"><b>Model Name:</b> Hummingbird<br> <b>Colours:</b> Heritage Cherry Sunburst<br> <b>Body: </b>Solid Mahogany<br> <b>Neck:</b> Maple<br> <b>Fingerboard: </b>Rosewood<br> <b>No. of Frets:</b> 20<br> <b>Unique Features:</b> 4-Ply Tortoise Shell Pickguard with floral and hummingbird inlay, Nickel Grover rotomatic tuners<br> <b>Strings:</b> Gibson J200 Phosphor Bronze Acoustic Guitar Strings Gauges .011 to .052<br> <b>Accessories:</b> L.R. Baggs Element Active Acoustic Pickup System</TD><TD ALIGN="RIGHT">£2,099</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G4_399" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G4- Gib 1942 J-45 Legend.jpg"><b>Model Name:</b> 1942 J-45 Legend<br> <b>Colours:</b> Vintage Sunburst<br> <b>Body: </b>Solid Mahogany<br> <b>Neck:</b> Maple<br> <b>Fingerboard: </b>Rosewood<br> <b>No. of Frets: </b>20<br> <b>Unique Features:</b> Hand-coloured tortoise "tear drop" pick guard<br> <b>Strings:</b> Gibson J200 Phosphor Bronze Acoustic Guitar Strings Gauges .011 to .052<br> <b>Accessories:</b> None</TD><TD ALIGN="RIGHT">£399</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G5_599.99" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G5- Ep AJ-500R.jpg"><b>Model Name:</b> AJ-500R<br> <b>Colours:</b> Natural Satin<br> <b>Body:</b> Solid Mahogany<br> <b>Neck:</b> Maple<br> <b>Fingerboard:</b> Rosewood<br> <b>No. of Frets:</b> 20<br> <b>Unique Features:</b> Mother-of-Pearl "stickpin" inlay, Offset Notch peghead<br> <b>Strings: </b> Gibson J200 Phosphor Bronze Acoustic Guitar Strings Gauges .011 to .052<br> <b>Accessories:</b> None</TD> <TD ALIGN="RIGHT">£599.99</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G6_199.99" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G6- Ep EL00.jpg"><b>Model Name:</b> EL-00<br> <b>Colours:</b> Black Vintage Sunburst<br> <b>Body:</b> Solid Mahogany<br> <b>Neck:</b> Maple<br> <b>Fingerboard:</b>Rosewood<br> <b>No. of Frets:</b> 20<br> <b>Unique Features:</b> 10 Year Guarantee<br> <b>Strings:</b> Gibson J200 Phosphor Bronze Acoustic Guitar Strings Gauges .011 to .052<br> <b>Accessories:</b>None</TD><TD ALIGN="RIGHT">£199.99</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G7_1200" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G7- Tay Jason Mraz Signature.jpg"><b>Model Name:</b> Jason Mraz Signature</br> <b>Colours:</b> Natural<br> <b>Body:</b> Red Cedar and Indian Rosewood<br> <b>Neck: </b>Tropical American Mahogany<br> <b>Fingerboard: </b>Rosewood<br> <b>No. of Frets: </b>20<br> <b>Unique Features: </b>Zodiac Koa Cypress rossette, ebony fretboard,<br> <b>Strings:</b>D'Addario Pro-Arte Extra Hard Tension, Gauges .011 to .052<br> <b>Accessories:</b>Taylor Deluxe Hardshell Case</TD> <TD ALIGN="RIGHT">£1,200</TD></TR> <TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_G8_1399" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD> <TD><img width=100 height=200 hspace=1 vspace=1 align= "left" src= "products/G8- Tay 315ce.jpg"><b>Model Name:</b> Taylor 315ce<BR> <b>Colours:</b> Natural<BR> <b>Body:</b> Sitka Spruce<br> <b>Neck:</b> Tropical American Mahogany<br> <b>Fingerboard:</b> Rosewood<br> <b>No. of Frets:</b> 20<br> <b>Unique Features:</b> Adjustable Truss Rod<br> <b>Strings:</b> Elixir Medium Gauge Strings with Nanoweb Coating, Gauges .011 to .052 <br> <b>Accessories:</b> Taylor Hardshell Case</TD> <TD ALIGN="RIGHT">£1,399</TD></TR> <TR><td><INPUT TYPE=RESET VALUE="Empty Basket"></td> <TD><b>TOTAL</b></TD> <TD ALIGN="RIGHT"><INPUT TYPE=TEXT NAME="TOTAL" SIZE=10 onFocus="this.form.elements[0].focus()"></TD> </table> <table title="Buy Now" id="Buy Now" border="1" table width="100%" border=0 cellpadding=0 cellspacing=0 bgcolor="#777777"> <tr> <a name="Checkout"></a> <TD COLSPAN=3 ALIGN="CENTER"><h3>Checkout</h3></TD></TR> <tr><td colspan=2><strong>Please fill your details in the form below:</strong></td></tr> <tr><td>Department:</td><td><select name="sendto"> <option value="k0602276@kingston.ac.uk">AcuSound Sales</option></td></tr> <tr><td><font color=red>*</font> Name:</td><td><input size=25 name="Name"></td></tr> <tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td></tr> <tr><td>Phone:</td><td><input size=25 name="Phone"></td></tr> <tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr> <tr><td> Address:</td> <td><textarea name="Address" rows=5 cols=35></textarea></td></tr> <tr><td>Total:</td><td><input size=25 name="###"></td></tr> <tr><td colspan=2 align=center><input type=submit name="send" value="Buy Now"></td></tr> <tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr> </table> </Form> </body> Hi, I've devised a javascript to guess what a user is typing in. That all works, but I'm stuck on how to take that value the user has submitted and put it back into the form. At the moment my form is: <form method="post" action="/bin/openathenssp_isapi.dll?type=ukfed&dir=req&requestURL=index.php" onsubmit="send_sso('login_form', 'loading', '<?=SITE_URL?>'); return false;"> <input type='text' name='uni_name' id='uni_name' value='' autocomplete="off"> <input type="hidden" name="entityID" id="entityID" /> //Where the name is guessed <script>actb(document.getElementById("uni_name"),customarray);</script> <input type='submit' name='submit' value='submit'> </form> So basically I'm trying to put uni_name into the entityID. Here's my javascript: function send_sso(ajax_div, loading_div, site_url) { replace_this_div = ajax_div; replace_loading_div = loading_div; var entityID = document.getElementById("entityID").value; if (uni_name == "Bournemouth University"){ document.getElementById('entityID').value = "https://idp.bournemouth.ac.uk/shibboleth"; ...MORE INSTITUTIONS GO HERE... } else { alert("Sorry, the institution '" +uni_name+ "' was not recognised or does not have access to this service."); } } But this does not seem to be working - any ideas? |