...

View Full Version : IF statement doesn't recognize value of variable.



phpStud
03-10-2012, 12:39 AM
Here's my script wher problem occurs:



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

swiper
03-10-2012, 01:06 AM
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')

??

webdev1958
03-10-2012, 01:15 AM
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");



$.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?!"); } });

webdev1958
03-10-2012, 01:19 AM
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.


var result='1';
if(result == 1) {alert('got here')} //output is 'got here'

phpStud
03-10-2012, 01:39 AM
I thought i fixed but now, i get the same problem again...

phpStud
03-10-2012, 02:30 AM
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");



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

felgall
03-10-2012, 02:46 AM
it doesn't matter. js has loose data typing - which is another can of worms.


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.

phpStud
03-10-2012, 03:08 AM
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.

webdev1958
03-10-2012, 03:53 AM
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.

felgall
03-10-2012, 04:19 AM
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.

webdev1958
03-10-2012, 04:46 AM
"Javascript experts"????:confused::confused::eek:

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.

glenngv
03-10-2012, 07:46 AM
You don't have to put alerts. Just use Firebug or Chrome Developer Tools and put a breakpoint at the start of success callback.

webdev1958
03-10-2012, 09:39 AM
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.

glenngv
03-10-2012, 01:28 PM
There are cases where using alert is not convenient. Take this example.


setInterval(function(){
alert('test');
}, 1000);

felgall
03-10-2012, 11:34 PM
There are cases where using alert is not convenient. Take this example.


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)

webdev1958
03-10-2012, 11:40 PM
There are cases where using alert is not convenient. Take this example.


setInterval(function(){
alert('test');
}, 1000);

In addition to felgall's reply, I don't see your example as an issue.

Quite often I simply comment out the setInterval and just call the function once as a "normal" function. Once it is debugged I then put it back in the setInterval().

Like I said, I would use a debugger only in a situation where someone was paying me to decode their whole script.

I always code in small chunks and test/debug as I go along. This way if something is not working correctly I am debugging only a small piece of code and 99.9999% of the time 1 or 2 alerts()s finds the cause of the problem for me and it is then easy to fix. I use the same coding principle whether coding in javascript or php.

glenngv
03-11-2012, 08:47 AM
What if you are doing a drag and drop operation and you want to check on something while an element is being moved. You cannot use alert to debug it. If you are using Firebug or Developer tools, you can use console.log() to not interrupt the drag and drop action.

<html>
<body>
<div id="drag" style="position:absolute;left:0,top:0;cursor:move;border:1px solid red;">Drag me</div>
<script>
var d = document.getElementById('drag');
d.onmousedown = function(){
document.onmousemove = function(e){
//alert(e.clientX + ' x ' + e.clientY);
//console.log(e.clientX + ' x ' + e.clientY);
d.style.left = e.clientX;
d.style.top = e.clientY;
}
}
d.onmouseup = function(){
document.onmousemove = null;
}
</script>
</body>
</html>

webdev1958
03-11-2012, 01:58 PM
I don't see the point you are making. If I can't use an alert() then obviously I would have to use something else. Doesn't that go without saying?

The point I am making is that if I can use an alert() I will and not bother wasting time using a debugger unless someone is paying me to debug an entire script.

phpStud
03-11-2012, 03:07 PM
Guys... dont u think that u go offtopic abit?

glenngv
03-11-2012, 03:10 PM
Well, I don't see the point with what you were saying that using a debugger is wasting more time than using alerts which require extra code and time to remove them all later.

webdev1958
03-11-2012, 03:14 PM
Well, I don't see the point with what you were saying that using a debugger is wasting more time than using alerts which require extra code and time to remove them all later.

But I am speaking on behalf of myself and no-one else. I explained how I code, test and debug.

If you want to use a debugger that's fine, but surely you're not suggesting that just because you use a debugger that everyone else must then think there is no better way doing things, are you? :). For me, using a debugger is a waste of time for the reasons I desctibed earlier.

And how long do you think it takes to delete 3-4 alert()s at the most at a time?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum