Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-31-2005, 07:04 AM   PM User | #1
Hardeep_S
New Coder

 
Join Date: Feb 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Hardeep_S is an unknown quantity at this point
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
Hardeep_S is offline   Reply With Quote
Old 03-31-2005, 09:11 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 03-31-2005, 09:44 AM   PM User | #3
Extreme43
New Coder

 
Join Date: Mar 2005
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Extreme43 is an unknown quantity at this point
is it possible to use a javascript variable with php?
like echo "javavariable";
i tried something similar to that and failed
Extreme43 is offline   Reply With Quote
Old 03-31-2005, 01:40 PM   PM User | #4
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Old 03-31-2005, 02:46 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-01-2005, 12:56 PM   PM User | #6
Extreme43
New Coder

 
Join Date: Mar 2005
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Extreme43 is an unknown quantity at this point
well it was actually for a countdown script
i will find a way around it...
Extreme43 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.