JavaScript - Using Php Variable In Javascript?
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 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 Can anyone tell me of how to take or assign a javascript variable into php.
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 ? 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!! 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. 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, 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 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 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> I'm trying to echo a php variable to my javascript function but nothing is happening Basically this php page displays bunch of records with a radio button next to each record. So when the radio button is clicked it would open that record in a new window that is clicked. Code: <input type='radio' name='option' value='$ctr' id='$ctr' onClick="edit_record('<?php echo $ctr ?>')"> <script type="text/javascript"> function edit_record(){ var value = arguements[0]; alert("record id: " + value); window.open("Edit.php", "Edit Record", "status=1, height=400, width=900, resizable=0"); } 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 ! I have this code but it doesnt work: Code: <script type="text/javascript"> var number = 10; document.getelementbyid(number).value = number; </script> <input type="text" id="number" value=""> Can anybody help me out here? 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 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 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.'"}'; ?> 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> |