...

View Full Version : Help



Jeremyfto
09-26-2012, 04:01 AM
Alright what I'm trying to do is create a check box type deal in which you choose what site you want to choose to go to by the choices you make and I created a sample code that is coming out with a error. can someone fix it and tell me what I did wrong thanks.(p.s its a sample cause it only dealt with 2/9 of the ways the final answer would turn out)

<html>
<head>
<title> New Script </title>
<script type="text/javascript">

function {
if (a=1) (b=0) (c=0)
then
{
x=(www.wix.com)
}
function {
if (a=0) (b=0) (c=0)
then
{
x=(www.google.com)"
}
</script>
</head>
<body>

<input type="checkbox" name="CBox0"
onsubmit="if(this.checked){var a=1} else {var a=0}"> Chat Room

<br>

<input type="checkbox" name="CBox1"
onsubmit="if(this.checked){var b=1} else {var b=0}"> Music

<br>

<input type="checkbox" name="CBox2"
onsubmit="if(this.checked){var c=1} else {var c=0}"> Games

<br>

<input type="submit" name="submit">
</body>
</html>

Old Pedant
09-26-2012, 04:34 AM
This isn't even vaguely correct:


if (a=1) (b=0) (c=0)

Where did you dream up that syntax??

First of all, you are only allowed one ( ) as the container for the condition of an if statement.

And then you are using =. In JavaScript (and in Java and C and C++ and C# and PHP and ...) the = operator is *ALWAYS* an assignment! Yes, even in an if statement.

To TEST a value, you must use ==.

So, I *THINK* what you are after is this:


if (a==1 && b==0 && c==0)


But there are so many other things wrong with that code, it's tough to know where to start.

A function must have a name and the name must have at least ( ) after it [optionally, parameter names can appear between the parentheses].

There is no such thing as onsubmit for checkboxes.

This isn't vaguely right:

x=(www.wix.com)
and I'm not even sure what you expected it to do.

You have nothing at all calling your (illegal) functions.

There is no such thing as a then keyword in JavaScript (or in Java or C++ or ... ).

Philip M
09-26-2012, 09:11 AM
Jeremy fto - I am sorry to say that your code is riddled with errors. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

You would do best to discuss your problems with your teacher.

Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.

Jeremyfto
09-26-2012, 02:11 PM
I see what you mean by the a==. you see I have never actully gone to a class on coding iv just watched tutorials. I was building this for a bigger project. its spost to be multi-choice check boxes but I'm guessing I failed at it. any tips on where I should be headed to get this coded?

This isn't even vaguely correct:


if (a=1) (b=0) (c=0)

Where did you dream up that syntax??

First of all, you are only allowed one ( ) as the container for the condition of an if statement.

And then you are using =. In JavaScript (and in Java and C and C++ and C# and PHP and ...) the = operator is *ALWAYS* an assignment! Yes, even in an if statement.

To TEST a value, you must use ==.

So, I *THINK* what you are after is this:


if (a==1 && b==0 && c==0)


But there are so many other things wrong with that code, it's tough to know where to start.

A function must have a name and the name must have at least ( ) after it [optionally, parameter names can appear between the parentheses].

There is no such thing as onsubmit for checkboxes.

This isn't vaguely right:

x=(www.wix.com)
and I'm not even sure what you expected it to do.

You have nothing at all calling your (illegal) functions.

There is no such thing as a then keyword in JavaScript (or in Java or C++ or ... ).

Philip M
09-26-2012, 05:44 PM
I see what you mean by the a==. you see I have never actully gone to a class on coding iv just watched tutorials. I was building this for a bigger project. its spost to be multi-choice check boxes but I'm guessing I failed at it. any tips on where I should be headed to get this coded?

Do what you would do if your car or a domestic appliance broke down or you had toothache - pay a professional who understands what they are doing to fix it. You can post in the Web Projects section of his forum.

Old Pedant
09-26-2012, 11:03 PM
I may regret this, but...


<!DOCTYPE html5>
<html>
<body>
<form id="theForm">
<label><input type="checkbox" name="choice" value="1"/> Chat Room</label><br/>
<label><input type="checkbox" name="choice" value="2"/> Music</label><br/>
<label><input type="checkbox" name="choice" value="4"/> Games</label><br/>
<input name="doit" type="button" value="Find Site"/>
</form>
<script type="text/javascript">
(
function( )
{
// replace my dummy urls with the real ones for the 7 possible combinations
var targets = [
null, /* no boxes checked */
"chat.com",
"music.com",
"chat_music.com",
"games.com",
"chat_games.com",
"music_games.com",
"chat_music_games.com"
];

// attach event to the button
form = document.getElementById("theForm");

form.doit.onclick = function( )
{
var cbs = form.choice;
var choiceSum = 0;
for ( var c = 0; c < cbs.length; ++c )
{
if ( cbs[c].checked ) choiceSum += Number(cbs[c].value);
}
if ( choiceSum != 0 )
{
location.href = "http://www." + targets[choiceSum];
}
}
}
)( );
</script>
</body>
</html>

A completely different approach than your complex set of "ifs".

By giving a "weight" to the 3 choices, you simply take the sum of the weights (which will be an integer from 0 to 7) and use it to pick the URL from an array of URLs.

Here, I have assumed that if the user checks no checkboxes, then you do not send her/him anywhere. But you can easily change that, of course.

Jeremyfto
09-27-2012, 02:23 AM
Thank you so much. I appreciate your help :) .

I may regret this, but...


<!DOCTYPE html5>
<html>
<body>
<form id="theForm">
<label><input type="checkbox" name="choice" value="1"/> Chat Room</label><br/>
<label><input type="checkbox" name="choice" value="2"/> Music</label><br/>
<label><input type="checkbox" name="choice" value="4"/> Games</label><br/>
<input name="doit" type="button" value="Find Site"/>
</form>
<script type="text/javascript">
(
function( )
{
// replace my dummy urls with the real ones for the 7 possible combinations
var targets = [
null, /* no boxes checked */
"chat.com",
"music.com",
"chat_music.com",
"games.com",
"chat_games.com",
"music_games.com",
"chat_music_games.com"
];

// attach event to the button
form = document.getElementById("theForm");

form.doit.onclick = function( )
{
var cbs = form.choice;
var choiceSum = 0;
for ( var c = 0; c < cbs.length; ++c )
{
if ( cbs[c].checked ) choiceSum += Number(cbs[c].value);
}
if ( choiceSum != 0 )
{
location.href = "http://www." + targets[choiceSum];
}
}
}
)( );
</script>
</body>
</html>

A completely different approach than your complex set of "ifs".

By giving a "weight" to the 3 choices, you simply take the sum of the weights (which will be an integer from 0 to 7) and use it to pick the URL from an array of URLs.

Here, I have assumed that if the user checks no checkboxes, then you do not send her/him anywhere. But you can easily change that, of course.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum