unistake Posted July 10, 2009 Share Posted July 10, 2009 Hi all, I am new to JS - please be patient! I am having trouble creating a script.. I have 3 radio buttons each of which, when selected, I want them to include a separate JS file. Also I have php variables that I wish to include within the included JS files. The link between the form and the javascript file also does not seem to work. This is what I have made so far... index.php <?php $x = 4; $y = 5; $variable = $x+$y; // If I left this variable here, how would I include it in the 3 javascript files below? ?> <script language="javascript"> function selection () { var radio; for(i=0;i<document.selection.radio.length;i++) { if(document.selection.radio[i].checked) radiochoice = document.selectio.radio[i].value; } if (radio=="initial") { include ("initial.js"); } if (radio=="sr") { include("sr.js"); } if (radio=="snr") { include("snr.js"); } } </script> <form name="selection"> <label> <input type="radio" name="radio" id="radio" value="initial" onClick="selection()" checked> </label> <label> initial <input type="radio" name="radio" id="radio2" value="sr" onClick="selection()"> stake returned </label> <label> <input type="radio" name="radio" id="radio3" value="snr" onClick="selection()"> stake not returned<br> </label> </form> Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/ Share on other sites More sharing options...
jug Posted July 10, 2009 Share Posted July 10, 2009 Why do you need to include the javascript pages? Unless the javascript pages you want to include has 1000s of lines of code (ie big script size that could make page load time slow), either replace the include lines with function calls or include all 3 scripts in the <head> of the html page. Hope this helps. jug Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-872887 Share on other sites More sharing options...
unistake Posted July 10, 2009 Author Share Posted July 10, 2009 Hi jug, thanks for the suggestion. each included file has about 100 lines of code and I think it would be easier to read and manage as a newbie to JS. Is there a way to include files like that in JS as I am used to PHP? Thanks Tim Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-872890 Share on other sites More sharing options...
jug Posted July 10, 2009 Share Posted July 10, 2009 As far as im aware you cant include other files like in PHP in the actual script. Personally i would include all 3 .js scripts like the following <html> <head> <script type="text/javascript" src="/initial.js"></script> <script type="text/javascript" src="/sr.js"></script> <script type="text/javascript" src="/snr.js"></script> </head> ...etc </html> then in the javascript just call the relevant initial function for each depending on radio value so... <script language="javascript"> function selection () { var radio; for(i=0;i<document.selection.radio.length;i++) { if(document.selection.radio.checked) radiochoice = document.selectio.radio.value; } if (radio=="initial") { initialInit(); } if (radio=="sr") { srInit(); } if (radio=="snr") { snrInit(); } } </script> the initialInit(); function would be in intial.js script the srInit(); function would be in sr.js script the snrInit(); function would be in snr.js script Again hope this hopes. Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-872914 Share on other sites More sharing options...
unistake Posted July 10, 2009 Author Share Posted July 10, 2009 Thanks for that i will use your method, could someone show me how to link the radio buttons in the form to the javascript to execute the IF functions, as the above code does not work, and I dont know where I have gone wrong. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-872961 Share on other sites More sharing options...
jug Posted July 11, 2009 Share Posted July 11, 2009 This should help. selection = document.getElementsByName("insertradiobuttonnamehere"); for(i=0; i<selection.length; i++){ if(selection.checked == true){ var selected = selection.value; } } if(selected == "initial"){ initialInit(); }else{ if(selected == "sr"){ srInit(); }else{ if(selected == "snr"){ snrInit(); } } } jug Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-873742 Share on other sites More sharing options...
unistake Posted July 11, 2009 Author Share Posted July 11, 2009 Hi Jug, thanks for the post, however it still seems to not work. The code I now have at present is.... <head> <script type="text/javascript"> function calc () { var radiochoice; selection = document.getElementsByName("typeofbet"); for(i=0; i<selection.length; i++){ if(selection.checked == true){ var selected = selection.value; } } if(selected == "initial"){ initialInit(); }else{ if(selected == "sr"){ srInit(); }else{ if(selected == "snr"){ snrInit(); } } } function qualifier () { document.write("this is function qualifier!"); } function sr() { document.write("this is function SR!"); } function snr() { document.write("this is function SNR!"); } } </script> </head> <body> <form name="calcform"> <p> <input type="radio" name="typeofbet" value="qualifier" checked> radio1 <input type="radio" name="typeofbet" value="freesr"> radio2 <input type="radio" name="typeofbet" value="freesnr"> radio3<br> <input type="submit" value="Click To Calculate" onClick="calc();"> </p> </form> </body> Any more ideas?! Thanks Tim Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-873753 Share on other sites More sharing options...
jug Posted July 11, 2009 Share Posted July 11, 2009 <html> <head> <script type="text/javascript"> function calc (){ selection = document.getElementsByName("typeofbet"); for(i=0; i<selection.length; i++){ if(selection.checked == true){ var selected = selection.value; } } if(selected == "qualifier"){ qualifier(); }else{ if(selected == "freesr"){ sr(); }else{ if(selected == "freesnr"){ snr(); } } } } function qualifier (){ alert("qualifier"); } function sr(){ alert("sr"); } function snr(){ alert("snr"); } </script> </head> <body> <form name="calcform"> <p> <input type="radio" name="typeofbet" value="qualifier" checked="checked"> qual <input type="radio" name="typeofbet" value="freesr"> sn <input type="radio" name="typeofbet" value="freesnr"> snr </p> <p><input type="button" value="Click To Calculate" onClick="return calc();"></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-873767 Share on other sites More sharing options...
unistake Posted July 11, 2009 Author Share Posted July 11, 2009 Hi jug, thanks again. does that script work as it should for you? I can not seem to get it to work still. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-873775 Share on other sites More sharing options...
unistake Posted July 12, 2009 Author Share Posted July 12, 2009 anyone got any ideas?! Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-873942 Share on other sites More sharing options...
jug Posted July 12, 2009 Share Posted July 12, 2009 works for me. http://dev.bw-design.co.uk/bin/unistake.htm Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-874066 Share on other sites More sharing options...
unistake Posted July 21, 2009 Author Share Posted July 21, 2009 Hi Jug, been away for the week. Thanks for that link. hopefully I can get it working! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/165500-if-functions-and-radio-buttons/#findComment-879532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.