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.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if statement not working in php javascript

    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";
    }
    }
    ?>

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please tel me what am I dong wrong.
    You're posting it in the wrong forum

    Check the outputted source and then figure out what is wrongso you know how to change your generating PHP script.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Feb 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the correct forum from my knowledge as this is routine is called from my php page.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,045
    Thanks
    10
    Thanked 93 Times in 91 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Sorry j-iN-uK
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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";

  • #8
    New Coder
    Join Date
    Feb 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok 4Given.......i will try it out

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

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

    Code:
    $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:

    Code:
    $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.
    De gustibus non est disputandum.

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #11
    New Coder
    Join Date
    Feb 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •