Jump to content

IF functions and radio buttons!


unistake

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.