JavaScript - Getting A Variable From A Form To Javascript
so i have this code i am working on(this is a dumbed down version of it)
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("#Submit").click(function(){ for (i=0;i<=50;i++) { $.ajax({ url:"process.php", type:"get", data:$("form").serialize(), success:function(response){ var obj = jQuery.parseJSON( response ); var success = obj.success; var actionsNumber = obj.number; $("#result").html('There have been '+actionsNumber+' loops completed'); } }) } }) }) </script> </head> <body> <form action="" method="post"> <p> <label><strong>Number of Loops</label> </p> <input name="count" type="text" value = "1"/> </p> <p> <input id="Submit" type = "button" value = "Send"> </p> </form> <p id="result">There have been 0 loops completed</p> </body></html> so you see this for loop for (i=0;i<=50;i++) I want it to be able to be like for (i=0;i<=count;i++) so that it will loop the number of times inputted also(though i dont know if this is something that i need to use js for) On my full code there are other textboxes that need to be sent to process.php each time that it is run. How can i achieve this? also before anyone asks i have googled this and searched around yet i havent been able to figure this out, everything i have tried hasnt worked and here is the process.php PHP Code: <?php session_start(); // process the form data here //::::::::: // if(!isset($_SESSION['number'])){ $_SESSION['number'] = 0; } $number = $_SESSION['number']++; // output json response echo'{"success":"true","number":"'.$number.'"}'; ?> 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. 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 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? 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 trying to pass a javascript variable into URL ..but not getting the value.. var newID = 2; row.insertCell(6).innerHTML = "<a href=Update_Gate.cfm?GateIdVar=newID>Edit Gate</a>"; i am getting "newID" in Update_Gate.cfm page ... i want to pass this javascript variable in url and read it on the other page as form field value any help !! Hi all, I am new to JS and usually learn by studying code posted online and modify it to have it do what I need. Recently, I used a totaling plugin for an online ordering form which does the below: item1 qty(user input text) * preset price = total price item2 qty(user input text) * preset price = total price item3 qty(user input text) * preset price = total price -------------------------------------------------------- grand total -------------------------------------------------------- The JS code which passes the grandTotal variable is as below: Code: function ($this){ // sum the total of the $("[id^=total_item]") selector var sum = $this.sum(); $("#grandTotal").text( // round the results to 2 digits "₹" + sum.toFixed(2) ); } The problem I have is the above grandTotal displays the final value when put into a table like below: Code: <td align="right" id="grandTotal"></td> But I am unable to make it work by passing it into a variable within the form fields. I would like to do something like this: Code: <input type="text" name="grandTotal" id="grandTotal" readonly="readonly" /> Can somebody please help me fix this? Any help will be greatly appreciated. Thank you. 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 ? Hi, I have a small script that opens a small popup in which to populate a form. What I want to do is include a variable so that a particular PHP page opens. I include where I am at present: HTML page calling JS popup: <input type="text" name="DOCUMENTS" id="DOCUMENTS" onclick="makeSelection(this.form, 'DOCUMENTS');"> JS Code: targetElement = null; function makeSelection(myform, id) { if(!myform || !id) return; targetElement = myform.elements[id]; var handle = window.open("../file_upload.php?dir=CODE", "mywindow","menubar=0,resizable=0,width=400,height=250"); } What I want to be able to do is get it so that the bit of JS that has 'dir=CODE' has a variable called from the HTML page. Is this possible, and if so, could anyone please help me out? Regards, Neil Hi, in PHP I can assign a variable do... PHP Code: function setvar($anyvar,$val){ $$anyvar=$val; } setval('myvar',$whatever); how to do in javascript ... I thied window.anyvar= and this[anyvar] and several other combinations but no joy Edit: fixed, my bad, was being a muppet, was calling setvar(varname,val) instead of setvar('varname',val); i am trying to pass the a variable "oneone" from a select box to a php page via java script. this is my html Code: <div id="wrapper"> <div id="content"> <form> Select a Customer: <select id="oneone" name="oneone" value=""> <option value="s_last">last</option> <option value="s_first ">first</option> <option value="c_city">cirty</option> </select> </form> <div> <form method="get" action="" class="asholder"> <small style="float:right">Hidden ID Field: <input type="text" id="testid" value="" style="font-size: 10px; width: 20px;" disabled="disabled" /></small> <label for="testinput">Person</label> <input style="width: 200px" type="text" id="testinput" value="" /> <input type="submit" value="submit" /> </form> </div> </div> this is my javascript Code: <script type="text/javascript"> var options = { script:"test.php?json=true&limit=6&", varname:"input", json:true, shownoresults:false, maxresults:6, callback: function (obj) { document.getElementById('testid').value = obj.id; } }; var as_json = new bsn.AutoSuggest('testinput', options); var optionsa = { script:"test.php?", varname:"one", json:false }; var as_jsona = new bsn.AutoSuggest('oneone', optionsa); </script> and the little part of my php Code: $input =$_GET['input']; $q =$_GET["one"]; can someone PLEASE tell my why i cant get this to work!!! 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> 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. Hi: I got a blank page returned when using javascript within the Frameset. Here is part of my codes: (my javascript part) <script language="JavaScript"> //this function will be used to get the parameter from URL using javascript function gup (name) { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; } //get the transactionID from the URL var transactionID = gup('name'); //add the transactionID as the URL parameter var url = "../../onetwothree/Approve.jsp?name=" + transactionID + '\"'; var temp = '<frame name=\"onetwothree\" ' + 'src=\"' + url + '>' + '</frameset>'; </script> ---------------------------- (my html part) <frameset rows="0,*" border="0" frameborder="0" framespacing="0"> <frame name="onetwothreeFrame" src="../onetwothree/abcd.html" marginwidth="0" marginheight="0" scrolling="no" noresize> <script language="JavaScript"> document.write(temp); </script> </frameset> 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! Hi, I'm having one javascript function which will return the variable and I need to pass that variable to command button action.Please find the below code and let me know how I can achieve this. <script> function addEntry(entries) { var uploadedEntry = entries[0].entryId; alert("Uploaded Entry Details::::" + uploadedEntry ); } </script> <h:commandButton tabindex="1" image="../img/submit.gif" action="#{portfolioListing.uploadMediaEntry(uploadedEntry)}" Thanks, Anil 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 |