...

View Full Version : if statement not working in php javascript



j-iN-uK
03-22-2004, 12:52 PM
Hi,

I have a routine, where onchange of an item in my listbox then only does my School & Addr get populated, this works fine, I also have an if statemetn in my java, that only, when document.f1.txtType.value = 'SCHOOL' then i should populate the remaining text boxs. But it does not seem to do the check fine, cos it populates the remaining boxs anyway, i want to only do it when Type = 'SCHOOL'. Please tel me what am I dong wrong.

$sJavaScript = "function Schoolselected(elem)

{ for (var i = document.f1.length; i >= 0; i--)
{ document.f1.txtSchool.value[i] = null
document.f1.txtAddr.value[i] = null

$SName="";

while ( $row = mysql_fetch_array($result) )
{
If ($SName!=$row["SchoolName"]){

$SName = $row["SchoolName"];
echo "\n<OPTION VALUE='".$row["SchoolName"]."'>".$SName."</OPTION>
";

$sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";

$sJavaScript = $sJavaScript."document.f1.txtSchool.value = ('".$row["SchoolName"]."');\n";
$sJavaScript = $sJavaScript."document.f1.txtAddr.value = ('".$row["Street"]."');\n";

$sJavaScript = $sJavaScript."if (document.f1.txtType.value = 'SCHOOL');{\n";
$sJavaScript = $sJavaScript."document.f1.txtAddr1.value = ('".$row["Street"]."');\n";}\n";

}
}
$sJavaScript = $sJavaScript."\n}\n}\n";
echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
echo "\n".$sJavaScript."\n</SCRIPT>\n";
}
}
?>

raf
03-22-2004, 01:29 PM
Please tel me what am I dong wrong.
You're posting it in the wrong forum:D

Check the outputted source and then figure out what is wrongso you know how to change your generating PHP script.

j-iN-uK
03-22-2004, 01:34 PM
This is the correct forum from my knowledge as this is routine is called from my php page.

raf
03-22-2004, 02:47 PM
Ooooh. Very sorry.


This is the correct forum from my knowledge as this is routine is called from my php page.

I see. Never heard of a routine that is called from a PHP page. Is this a clientside routine that is called? How intresting. I never knew this was possible. Which is probably why i completely misread it or why i don't understand your innovating code. I mistook you for a regular PHP coder that was hindered by the technical limitations of server side scripting. But you are calling clientside routines from your php page !!
But anyway, what is wrong? your PHP code or your javascript logic? The actions you describe, are they serversided, cause it kinda looks like a javascriptarray your reading with your javascript function?
And it kinda sounds like there is something wrong with your javascrip-function. So you might wanna look into that direction to start debugging it.

What probably confused me was this


$sJavaScript = $sJavaScript."\n}\n}\n";
echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
echo "\n".$sJavaScript."\n</SCRIPT>\n";

which i thought was just echoing the javascript and array to the client . You see, traditionally, after that the code is sent to the client, the server side script has no longer control over it. So all the actions you described, well, there isn't any server side processing involved. But you obviously diagree since you are etc etc

No? So it would be intresting to see the code that is generated by your PHP code + maybe even the form where your controls are in (or maybe you don't use forms? You never know with these cutting edge features). Will be a lott easier to debug, for people with javascript knowledge. Ooopss. Forgive me. I'm still thinking in traditional terms where clientsided javascript is executed clientsided. But you see, this is all so new to me ...

But i'll leave you allone now with my obvious outdated knowledge. Maybe someone with a more modern, unrestricted and clearer view on things will step in. I don't care where you post it, just telling you that your probably looking in the wrong place for an anser, which is basically just your own problem.

firepages
03-22-2004, 03:01 PM
errrrr ;)

while Raf is right I think you may have caught him on a bad day ...

get the javascript working first (post in javascript if having problems), then see if & if so ,why your PHP is not creating that javascipt correctly.

raf
03-22-2004, 03:28 PM
Originally posted by firepages
errrrr ;)

while Raf is right I think you may have caught him on a bad day ...

get the javascript working first (post in javascript if having problems), then see if & if so ,why your PHP is not creating that javascipt correctly.
Yes, I'm sorry about being sarcastic.
Just found my first grey hair this morning :D

Sorry j-iN-uK

Nightfire
03-22-2004, 03:47 PM
You missin the . before the = on these lines

$sJavaScript .= $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";

$sJavaScript .= $sJavaScript."document.f1.txtSchool.value = ('".$row["SchoolName"]."');\n";
$sJavaScript .= $sJavaScript."document.f1.txtAddr.value = ('".$row["Street"]."');\n";

$sJavaScript .= $sJavaScript."if (document.f1.txtType.value = 'SCHOOL');{\n";
$sJavaScript .= $sJavaScript."document.f1.txtAddr1.value = ('".$row["Street"]."');\n";}\n";

}
}
$sJavaScript .= $sJavaScript."\n}\n}\n";

j-iN-uK
03-22-2004, 03:48 PM
Ok 4Given.......i will try it out

mordred
03-22-2004, 03:49 PM
Raf, you bitter old man, I'll seriously consider nominating you for the most sarcastic member of the week award. Very well written response, especially the line with the "cutting edge" features. I learned a lot. :D

@j-iN-uK:
The problem might be that your if-statement is wrong:



$sJavaScript = $sJavaScript."if (document.f1.txtType.value = 'SCHOOL');{\n";


looks to me that you're doing an assignment rather than a comparison in the condition. To check if two values are equal, you need two equality signs, not one. So check if this would work:



$sJavaScript = $sJavaScript."if (document.f1.txtType.value == 'SCHOOL');{\n";


Apart from that, you're really in the wrong forum, because your problem is not related to PHP at all. As others have said already, posting just the output in the javascript forum would have brought you a quicker answer than dumping it into here.

raf
03-22-2004, 04:12 PM
You missin the . before the = on these lines

$sJavaScript .= $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";
...


It's either (the slightly unefficent and less readable old school)

$sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";


which he had, or the newer and more efficient writable

$sJavaScript .= "}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";


How cruel to lead him up the wrong path !


Mordred :[/i]
Raf, you bitter old man, I'll seriously consider nominating you for the most sarcastic member of the week award.
Just shows my luck :
- first this grey hair on my pillow;
- then my expresso-machine goes broke;
- then at lunch, i seem to have the only plate with only 3 meatballs in my spaghetti;
- then some evil spirit takes posession of me and makes me write horrible replyes
and on top of this all, i'm in danger to get nominated ...

I wonder ... maybe i was unable to post here this weekend because Georges sarcastometer was activated ...

j-iN-uK
03-22-2004, 04:24 PM
$sJavaScript .= $sJavaScript."document.f1.txtSchool.value = ('".$row["SchoolName"]."');\n";
$sJavaScript .= $sJavaScript."document.f1.txtAddr.value = ('".$row["Street"]."');\n";

It's not that I'm missing the . becos if that was the case, then my script won't work at all, the problem is the 2nd part of the script is executing in all cases and not just when if (document.f1.txtFundedBy.value == 'SCHOOL');

Mordred, thanks, I had to change it to ==, but it is still executing in all cases. I am displaying txtFundedBy & can see that its value is something therefore it should not populate the boxes....but it does not wk this way



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum