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 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IF statement doesn't recognize value of variable.

    Here's my script wher problem occurs:

    Code:
    $.post('do/register.php?username='+('#username').value, 
    	function(result) {
    		if(result == 1) { $('#message').html("Everything's ok"); }
    		else if(result == 2) { $('#message').html("Username is missing."); }
    		else { $('#message').html("What the hell is wrong here?!"); }
    	});
    If i put alert(result); in function(result){ } part, i get "result" value that is '1' and that is correct value but it seems like IF statement is not recognizing it at all...

    #message div constantly shows me "What the hell is wrong here?!" message...

    Anyone know what could cause this problem?

    Thanks in advance, phpStud.

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Location
    New York, NY
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    well. . . . if the result value is as you say: '1'

    then why dont you put it in quotes too??
    have you tried if(result == '1')

    ??

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Put an alert inside the first IF branch. That will tell you if the code actually gets there or whether the problem might be with $('#message').html("Everything's ok");

    Code:
    $.post('do/register.php?username='+('#username').value,      
    function(result) {         if(result == 1) { alert(result); $('#message').html("Everything's ok"); }         
    else if(result == 2) { $('#message').html("Username is missing."); }         
    else { $('#message').html("What the hell is wrong here?!"); }     });
    Last edited by webdev1958; 03-10-2012 at 12:40 AM.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by swiper View Post
    well. . . . if the result value is as you say: '1'

    then why dont you put it in quotes too??
    have you tried if(result == '1')

    ??
    it doesn't matter. js has loose data typing - which is another can of worms.
    Code:
            var result='1';
            if(result == 1) {alert('got here')} //output is 'got here'

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought i fixed but now, i get the same problem again...
    Last edited by phpStud; 03-10-2012 at 01:16 AM.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by webdev1958 View Post
    Put an alert inside the first IF branch. That will tell you if the code actually gets there or whether the problem might be with $('#message').html("Everything's ok");

    Code:
    $.post('do/register.php?username='+('#username').value,      
    function(result) {         if(result == 1) { alert(result); $('#message').html("Everything's ok"); }         
    else if(result == 2) { $('#message').html("Username is missing."); }         
    else { $('#message').html("What the hell is wrong here?!"); }     });
    I've already tried this before i posted here. It doesn't show alert when it is in IF statement. I'm getting really nervous...

    do/register.php file should check if(strlen($_GET['username']) > 3) { echo 1; } else { echo 0; }

    I tried with $_POST['username'] but then i dont get any value as username in php file.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by webdev1958 View Post
    it doesn't matter. js has loose data typing - which is another can of worms.
    Code:
            var result='1';
            if(result == 1) {alert('got here')} //output is 'got here'
    That's what === is for - to disallow type conversion in the comparison. It is also why you should always use === and never use == (and similarly !== and never !=) when writing your JavaScript. That way you avoid that particular can of worms - at least in that comparison.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    That's what === is for - to disallow type conversion in the comparison. It is also why you should always use === and never use == (and similarly !== and never !=) when writing your JavaScript. That way you avoid that particular can of worms - at least in that comparison.
    I've tried with === too but i forgot did i write === '1' or === 1. I will try again tomorrow, i am so tired now.

    Thanks for answers, i appreciate.

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by felgall View Post
    That's what === is for - to disallow type conversion in the comparison. It is also why you should always use === and never use == (and similarly !== and never !=) when writing your JavaScript. That way you avoid that particular can of worms - at least in that comparison.
    I don't bother with === or !==. I just convert the data to the correct data type first. I've probably been "brain-washed" during my Java days.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by webdev1958 View Post
    I've probably been "brain-washed" during my Java days.
    Possibly.

    Using == and != is actually considered to be bad practice by the JavaScript experts. The JSlint program will give a warning whenever you use them.

    They are also particularly prone to typos such as accidentally leaving out one of the = from == and getting totally unexpected results whereas if you leave out an = from === it will still most likely work correctly. Both are easily found by JSlint though so provided you actually validate your script code through that program you can avoid those typos from being missed.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    "Javascript experts"????

    Who defines the criteria. Surely that judgement is subjective.

    There are people on this and other websites (not you) who I'm sure consider themselves javascript experts and imo are not even close.

    Although your "legal advice" (given to me in the past) leaves a lot to be desired, I view you and people like Paul Wilkins over at Sitepoint as js "experts". I'm sure there are other worthy "experts" around as well but I just haven't seen enough of their posts to judge for myself. Personally, I would say I'm at an intermediate level.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You don't have to put alerts. Just use Firebug or Chrome Developer Tools and put a breakpoint at the start of success callback.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by glenngv View Post
    You don't have to put alerts. Just use Firebug or Chrome Developer Tools and put a breakpoint at the start of success callback.
    Don't have to use Firebug or any debugger either. I code in small chunks and using alert()s is a lot quicker for me than using a debugger and setting break points, watch lists etc blah blah.

    The only time I'd use a debugger is if someone was paying me to fix their stuff ups on a whole script.
    Last edited by webdev1958; 03-10-2012 at 08:41 AM.

  • #14
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    There are cases where using alert is not convenient. Take this example.

    Code:
    setInterval(function(){
        alert('test');
    }, 1000);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by glenngv View Post
    There are cases where using alert is not convenient. Take this example.

    Code:
    setInterval(function(){
        alert('test');
    }, 1000);
    That would work just fine in Opera or IE9 since every alert Opera displays gives you the option to turn off the script and from the second alert onward IE9 gives the option to disable all dialogs. So as soon as you'd worked out what is going on you'd use the supplied option.

    Since alert is now only really used for debugging (since it provides those options in Opera and IE) it would make sense for the other browsers to add similar options.


    All modern browsers except Firefox have a debugger built in that works in almost exactly the same way. You can get near identical functionality for Firefox by installing the Venkmann extension (Firebug works differently from the standard JavaScript debugger and is intended more for use in resolving HTML and CSS issues)
    Last edited by felgall; 03-10-2012 at 10:38 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  
    Page 1 of 2 12 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
    •