JavaScript - Javascript Variable Iteration
I am working on a commenting system and I was able to get it working by using 5 separate scripts, which are exactly the same, except for having slightly different variables. I had to repeat (copy & paste) the js function 5 times in the html <head>, and only allow for 5 posts per page. Which works fine. But, I thought maybe I could replace it with a single function that includes an iteration counter that changes the variables automatically?
Here's what I have now (below), if you think this can be easily turned into 1 function with iteration counter, please let me know how, as I am new to js. I already have the iteration working on the php end, but, I just couldn't figure out how to do it for the javascript. Code: <script type="text/javascript"> $(function() { $(".submit1").click(function() { var post_id = $("#post_id1").val(); var member_id = $("#member_id").val(); var comment = $("#comment1").val(); var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; if(comment=='') { alert('Please enter a valid comment 1'); } else { $("#flash1").show(); $("#flash1").fadeIn(2400).html('<span class="loading">Loading Comment...</span>'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function(html){ $("li#update1").append(html); $("li#update1 li:last").fadeIn("3600"); $("#flash1").hide(); document.getElementById('post_id1').value=''; document.getElementById('member_id').value=''; document.getElementById('comment1').value=''; $("#comment1").focus(); $("#flash1").hide(); } }); } return false; }); }); </script> <script type="text/javascript"> $(function() { $(".submit2").click(function() { var post_id = $("#post_id2").val(); var member_id = $("#member_id").val(); var comment = $("#comment2").val(); var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; if(comment=='') { alert('Please enter a valid comment 2'); } else { $("#flash2").show(); $("#flash2").fadeIn(2400).html('<span class="loading">Loading Comment...</span>'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function(html){ $("li#update2").append(html); $("li#update2 li:last").fadeIn("3600"); $("#flash2").hide(); document.getElementById('post_id2').value=''; document.getElementById('member_id').value=''; document.getElementById('comment2').value=''; $("#comment2").focus(); $("#flash2").hide(); } }); } return false; }); }); </script> <script type="text/javascript"> $(function() { $(".submit3").click(function() { var post_id = $("#post_id3").val(); var member_id = $("#member_id").val(); var comment = $("#comment3").val(); var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; if(comment=='') { alert('Please enter a valid comment 3'); } else { $("#flash3").show(); $("#flash3").fadeIn(2400).html('<span class="loading">Loading Comment...</span>'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function(html){ $("li#update3").append(html); $("li#update3 li:last").fadeIn("3600"); $("#flash3").hide(); document.getElementById('post_id3').value=''; document.getElementById('member_id').value=''; document.getElementById('comment3').value=''; $("#comment3").focus(); $("#flash3").hide(); } }); } return false; }); }); </script> <script type="text/javascript"> $(function() { $(".submit4").click(function() { var post_id = $("#post_id4").val(); var member_id = $("#member_id").val(); var comment = $("#comment4").val(); var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; if(comment=='') { alert('Please enter a valid comment 4'); } else { $("#flash4").show(); $("#flash4").fadeIn(2400).html('<span class="loading">Loading Comment...</span>'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function(html){ $("li#update4").append(html); $("li#update4 li:last").fadeIn("3600"); $("#flash3").hide(); document.getElementById('post_id4').value=''; document.getElementById('member_id').value=''; document.getElementById('comment4').value=''; $("#comment4").focus(); $("#flash4").hide(); } }); } return false; }); }); </script> <script type="text/javascript"> $(function() { $(".submit5").click(function() { var post_id = $("#post_id5").val(); var member_id = $("#member_id").val(); var comment = $("#comment5").val(); var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; if(comment=='') { alert('Please enter a valid comment 5'); } else { $("#flash5").show(); $("#flash5").fadeIn(2400).html('<span class="loading">Loading Comment...</span>'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function(html){ $("li#update5").append(html); $("li#update5 li:last").fadeIn("3600"); $("#flash5").hide(); document.getElementById('post_id5').value=''; document.getElementById('member_id').value=''; document.getElementById('comment5').value=''; $("#comment5").focus(); $("#flash5").hide(); } }); } return false; }); }); </script> Similar TutorialsHi! 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. maybe i've been spoiled by php's associative arrays which can be both sorted/iterated in a reliable, consistent order AND accessed via indexed string keys...but is there something like this in javascript? it seems any way you slice it (pardon the pun), you have to write an object that maintains either keys for you or element order. i suppose it makes more sense to maintain keys since there are a lot of built-in functions for working with arrays. any thoughts? thanks, Leon Hello I have a piece of javascrip that refreshes the page: ---------- Code: <script type="text/javascript"> <!-- Begin function reFresh() { location.reload(true) } /* Set the number below to the amount of delay, in milliseconds, you want between page reloads: 1 minute = 60000 milliseconds. 2 minutes = 120000 milliseconds 5 minutes = 300000 milliseconds*/ window.setInterval("reFresh()",20000); // End --> </script> -------- I am new to javascript and wanted to use an html options menu in order to be able to choose the refresh interval: Code: <SELECT NAME="refreshtime"> <OPTION VALUE="60000">1 min</option> <OPTION VALUE="120000">2 min</option> <OPTION VALUE="180000">3 min</option> <OPTION VALUE="240000">4 min</option> </SELECT> I know that I have to pass a variable to the function. I have tried several variations but none have worked. I have mostly received a "done with errors" warning. Any help would be appreciated. Thanks Zam Can anyone tell me of how to take or assign a javascript variable into php.
SOLVED I am trying to use a php variable in javascript. At the moment I am using a fixed variable PHP Code: $articleID = 5; Then outputting it to the page to declare a javascript variable Code: var articleid = "<?php echo $articleID; ?>"; Then trying to us it within a string "FlashVars", "fileURL=../uploadedfiles/+ articleid + .gpx&key=ABQIAAAAOQBC......... Can anyone tell me what I am doing wrong? I am not very used to Javascript and perhaps I am going about it totally the wrong way. Help! Gordon Is it possible to pass the value of a javascript variable through src? For example, instead of... <script type="text/javascript" src="/myfolder/myjsfile.js"></script> ...could I have something like... <script type="text/javascript" src=JS VARIABLE VALUE></script> What would be the syntax for this? Is it even possible? Thank you! I get the screen resolution with this script: <SCRIPT language="JavaScript"> <!-- height = screen.height; width = screen.width; document.write(width + "x" + height); var x = ( width + "x" + height); //--> </SCRIPT> Now I have some links below and I want to use bold if the link has the same screen resolution: <a href="page1">800x600</a> <a href="page2">1024x768</a> <a href="page3">1680x1050</a> <a href="page4">1920x1200</a> Supposing my screen resolution is 1920x1200, the result should be: 800x600 1024x768 1680x1050 1920x1200 How do I do that? Thx!! Hi, is it cool/possible to use a php variable inside the javascript ? i have this hidden html form field PHP Code: <?php $unique = md5(uniqid()); ?> <input type="hidden" name="unique" id="<?php echo $unique; ?>" value="" /> then i have this javascript PHP Code: var hiddenfield = document.getElementById('<?php echo $unique; ?>'); hiddenfield.value = new Date().getTime(); LOL it doesn't work, can somebody help me please ? Hello Can i pass php Variable into javascript function like this example. PHP Code: <script type="text/javascript"> <!-- function confirmation() { var answer = confirm("are u sure?") if (answer){ window.location = "user.php?action=statusd&uid=".$row['id'].""; } else{ alert("Canceled") } } //--> </script> This ".$row['id']." will be an user id ... link PHP Code: <a href='#' onclick='confirmation(); return false;'>Go</font></a> Hi I want to use the javascript variable value in html , for example <script > var alignment = "Right"; </script> <html> <p align = alignment > Hi </p> </html> How can i do like this ! Hi, I am trying to write some code (not my main function in my job) to have a variable be inserted in the middle of a "<a href>" tag. I created an XML file and I am parsing it with XSL and that is working well. In my XSL code I have the following (and it is working): Code: <xsl:for-each select="documents/data[frequency='Daily']"> <script>var meetingIndicator = <xsl:value-of select="meetingIndicator"/>; </script> <tr> <td class="tableLink"><a href="" onMouseover="showmenu(event,linkset[0], '450px')" onMouseout="delayhidemenu()"><xsl:value-of select="meetingCategory"/></a><script>document.write(meetingIndicator);</script> </td> <td><xsl:value-of select="meetingCategory"/></td> <td> <xsl:value-of select="duration"/> </td> <td> <xsl:value-of select="purpose"/> </td> <td> <xsl:value-of select="idealFor"/> </td> <td> <xsl:value-of select="instructions"/> </td> <td class="tableLink"> <a><xsl:attribute name="href"> <xsl:value-of select="documentURL"/></xsl:attribute> <xsl:value-of select="supportingDocuments"/></a> </td> </tr> </xsl:for-each> </table> In this part of the code: Code: <td class="tableLink"><a href="" onMouseover="showmenu(event,linkset[0], '450px')" onMouseout="delayhidemenu()"><xsl:value-of select="meetingCategory"/></a><script>document.write(meetingIndicator);</script> I want to replace the 0 in (event,linkset[0], '450px') with the "meetingIndicator" variable (or even the "<xsl:value-of select="meetingIndicator"/>;") but I don't know how to. I can call the "<xsl:value-of select="meetingIndicator"/>;" and it displays the correct value from the XML file. i just need to know how to place that in the "event,linkset[0]" part of the "a href" tag. I guess it is going to be pretty easy but I have been googling and playing it since yesterday and i am not having any luck. Thank you. I've been having a hard time with this and was hoping someone here would take a little pity and render me a great service. I'm using a javascript to get the user's timezone and am trying to pass the response to an input field in a php form. With a great deal of trial and error I got a demo working to a point but I'm stuck. Here's what works now: Code: <p id="timezoneInformation" style="text-align: center;">click here</p> <script src="jstz.js"></script> <script> var container = document.getElementById("timezoneInformation"); var showTimezone = function () {container.textContent = jstz.determine().name();}; container.addEventListener("click", showTimezone); </script> jstz.js is from Automatic timezone detection using JavaScript The example above is as far as I've gotten. I would like to send the results to a form field to submit to the server. It's probably a lot easier than I'm making it out, but I've had little success and would welcome any help. Thank you. Hi, I am making a small software which will require activation. My problem is when I activate my software after checking the key through my website I want to enter some information in registry of the user. Now my problem is when I check the value through my website how can I return after verification to the local computer. is it possible if I create a session variable or some other type of variable (not sure what type) and then read that variable on user computer. If that matches I will enter the code in registry. I have used php with forms but can I do easy form to java vars? I saw one snippit of code that did it when I was newer but did not understand it. what is the way you usually do this? Hi friends i have this var infobox = "<div style='font-size:12px;font-weight:bold; background-color:#E6E6E6;height:100px;width:100%;;filter: alpha(opacity=100)'>" + name + "<br>" + Address + "<br><br> <a href ='wfmprintservice.aspx?CustNumber='"+ Cust_number +">View More Details</a></div>"; i am able to redirect by clicking on the ling but the variable Cust_number is not being passed...Can someone please check if my h ref is being properly set... Thank you So, I need to use a javascript variable when reading a value from a asp array. Currently I have a windows app that has a webbrowser. Teh windows app invokes a javascript method through the webbrowser and this javascript method returns an object. This object is built in the javascript function using values which exist inside a classic asp array. Phew! The function that sits on the relevant asp page, is invoked from my windows app and then returns the object to the windows app is below. function item(count) { var pid; var description; var fullprice; var discountprice; var quantity; var size; this.pid = '<%=SBagArray(0,count) %>'; //ProductId this.description = "<%=SBagArray(1,count)%>"; //description this.fullprice = <%=SBagArray(8,count) %>; //Fullprice this.discountprice = <%=SBagArray(9,count) %>; //Discountprice this.quantity = <%=SBagArray(6,count) %>; //Quantity this.size = <%=SBagArray(2,count) %>; //Size this.getPid = function() { return this.pid; } this.getDescription = function() { return this.description; } this.getFullprice = function() { return this.fullprice; } this.getDiscountprice = function() { return this.discountprice; } this.getQuantity = function() { return this.quantity; } this.getSize = function() { return this.size; } } My problem is that the variable 'count' is not recognised by the asp. So, how can I use this variable in such a way that it works? This is driving me crazy! Any help would be greatly appreciated! Thanks! Gonna be asking a few Q's about a quiz javascript i'm making. I'm pretty new to JS. I have this, a select box, in the body. Code: <div align="center"><center><p><strong>Your response:</strong> <select name="theresponse" size="1" onChange="responses()"> <option value="-----">----</option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> </select> Then the relevant bit of script lower down Code: var loop=0 function responses(){ loop++ var temp2=document.instantquiz.theresponse var temp3=temp2.options[temp2.selectedIndex].text if (temp3!=solution[whichone]&&temp2.selectedIndex!=0) document.instantquiz.thesolution.value="Sorry, the correct answer is "+solution[whichone]; else if(temp2.selectedIndex!=0) document.instantquiz.thesolution.value=compliments[whichone]; } I want to add 1 to 'loop' (so that people can't score twice etc.). The problem I have here is that loop just doesn't want to go up. I've tried all sorts of things like doing it under a new variable, and even putting a new function up, all in vain. Why doesn't it work? =s Hi, new to the forum (and Javascript coding). I hope someone can help as this is driving me nuts... I want my website to display the content of another webpage in an iframe. the other webpage extension will change each day (e.g. 'www.example.com/16_02_2012.html' would be today's file to view, 'www.example.com/15_02_2012.html' would have been yesterday's etc.). I have worked out some code to grab today's date and create the web address as a variable. how do i then use that variable in the <iframe src="??"> when someone click a link to "update the iframe"? Here is my code so far... I would really appreciate some help!!! :s <html> <script> function getMonth() { var now = new Date(); var month = now.getMonth(); return month < 10 ? "0" + (month+1) : month+1; } function getDay() { var now = new Date(); var day = now.getDate(); return day < 10 ? "0" + (day) : day; } var month = getMonth(); var day = getDay(); var year=new Date(); var address = "http://www.example.com/" + day + "_" + month + "_" + year.getFullYear() + ".html"; function iFrameWrite(id, url){ document.getElementById(id).src = url; } </script> <iframe id="info" width="800px" height="800px"></iframe><br /> <a href="#" onclick="javascript:iFrameWrite('info', 'address')">Update the Iframe</a> </html> |