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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Embedding Javascript inside PHP script

    Hi. I'm fairly new to PHP and i was writing a script in which i wanted to have some javascript included. I was wondering if someone could look this piece of the code over and tell me if this is even valid..
    PHP Code:
    for($k = 0; $k < count($terrArray); $k++){
        if($armiesArray[$k] != NULL || $armiesArray[$k] != 0){
        ?>
        <script language="JavaScript">
        var terrname = <?php echo $terrArray[$k]?>
        document.getElementById(terrname).style.visibility = "hidden";
        </script>
        <?php    
        
    }else if{
        
    ?>
        <script language="JavaScript">
        var terrname = <?php echo $terrArray[$k]?>
        var numarmy = <?php echo $armiesArray[$k]?>
        var player = <?php echo $playerArray[$k]?>
        var bcolor;
        if(player = 1){
            bcolor = "red"
        }else{
            bcolor = "aqua"
        }//if
        document.getElementById(terrname).style.visibility = "visible";
        document.getElementById(terrname).value = numarmy;
        document.getElementById(terrname).style.background-color = bcolor;
        </script>
        <?php
        
    }//if
    }//for

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Umm, well not quite. I wouldn't use a NULL attribute at all, I'd use an isset() or !empty() function instead. I'd also adjust your <?php echo $var?> to simply adding a semi colon at the end of the variable. Don't think a one liner would cause problems, but I've never done it without so I'm not sure.
    As for your javascript, I cannot help you validate that, I'm not much of a javascripter. What I can say, is depending on what you are doing there may be a different way to do the same thing using arrays in javascript. Simply due to $terrArray, if its like 20 for instance then you are going to have a lot of code which surely can be arranged in another way. Best to check with the js guys though for that one.

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is it possible to use a javascript variable with php?
    like echo "javavariable";
    i tried something similar to that and failed

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP has very little (if nothing) to do with JS. PHP just generates the code that is passed to the browser. If you print JS, JS will be sent and if that JS works it will be executed by the browser.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    yeah, JS cannot comunicate with php, but they can work together. PHP can dynamically generate the information that you need, but you cannot set a php variable with javascript. Now, there is I suppose one way to do it, you can use js to set cookies, and you can retrieve these with php. I've never done this before, as I mentioned, I'm not much of a javascripter, and don't know how everything works with it. The question is, do you really need javascript at all? I mean, I find more an more visitors to my sites do not support javascript with their browsers, and therefore I refuse to use it at all, at least for things that need to be done by php, but can shortcut with javascript (checking form fields for example). Otherwise, I find it pretty much useless.

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well it was actually for a countdown script
    i will find a way around it...


  •  

    Posting Permissions

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