Jump to content

javascript and php variables??


shadiadiph

Recommended Posts

I have been building an AJAX form my javascript knowledge is terrible but I am having a horribble time with javascript variables I assign the response var a = xmlHttp.responseText etc result which it gets fine why if i get a response from an xml node the javascript variable can compare if (var a =="error") { do this } else { do that} but with the .responseText reply will not recognise it and jump straight to the else I have tried returning a number as an error false as an error javascript just won't seem to read a returned variable properly I even tried .length==0 no joy??? Any tips? I'm beginning to think javascript and php were not supposed to coexist :(

Link to comment
https://forums.phpfreaks.com/topic/205673-javascript-and-php-variables/
Share on other sites

Ok I have a form it is sending country,state,city,postal step by step with AJAX to a php page it either returns "error" if the next set of data is not found and does not display the div.

 

Problem is when i use

 

var result = xmlHttp.responseText;

if (result=="error')
{
//hide div
}
else
{
// display div
}

 

It doesn't recognise the word error but displays error as the else result

 

I have tried

 

var resultlen = result.length;

 

it keeps returning 8 if I don't return anything the length is 3 my question is why can't javascript not recognise the string with the value of error?  When I did this a different way using <xml> calling the node values it recognised the text string if (result=="error") etc it understood that but why not with xmlHttp.responseText; and how to deal with this?

 

 

right now here is my code the alert returns nostates when i select a country that has no states and when it has it returns the correct value but everytime it only executes the else statement regardless??

 

document.getElementById("tempresult").value=xmlHttp.responseText;

var stateval;
var stateval = document.getElementById("tempresult").value;

alert('State Value' +stateval);

if (stateval == "nostates")
{
document.getElementById('staterow').style.visibility='hidden';
document.getElementById('cityrow').style.visibility='hidden';
document.getElementById('postalrow').style.visibility='hidden';
getcitybycc();  
}
else
{
document.getElementById('staterow').style.visibility='visible';
document.getElementById('stateinput').innerHTML=xmlHttp.responseText;
document.getElementById('cityrow').style.visibility='hidden';
document.getElementById('postalrow').style.visibility='hidden';
}

 

I also get the same if i use

 

document.getElementById('tempresult').innerHTML=xmlHttp.responseText;

 

my php page

 

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

 

 

i tried it with trim without trim still the same result

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.