JavaScript - Inserting Php Variables Using Javascript
Hi,
I am using javascript to collect hidden form elements and then send the values to a php page, without reloading or changing the page. Basically i have a list of job vacancies, with just an apply button, when a person clicks on the apply button, the form collects some information from their account and sends it using the javascript code. Now this works fine, except for if somebody then clicks on the second job it sends the information from the first form again. And it seems to remember the information. What i am trying to find out is it possible to have multiple forms on one page which once submitted send variables to javascript which then send them to a php but clear themselves so that it doesn't remember the previous values. Here is the code for the form: Code: <form id="submit" method="post"> <fieldset> <input type="hidden" name="usern" id="usern" value="php variable" /> <input type="hidden" name="joborder_id" id ="joborder_id" value="php variable" /> <input type="hidden" name="site_id" id ="site_id" value="php variable" /> <button class="button positive">Apply For This Job </button> </fieldset> </form> <div class="success" style="display: none;">Your Application has been sent.</div> </form> And the Javascript code is he Code: $(document).ready(function(){ $("form#submit").submit(function() { // we want to store the values from the form input box, then send via ajax below var usern = $('#usern').attr('value'); var joborder_id = $('#joborder_id').attr('value'); var site_id = $('#site_id').attr('value'); $.ajax({ type: "POST", url: "apply.php", data: "usern="+ usern +"& joborder_id="+ joborder_id +"& site_id="+ site_id, success: function(){ $('form#submit').hide(function(){$('div.success').fadeIn();}); } }); return false; }); }); Any ideas or suggestions would be appreciated, Thanks Lee Similar TutorialsSo I've got an order form that uses javascript to immediately calculate the price as soon as you enter the quantity that you want to order: Code: <form method="post" action="submitted.html" name="form"> <table id="order-table"> <tr> <td>Widgets</td> <td><input type="text" name="quantityWidgets" onchange="calculate()"></input></td> <td> x </td> <td>$650</td> <td>=</td> <td><input type="text" name="totalWidgets" disabled="disabled"></input></td> </tr> </table> </form> And here's the Javascript Code: function calculate(){ total = document.form.quantityWidgets.value * 650; document.form.totalWidgets.value = total; } But that's just for 1 row and I have several rows of products. I could just use a different calculate() function for each row, but I'm trying to pass arguments into calculate() AND make it display the result in the proper place, but my syntax must be wrong. Code: ... <td><input type="text" name="quantityWidgets" onchange="calculate(quantityWidgets, 650, totalWidgets)"></input></td> ... function calculate(quantity, price, total){ total = document.form.quantity.value * price; document.form.total.value = total; } Can anyone help me please? Hello. I have two javascript problems. 1). I have a java script that displays 'What's On Air Now' and 'Today's Schedule' which are located in a 'Resources' folder on our website. I have been using Namo Web Editor for several years but, after many problems, I bought WebPlus X4 from Serif Applications. It's a very good program but I'm having severe problems inserting the Javascripts mentioned above. With Namo, I just had to add: "<script type="text/javascript" src="resources/onair.js"> and the same for 'Today's Programs':"<script type="text/javascript" src="resources/schedule.js">". with WebPlus X4, I've had no luck after trying numerous times and requesting help from Serif who would not help. I would be grateful if anyone could help me, please. Thank you. PS: I have attached the text version of the scripts for you to have a look at.
The below code is not inserting the smile into the textbox. In IE8 I get an error around Code: if((sel.type == "Text" || sel.type == "None") && rng != null) In Firefox 3.5 I get an error around: Code: if (es <= 2) Each textarea has: id and name Code: <textarea class="textinput" id="about_me" name="about_me" cols="45" rows="6"> Here is the whole smile code I am using. Code: //Smile Start var myAgent = navigator.userAgent.toLowerCase(); var myVersion = parseInt(navigator.appVersion); var is_ie = ((myAgent.indexOf("msie") != -1) && (myAgent.indexOf("opera") == -1)); var is_nav = ((myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1) && (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1) && (myAgent.indexOf('webtv') ==-1) && (myAgent.indexOf('hotjava')==-1)); var is_win = ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1)); var is_mac = (myAgent.indexOf("mac")!=-1); function smile( txt ) { // document.all.txtmessage.value = document.all.txtmessage.value + txt; // return false; doInsert(" " + txt + " ", "", false,document.getElementById('txtmessage')); } function smile2( txt ) { doInsert(" " + txt + " ", "", false,document.getElementById('txttemplate')); } function doInsert(ibTag, ibClsTag, isSingle, name_txt) { var isClose = false; var obj_ta = name_txt; //---------------------------------------- // It's IE! //---------------------------------------- if ( (myVersion >= 4) && is_ie && is_win) // if ( (ua_vers >= 4) && is_ie && is_win) { if (obj_ta.isTextEdit) { obj_ta.focus(); var sel = document.selection; var rng = sel.createRange(); rng.colapse; if((sel.type == "Text" || sel.type == "None") && rng != null) { if(ibClsTag != "" && rng.text.length > 0) ibTag += rng.text + ibClsTag; else if(isSingle) isClose = true; rng.text = ibTag; } } else { //-- mod_bbcode begin // this should work with Mozillas if ( (myVersion >= 4) && is_win) { var length = obj_ta.textLength; var start = obj_ta.selectionStart; var end = obj_ta.selectionEnd; if (end == 1 || end == 2) end = length; var head = obj_ta.value.substring(0,start); var rng = obj_ta.value.substring(start, end); var tail = obj_ta.value.substring(end, length); if( start != end ){ if (ibClsTag != "" && length > 0) ibTag += rng + ibClsTag; else if (isSingle) isClose = true; rng = ibTag; obj_ta.value = head + rng + tail; start = start + rng.length; } else{ if(isSingle) isClose = true; obj_ta.value = head + ibTag + tail; start = start + ibTag.length; } obj_ta.selectionStart = start; obj_ta.selectionEnd = start; } else { //-- mod_bbcode end if(isSingle) { isClose = true; } obj_ta.value += ibTag; //-- mod_bbcode begin } //-- mod_bbcode end } } //---------------------------------------- // It's MOZZY! //---------------------------------------- else if ( obj_ta.selectionEnd ) { var ss = obj_ta.selectionStart; var st = obj_ta.scrollTop; var es = obj_ta.selectionEnd; if (es <= 2) { es = obj_ta.textLength; } var start = (obj_ta.value).substring(0, ss); var middle = (obj_ta.value).substring(ss, es); var end = (obj_ta.value).substring(es, obj_ta.textLength); //----------------------------------- // text range? //----------------------------------- if (obj_ta.selectionEnd - obj_ta.selectionStart > 0) { middle = ibTag + middle + ibClsTag; } else { middle = ibTag + middle; if (isSingle) { isClose = true; } } obj_ta.value = start + middle + end; var cpos = ss + (middle.length); obj_ta.selectionStart = cpos; obj_ta.selectionEnd = cpos; obj_ta.scrollTop = st; } //---------------------------------------- // It's CRAPPY! //---------------------------------------- else { if (isSingle) { isClose = true; } obj_ta.value += ibTag; } obj_ta.focus(); return isClose; } //Smile End Hello my first post. I have a function that insert commas via onclick or onblur. Code: function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } I also am using code from this link, where I can specify what parameters to allow the user to enter only positive/negative, decimals values and restricting characters that are not allowed. http://www.mredkj.com/tutorials/validate2.html I am having a hard time to incorporate the addCommas function into the link above. Does anyone have an example that I can look at where I can have commas added to a value dynamically while typing? e.g. if I type 1000 after that last 0 keystroke, it turns the value into 1,000. If I add 3 more zeros it turns it into 1,000,000. In the same vain if I subtract a 0 from the 1,000,000 the value is 100,000 instead. I appreciate any assistance in advance. I have the need to include an Ad Tag from a third party source that is generated via javascript which document.writes the code to display the ad. I have no control over this. I have it working on my end, BUT for the life of me I cannot get this to center in my div. It's like it busts out and aligns left after my div. I can't fathom what is going on. "IF" I include this ad tag directly in the page inside the div, it centers. But I need to dynamically pass it some data before it requests the ad, so I Have to dynamically create it, but this is the issue. It displays, just aligned left outside the target div. So, I am given a js tag with a src and that src generates a document.write that writes out an iframe. Here is my code below. I take the js src and replace some value in it since it is a string. Then I create a script element and then insert it into my div element on my page. var js_src = ("contents_of_the_src").gsub(/tie=/,'tie=' + random10_digit) var js_script = new Element('script',{'src': js_src}); Event.observe($('myDiv'), 'load', $('myDiv').insert({top : js_script})); so the view in question is something like this: <div id="myDiv"> <script language="text/javascript"> var js_src = ("contents_of_the_src").gsub(/tie=/,'tie=' + random10_digit) var js_script = new Element('script',{'src': js_src}); Event.observe($('myDiv'), 'load', $('myDiv').insert({top : js_script})); </script> </div> I've tried moving the js out of the div, afterwards, and try to insert then. I tried to create a new div, center that, and write the ad tag to it. All left alighned and outside of the div. Hi, I have the following code snippet: test.html ====== <script language="javascript" type="text/javascript"> var testVariable = "test"; </script> <script language="javascript" type="text/javascript" src="test.js"> </script> test.js ===== var testVariable = window.top.testVariable; In firefox, I'm able to access testvariable defined within test.html in test.js. But in chrome, test.js couldnot get the window.top.testVariable field defined in test.html. Can any one please let me know how i can make it work in chrome?. Am i missing something here?. All -- I have a JavaScript config file called gameSetting.js which contains a bunch of variables which configures a particular game. I also have a shared JavaScript library which uses the variables in gameSetting.js, which I include like so: <script type="text/javascript" src="gameSetting.js" ></script> <script type="text/javascript" src="gameLibrary.js" ></script> In gameSetting.js I have: $(document).ready(function() { // call some functions / classes in gameLibrary.js } in Firefox, Safari, and Chrome, this works fine. However, in IE, when it's parsing gameSetting.js, it complains that the functions that live in gameLibrary.js aren't defined. When it gets to parsing gameLibrary.js, the variables in gameSetting.js are reported as not being defined. I've tried dynamically bootstrapping the gameLibrary file using this function in document.ready for dynamic load... $.getScript("gameLibrary.js"); However, the same problem still happens in IE, where when it parses the files individually it's not taking into context the file/variables that came before, so it's not an out of load order problem. My options a 1) collapsing all the functions in gameLibrary.js and variables in gameSetting.js into one file. However, this is not practical because this is dealing with literally hundreds of games, and having a gameLibrary.js in ONE location for ONE update is what makes most logical sense. 2) figure out a way to get this to work where variables in file1 are accessible to file2 in IE (as it seems they are in other browsers). jQuery seems to be able to have multiple plugins that all refer to the based jQuery-1.3.2.js, so I know there is a way to get this to work. Help appreciated. Nero Hi, Ive got a iphone based website im writing in html/javascript, it all works perfectly well apart from an issue with a javascript variable and i cant figure out what im doing wrong At the start of the code i declare a variable var jpgname='blank'; - its blank for testing I then have a href which attempts to change this variables value when clicked <li class="flip"><a onClick="jpgname='test'" href="#page">Cars</a></li> If i replace the jpgname='test' with confirm('test) i get a popupbox so i know its running this code under my #page section (just a div named section) i have [<h1><script>document.write(jpgname);</script></h1> however this always has the value 'blank' Im sure im doing something stupid but im not sure what Any help gratefully recieved thanks Mike Hi, I'm looking to use the document function in javascript to change the TYPE of an INPUT. However I want to change the TYPE of the INPUT according to a variable passed to the javascript button, so i use: document.formname.inputname.value="newvalue"; I want to be able to change what inputname is depending on what is passed to the function, but right now firefox is telling me that it's undefined. Is there any way I can do this? Thanks in advance, Daniel I am perplexed about this one. Not sure if php is the best solution or maybe javascript. ( or both.) I am writing a litle script that will go to my mysql table and take out 20 rows ( from about 10,000 rows ) based on the WHERE statement. Then I want to step through these 20 rows displaying just two filds in this fashion: First, I want to display one field in a "box" ( using divs and css ) then wait for a form input. Then while keeping the first displayed box and field display the second field in a similar box a fee lines below. Wait for a form input update some data. Then onto the next row. If I use javascript then I could keep all the processing on one page and not have to have server refreshes. But how do I get these those array elements from $row['field1'] and $row['field2'] into javascript vars ? Is this the best way to do this ? Or would php and having a couple of extra trips to the server for form processing be better ? Thanks for any input. . Hi is it possible to compare 2 php variables with javascript and hide-show a div according to the result? lets say php variables are Code: $a $b and if ($a > $b) show the div can anyone please help me to do this? Sorry for language errors Thanks I have jQuery like: jQuery(".forms_1_1").validate({ rules: { inputname1: { required: true, }, or inputname1: { required: "Requested field!.", }, Is it technically possible to add 1. PHP variable like $_SESSION and 2. Smarty variable into Javascript like {$global.variable1} inputname1 is value for each variable (PHP, Smarty). Need help how to include variable values into Javascript code. I am about to translate some C++ code to JavaScript. In C++, it is straightforward to declare and use a Boolean variable; all I do is swap it between true and false and back again as needed. For example, Code: // C++ Code . . . bool na = 1; . . . na = !na; . . . I am wondering if JavaScript offers boolean variables. As far as I know, it does not, but I thought I would confirm by asking around. At the moment, I am planning to code the comparable block in Javascript using an "if" statement or a ternary operation. For example, Code: JavaScript Code . . . var na = 1; // Option 1 if (na) na = 0; else na = 1; // Option 2 na = ((na) ? 0 : 1); . . . Is one of these options recommended as the best way to simulate a boolean variable and operation? (I would like the fastest-executing code possible.) Or is there another--better--way to do it? All advice and recommendations appreciated. Thank-you in advance. 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); How can I have global variables and global arrays whose value(s) can be modified and accessed by all the Javascript functions in my HTML document as well as by the HTML code? Thank You CGG Hi all, I'm a javascript newbie and having some trouble with what I imagine it s a very basic issue! I am trying to declare a variable inside a function and use it later on in my code... but it just already returns white space... i.e. not variable value. I am setting it within this function: Code: function show_video1(){ document.getElementById('video1').style.display="block"; var video1Name = "Education World News (Part 1)"; document.getElementById('video2').style.display="none"; document.getElementById('video3').style.display="none"; document.getElementById('video4').style.display="none"; document.getElementById('video5').style.display="none"; document.getElementById('video6').style.display="none"; document.getElementById('video7').style.display="none"; document.getElementById('video8').style.display="none"; document.getElementById('video9').style.display="none"; document.getElementById('video10').style.display="none"; document.getElementById('video11').style.display="none"; } and trying to call it later on with this: Code: <script type="text/javascript">document.write(video1Name)</script> It might be worth noting that each one of my 11 videos will hace a different name. Can anyone help out a newbie? Many thanks, greens85 Hi, I hope someone can help me! I have a list of buttons, each with a different id which is taken from the ids of these values from the database. When I click one of the buttons it shows the items that correspond to this id in the database. Therefore, when different buttons are clicked the display changes to match the objects relating to this particular button. When a button is clicked, this shows various other objects which I can then select (using a checkbox). When a checkbox is clicked, a Javascript function is called to change the html text next to the appropriate button. However, I can't get this to work as I need to pass the id of the button, and the html span, to the Javascript in order to change the correct text. Here is my Javascript : Code: function questionnum(checkbox) { if(checkbox.checked) { var qdesNumElement = document.getElementById('<?php echo $q;?>'); var qdesNum = qdesNumElement.innerHTML; qdesNumElement.innerHTML = ++qdesNum; } This code only gets the last button. Please help. Hello Im using javascript and cookies to send to another page in my site some variables In the other page i recieve them like that : Code: <script language="JavaScript"> if (widget_array != ""){ for (i=1; i < widget_array.length; i++){ document.write("<h3>" + widget_array[i] + "</h3>"); }//ends diplay FOR }//ends widget display if(ninja_array != ""){ for (i=1; i < ninja_array.length; i++){ document.write("<h3>" + ninja_array[i] + "</h3>"); }//ends FOR }//ends ninja display </script> Now my question is ....(and this is the php - sql question) how can i make the variable the i passed to stay a variable , e.g. variable page 1: id=123 *i entered the number 123 into the cookie variable page 2: now this page will show me ALL the array ( 123...134...156...etc...) but i dont want it to print it on the screen i just want to get variables that i can use php-mysql e.g. $id=123 in order to print the specific data from my data base hope u got it.... Danny Hi, I'm very new to using javascript to program Photoshop, and was looking for some help with changing the variables in an action. The basic end result I'm looking for (in laymens terms) is: Set variables Perform action Change variables (iteratively, ie a=a+1) Repeat until condition met (ie, after 100 iterations, the script would end). I used a script that converts Photoshop actions to Javascript, so I have this as a base: Code: function step1(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); desc1.putInteger(cTID('0001'), 215 ); desc1.putInteger(cTID('0002'), 172 ); etc etc (goes through many other variables) executeAction(sTID('Flaming Pear'), desc1, I've put the properties that would need to become variables in red. I have edited out a bunch of lines in this post as these seem to be the significant ones in terms of what I need to do (obviously keeping them in the script itself!). Other than that I think that's all I need to know for now - just that little thing would enhance my workflow no end. Also, I'm a graphic designer so if anyone would be interested in doing some skillshare or work swap - I'd be happy to provide help and service with graphics in exchange for small tidbits about javascript. Cheers, Jonathan |