JavaScript - Adding If Statement To Variable Message.
Hello All,
I am a beginner javascript student and am hoping to ask for some help with a problem i am having on an assignment. I have been assigned the old Pizza Form tutorial. My form asks for customer info, select size with radio button, and select toppings with checkboxes. I created a ValidateForm() function to check if these are filled in and output a message for each field saying "please fill in field." My problem now is that on Submit I need to print out a message with customer info, selected size, and selected toppings. I have the customer info done with a var message: var message = "Name: " + name + "\n"; message += "Address: " + address + "\n"; message += "City: " + city + "\n"; message += "State: " + state + "\n"; message += "Zip: " + zip + "\n"; message += "Phone: " + phone + "\n"; message += "Email: " + email + "\n"; I know I need to add IF statements to this for the sizes and toppings but do now know where to put them. here is my whole script if that helps: function validate_form() { valid = true; if (document.PizzaForm.customer.value == "" ) { alert ( "Please fill in the 'Name' box." ); valid = true; } if (document.PizzaForm.address.value == "" ) { alert ( "Please fill in the 'Address' box." ); valid = false; } if (document.PizzaForm.city.value == "" ) { alert ( "Please fill in the 'City' box." ); valid = false; } if (document.PizzaForm.state.value == "" ) { alert ( "Please fill in the 'State' box." ); valid = false; } if (document.PizzaForm.zip.value == "" ) { alert ( "Please fill in the 'Zip' box." ); valid = false; } if (document.PizzaForm.phone.value == "" ) { alert ( "Please fill in the 'Phone' box." ); valid = false; } if (document.PizzaForm.email.value == "" ) { alert ( "Please fill in the 'Email' box." ); valid = false; } if ( ( document.PizzaForm.sizes[0].checked == false ) && ( document.PizzaForm.sizes[1].checked == false ) && ( document.PizzaForm.sizes[2].checked == false ) && ( document.PizzaForm.sizes[3].checked == false ) ) { alert ( "Please choose your Size" ); valid = false; } if ( ( document.PizzaForm.toppings[0].checked == false ) && ( document.PizzaForm.toppings[1].checked == false ) && ( document.PizzaForm.toppings[2].checked == false ) && ( document.PizzaForm.toppings[3].checked == false ) && ( document.PizzaForm.toppings[4].checked == false ) && ( document.PizzaForm.toppings[5].checked == false ) && ( document.PizzaForm.toppings[6].checked == false ) && ( document.PizzaForm.toppings[7].checked == false ) ) { alert ( "Please choose your Toppings" ); valid = false; } return valid; } function orderDetails() { var name = document.PizzaForm.customer.value; var address = document.PizzaForm.address.value; var city = document.PizzaForm.city.value; var state = document.PizzaForm.state.value; var zip = document.PizzaForm.zip.value; var phone = document.PizzaForm.phone.value; var email = document.PizzaForm.email.value; var message = "Name: " + name + "\n"; message += "Address: " + address + "\n"; message += "City: " + city + "\n"; message += "State: " + state + "\n"; message += "Zip: " + zip + "\n"; message += "Phone: " + phone + "\n"; message += "Email: " + email + "\n"; alert(message) return; } function doSubmit() { if (validate_form() == true) { orderDetails(); } } Thank you! Similar TutorialsHi, I am a newbie in JS, and have already been stuck with this problem for the whole day. Basically I want to display a var returned from a function with <fmt:message/> tag. It sounds easy, but somehow I just can't get it to be display correctly (Tried with alert() and it worked). Here is my code: ------------------------------------------------------------------------------------------- <script> function ReturnTime() { var dateobj=new Date(); if (dateobj.getSeconds()%30==0) { return dateobj.getSeconds(); } else { return dateobj.getSeconds(); } } time1 = ShowTime(); alert(time1); </script> <fmt:message key='content.currentTime'/>: <cut value="${time1}"/> ------------------------------------------------------------------------------------------------------------------------- I might have done some stupid things here. But I am very new to JS (2 days of experience so far). So please be patient. Your kind help will be very much appreciated. Regards, Robert Here's my script wher problem occurs: Code: $.post('do/register.php?username='+('#username').value, function(result) { if(result == 1) { $('#message').html("Everything's ok"); } else if(result == 2) { $('#message').html("Username is missing."); } else { $('#message').html("What the hell is wrong here?!"); } }); If i put alert(result); in function(result){ } part, i get "result" value that is '1' and that is correct value but it seems like IF statement is not recognizing it at all... #message div constantly shows me "What the hell is wrong here?!" message... Anyone know what could cause this problem? Thanks in advance, phpStud. I am trying to get the variable extraCharge to increment by $1 for every topping over the 3 included toppings. How can this be done? For statements confuse me 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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Checkbox Toppings</title> <script type="text/javascript"> function validate(form) { var toppingsArray = document.form1.scripts.length; var extraCharge = 0; // Checking if at least one period button is selected. Or not. if (!document.form1.orderType[0].checked && !document.form1.orderType[1].checked) { alert("Please Select orderType"); return false; } var total="" for(var i=0; i < document.form1.scripts.length; i++) { if(extraCharge >= 3) alert("You can only select 2"); else if(document.form1.scripts[i].checked) total +=document.form1.scripts[i].value + "\n" } for(var i=3; i >= document.form1.scripts.length; i++) { var extraCharge = extraCharge + 1; } if(total=="") alert("select Toppings") else document.form2.checklist.value=(total); document.form2.toppingCharge.value=(extraCharge); return false; } </script> </head> <body> <h2>3 Toppings included, $1 for each additional topping</h2> <table width="333" border="0"> <tr> <td width="179"><form name="form1" method="post" action="" onchange="return validate(this)"> Select Order Type First:<br /> <input name="orderType" value="delivery" type="radio">delivery </font> <br /> <input name="orderType" value="takeOut" type="radio"><font face="verdana" size="2">takeOut</order></b> <br /> <br /> <br /> <input name="scripts" value="extracheese" type="checkbox">Extra Cheese <br /> <input name="scripts" value="sausage" type="checkbox">Sausage <br /> <input name="scripts" value="peperoni" type="checkbox">Peperoni </font> <br /> <input name="scripts" value="bacon" type="checkbox">Bacon <br /> <input name="scripts" value="canadianBacon" type="checkbox">Canadian Bacon <br /> <input name="scripts" value="mushroom" type="checkbox">Mushroom </font> <br /> <input name="scripts" value="pineapple" type="checkbox">Pineapple </font> <br /> <input name="scripts" value="onion" type="checkbox">Onion <br /> <input name="scripts" value="olive" type="checkbox">Olive <br /> <input name="scripts" value="greenPepper" type="checkbox">Green Pepper </font> </form> </td> <td width="144">Selected Toppings: <br /> <form name="form2"><textarea name="checklist" cols="20" rows="15" value="total"></textarea> Extra Topping Charge: <input name="toppingCharge" type="text" /> </form></td> </tr> </table> </body> </html> Hello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! Hi, I have a programing problem that have been around for ages. I have search on google using several expressions and words and after hours of digging i'm still unable to do it. I would like to get a value from a HTML page hosted remotely with an inconstant value. Then define this value and name as a javascript variable and apply or show in my page. Thanks for all the help P.S. Is there any way to make a domain lookup in javascript? I mean a user enters a domain and the script converts to an ip and shows to the user. If not thanks, probably it can only be done in the server side... I don't know how I should do ? Quote: <html> <head> <script type="text/javascript"> function add(a,b){ y=a+b return y } var aaa = add(one,two) //one needs to get somehow get the value of yil, in this case 10 var one = function reas(){i=10;if(i<10){yil = 15}; else{yil = 10; yil = one;}; var two = 20; document.write(y) </script> </head> </html> also why doesn't this work? Quote: <html> <head> <script type="text/javascript"> function adder(a,b){ var k=a+b; return k } var hes=adder(5,kol); kol=40; alert(hes); </script> </head> </html> you cannot have variables in callback functions? Hello I have a php page in which I declared a js variable... right at the top of the page... <script type="text/javascript"> var tester = 0; </script> Later in the page I test this variable... <script type="text/javascript"> if (tester==1){ do some stuff!! } </script> But I keep getting an error variable _tester undefined Am I missing something obvious... I am new to js... but this seems really straightforward Hi! I have a javascript in the head of the document which has a variable named "ref2" ... ref2 is already working as I can see its value working in another function. I need to send this variable as the value of a hidden field in the form which is in the body of the document. This is my JavaScript Code: Code: function WriteContactFormStatement1 () { var ContactFormValue = ref2; document.write('<input type="hidden" name="UReferrersName" value="' + ContactFormValue + '" />'); } var WriteContactFormStatement = WriteContactFormStatement1 (); And at the end of my form, before the submit button, I have the following code: Code: <!-- START -- Javascript to print the statement for UReferrersName --> <script language="JavaScript" type="text/JavaScript"> //WriteContactFormStatement(); document.write (WriteContactFormStatement); </script> <!-- End -- Javascript to print the statement for UReferrersName --> When I execute the form, it doesn't work the way it should, plus, gives me a word "undefined" next to the "Submit" button ..... Please help !... - Xeirus. i have multiple customers in my company and i want to send an SMS to the mobile (cell phone) of the customer that he must pay the renewals fee's durng 2 weeks.. so i send to him an e-mail and at the same time i send for him an SMS message to his phone to ensure that he will read the message that he must pay the fee's. i dont know if there is a module that can make this idea or i must buy a product or register into a website to make this module... thnx alot I was wondering if there was an easy way to configure a message box to resemble that of a login form. It would preferably have two text fields for username/password and OK and Cancel buttons as well. I currently have a normal login form on my website designed using PHP but it does not figure into what we need to do now. I'm not sure about going about this and I have tried looking for quite sometime for an example of this but cannot find one.
Hello all, I'm trying to write a simple script in JS that will show a message for each day of the week. I have searched in your forums and other places but sadly I keep failing. Thank you for helping a newb. Code: <script type="JavaScript"> function showMessage() { var d = new Date(); var day = d.getDay(); // Note: Sunday=0, Monday=1, etc... switch( true ) } case ( day == 6 ) : { document.write("Today is saturday!") } case ( day == 5 ) : { document.write("Today is friday!") } case ( day == 4 ) : { document.write("Today is thursday!") } case ( day == 3 ) : { document.write("Today is wednesday!") } case ( day == 2 ) : { document.write("Today is tuesday!") } case ( day == 1 ) : { document.write("Today is monday!") } case ( day == 0 ) : { document.write("Today is sunday!") } } } </script> Hi all new guy on the block here, I've got a website, and I want to add a welcoming message which hovers on a certain part of the page which only loads for the visitor for the first time they login, and won't again(presumably cookies used). And says something like "adjust your settings here.." I don't want it to be an external popup but something that loads on the page in a certain area, defined by me (PX-pixle reference) I think i nutted something out in a bit of PHP, this is what i've got so far PHP Code: <?php if (firstLogin()){ genSpecial(***load jquerie or css etc***); } else{ genRegular(); } ?> can anyone help me figure out how to make this all work and load a box from jquerie or something similar? thanks David Hi there, I have this form that validates a few textboxes & a dropdownlist. And when it is not filled in, the border of the textboxes and dropdownlist would turn red, followed by an alert message notifying which field have not been filled in. Else, the border will revert back to black, and the form will be submitted successfully. There's a problem whereby when everything is filled in, the alert message still pops up. Any kind souls to help me? Thanks in advance. Javascript Code: function check(checkForm) { var fields = new Array("Name","Email Address", "Domain Name"); var index = new Array(),k=0; for(var i=0;i<fields.length;i++) { var isFilled = false; var c = document.getElementsByName(fields[i]); for(var j = 0; j < c.length; j++) if(!c[j].value == "") { isFilled = true; c[j].className = "defaultColor"; } else { c[j].className ="changeToRed"; } if(!isFilled) { index[k++] = fields[i]; } } if(k.length!=0) { joinComma = index.join(', '); alert('The field(s) corresponding to '+ joinComma + ' is/are not selected.'); return false; } } HTML Code: *Last Name: <input type="text" id="Text27" name="Last Name" /><br /> <br /> *Email Address: <input type="text" id="Text28" name="Email Address" /> @ <select id="Select5" name="Domain Name"> <option></option> <option>hotmail.com</option> <option>yahoo.com</option> </select> <input id="Submit5" type="submit" value="Submit" onclick="return check(checkForm)"/> Hee guys, I've got a little bit of a problem on my hand. Someone has asked me to help them on a FREE forum host. You have no access to PHP or SQL or anything like that. Just the templates, but even those are really messy! Now on the navagation bar there a PM section (obviously) but the wording they have used is very long and ugly. I was wondering if it would be possible to change the wording whilst still having it show the amount of PM's that you have. If you need anymore information just ask, but I really hope someone can help Can anyone help me with the following error message and correlating script please. I think it's just a simple omission. Any help appreciated. Thanks Microsoft VBScript compilation error '800a0400' Expected statement /shop$db.asp, line 3083 end sub ^ sub ShopWriteHeaderBox (msg) %> <div class="comment_title fontbold"><%=msg%></div> <div class="comment_text"> <div class="blog_review_container" > <% end sub sub ShopWriteFooterBox (yesbr) %> </div> </div> <% if lcase(trim(yesbr)) = "yes" then %> <br /> <% end if end sub Hello, I was wanting a message to be displayed if it is the visitors first time to the site. It will be a toolbar displayed at the top of the website, with links to FAQ's. Any method of display this is fine. Thanks im trying to display a random number, but each time a button is pressed that calls the test function, i don't want to random number to be repeated. i know i have to declare 2 variables and a while loop, but i'm stuck as to what to put in the second variable and in the while loop. any advice is appreciated var random = Math.floor(Q * Math.random()); var random2 = ?? function test () { document.write(random); while (random == random2) { document.write(random); } } javascript2.js Code: function displayMessage(msg) { // Open a new window var msgWindow = window.open('', 'Message'); // Write message in the new Window msgWindow.document.write(msg); // Raise this window, in case it's not visible msgWindow.focus(); } //Enter total number of questions: var totalquestions=13 var correctchoices=new Array("a","a","b","c","b","c","a","a","b","c","a","b","b"); var correctanswers=new Array("memory locations","_ underscore","x=5+5;","20","Else","lose win","ticker = ticker + 1;","Ticker = ticker + 1;","300.000","Jonathon \b","var","var counter;","var mark = 50, 70"); function gradeit(){ var actualchoices=new Array() var msg1=new Array() var correctanswersno=0 var t=0 var displaycorrectanswers="" for (q=1;q<=totalquestions;q++){ var thequestion=eval("document.Questionform.q"+q) for (c=0;c<thequestion.length;c++){ if (thequestion[c].checked==true) actualchoices[q]=thequestion[c].value } if (actualchoices[q]==correctchoices[q]){ msg1[t]=q+")"+" Correct"; correctanswersno = correctanswersno + 1; t = t + 1; } else { msg1[t]=q+")"+" Incorrect: <br>Correct Answer: "+correctchoices[q]+") "+correctanswers[q]; t = t + 1; } } for (p=0;p<=12;p++){ displaycorrectanswers+="<br>"+msg1[p]; } var msg="Sco "+correctanswersno+"/"+totalquestions+" <br><br>Marks: "+displaycorrectanswers; displayMessage(msg); } Basically on my index page it has a button which when clicked makes use of gradeit(). Currently this then used displayMessage(msg) which opens a new window and displays the message. However, what I want it to do is to open another created html page e.g. answer.html and then for this page to display the message. How do i do this? |