Hi I have never really bothered with javascript before but i am having alot of problems with a ajax php form my php code returns

if ($states==false)
$result = 'nostates';
$result = trim($result);
return $result;

And my javascript alert returns nostates but always executes the else statement when php returns nostates

var stateval;
var stateval = document.getElementById("tempresult").value;
alert('State Value' +stateval);

if (stateval == "nostates")

Any advice? this is driving me nuts :confused:

Assuming that you have already checked the cases, another possibility is whitespace.
Try an additional alert

alert('length' + stateval.length);

How can Javascript read what the PHP code has returned? PHP code returned is processed on server side and not on client side.

Good point about str .length I tried that already .length of nostates returns a value of 9?? thats why i trimmed it in php but i get the same result? I also tried changinging the return error to a number 69 and using the typeof doesn''t work could it be the new server I am using? It's really p@ss#ng me !ff

Trimming on the php side won't solve your problem.
I haven't tested yet, but after the assignment from .responseText, .innerHTML probably contains something like 'nostates\n'.

sorry i am more of a php programmer \n mmm i tied to trim(stateval); in javascript but it seemed to tell me stateval needed to be defined when it already had been. So how to trim(str) in javascript which to me seems illogical and was made by aliens.

The javascript string object has no trim() method.
If you are not using a framework which supplies one, you can either code one yourself or match in such a way that whitespace is irrelevant. In your situation this

if (/nostates/.test(stateval))

is arguably the smallest change to your existing code that will get the desired result.

So I am now going to argue that point with myself. :)
Depending on how the query is made, it may be possible to leave the comparison exactly as you originally wrote it and change the assignment instead


thanks fxm that worked great just wondering if it is a whitespace issue if so i might have to read up on trimming whitespaces and make my own trim() function.

Thanks alot

just wondering if it is a whitespace issue

Use your browser's javascript debugger to inspect the element.

read up on trimming whitespaces and make my own trim() function.

http://www.somacon.com/p355.php [among many others]

thanks for all the help fxm it was a whitespace issue trimmed it and now if(stateval=="nostates") works woohoo :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.