Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 69
  1. #1
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Problem with Code

    hey there,
    The code i'm about to give you, isn't for some reason, doing what its ment. Its not outputing anything.

    Code:
    function bold(form, txtbox) {
        if(a == '1') {
            txt = document.forms[form][txtbox].value
            text = txt + '[/B]'
            document.forms[form][txtbox].value = text
            a = 0
        }
        else
        {    txt = document.forms[form][txtbox].value
            text = txt + '[B]'
            a = 1
        }
    }
    If you have any ideas please help...

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    477
    Thanks
    0
    Thanked 51 Times in 50 Posts
    If you're not getting anything at all, please post more of your code (the form at least). If you're getting [/B] but not [B], you're missing a line from the else block.

    Duplicate this line from the if block into the else block
    Code:
    document.forms[form][txtbox].value = text

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Ok, I now have (fully):
    Code:
    <script type="text/javascript">
    
    function bold(form, txtbox) {
    	if(a == 1) {
    		txt = document.forms[form][txtbox].value
    		text = txt + '[/B]'
    		document.forms[form][txtbox].value = text
    		a = 0
    	} 
    	else
    	{
    		txt = document.forms[form][txtbox].value
    		text = txt + '[B]'
    		document.forms[form][txtbox].value = text
    		a = 1
    	}
    }
    
    </script>
    
    <form action="index.php?p=members/cpanel&a=addreview2" method="post" name="addreview">
    Game: <br>
    <input type="text" name="Game"><br><br>
    <u>Ratings</u><br>
    Graphics: (1-10)<br>
    <input type="text" name="Graphics"><br>
    Sound: (1-10)<br>
    <input type="text" name="Sound"><br>
    Multiplayer: (1-10 [Enter 0 if no multiplayer])<br>
    <input type="text" name="Multiplayer"><br>
    Overall: (1-100)<br>
    <input type="text" name="Overall"><br><br>
    <u>Console And Review</u><br>
    Consoles: <br>
    <input name="XBox" type="checkbox" id="XBox" value="XBox">X-Box<br>
    <input name="PS2" type="checkbox" id="PS2" value="PS2">PS2<br>
    <input name="GC" type="checkbox" id="GC" value="GC">Game Cube<br>
    <input name="GBA" type="checkbox" id="GBA" value="GBA">Game Boy Advance<br>
    <input name="PC" type="checkbox" id="PC" value="PC">PC<br>
    <br>
    Review (No HTML):
    <i>Tags</i><br>
    <input type="Button" name="Bold" value="Bold" onclick="bold('addreview', 'Review')"></input>
    <br>
    <textarea name="Review" cols="40" rows="5"></textarea><br><br>
    <input type="submit" name="Submit" value="Submit"><br>
    Hope this helps you to help me!

    -Neo

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    if(a == 1) {
    Remove the quote marks, you're setting numeric values but testing alpha.

    Also make sure you turn on notifications about javascript errors in IE or open the javascript console in Moz/Firefox so you can see when errors occur.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Ok, Thanks for that, And It's giving me the error:
    Quote Originally Posted by Mozilla FireFox
    Error: a is not defined
    Source File: http://home.prcentral.co.uk/neo/gs/i...ditreview&id=5
    Line: 51
    I understand the error, but how am I ment to set a? I only wish to set it to 2 for example if its not equal to 1...so how would I do this?

    -Thanks
    -Neo

  • #6
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do this:

    Code:
    a = 0; // Defines the global variable 'a'
    function bold(form, txtbox) {
    ...

  • #7
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Wouldn't that set a to 0 even if its equal to 1? I only want it set to 0 if its non-existant

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    477
    Thanks
    0
    Thanked 51 Times in 50 Posts
    Outside the function it will set a to 0 once, and only once. This is known as initializing the variable.

  • #9
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Ok, I now have:

    Code:
    <script type="text/javascript">
    
    $a = 0;
    $b = 0;
    $c = 0;
    
    function bold(form, txtbox) {
    	if(a == 1) {
    		txt = document.forms[form][txtbox].value
    		text = txt + '[/B]'
    		document.forms[form][txtbox].value = text
    		a = 0
    	} 
    	else
    	{
    		txt = document.forms[form][txtbox].value
    		text = txt + '[B]'
    		document.forms[form][txtbox].value = text
    		a = 1
    	}
    }
    
    function italic(form, txtbox) {
    	if(b == 1) {
    		txt = document.forms[form][txtbox].value
    		text = txt + '[/I]'
    		document.forms[form][txtbox].value = text
    		b = 0
    	} 
    	else
    	{	
    		txt = document.forms[form][txtbox].value
    		text = txt + '[I]'
    		b = 1
    	}
    }
    
    function underline(form, txtbox) {
    	if(c == 1) {
    		txt = document.forms[form][txtbox].value
    		text = txt + '[/U]'
    		document.forms[form][txtbox].value = text
    		c = 0
    	} 
    	else
    	{
    		txt = document.forms[form][txtbox].value
    		text = txt + '[U]'
    		c = 1
    	}
    }
    
    </script>
    And still get:
    Error: a is not defined
    Source File: http://home.prcentral.co.uk/neo/gs/i...ditreview&id=5
    Line: 55

  • #10
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dude... You defined it as $a, not a. You're confusing PHP/PERL with JS

  • #11
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by NeoPuma
    Ok, I now have:
    Code:
    $a = 0;
    $b = 0;
    $c = 0;
    That' PHP syntax. It should be
    Code:
    var a = 0;
    var b = 0;
    var c = 0;

  • #12
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, that's what I said...

  • #13
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Dam you php >.<. Thanks guys - Works!
    Your the best :P

    -Neo

  • #14
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by AaronW
    Hey, that's what I said...
    Yeah, that happens when it takes you an hour to get back to a tab.

  • #15
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, and by the way, it isn't "var a", it's just "a". The "var" keyword suggests local to the current scope I believe. That's why it's used in object definitions.

    I could be wrong though. I'm rather good at that.


  •  
    Page 1 of 5 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •