Jump to content

[SOLVED] Javascript and HTML


ToddAtWSU

Recommended Posts

If I have a value stored in a PHP variable and I need that value inside some Javascript code can I say something like:

 

<script language="javascript">
   ...
   var num = </script> <?php $value ?> <script language="javascript">
   ...
</script>

 

I am very new to Javascript and really don't know how well it and PHP get along. If this is not doable is there a way to get a php value into a javascript value? Thanks again!

Link to comment
https://forums.phpfreaks.com/topic/42885-solved-javascript-and-html/
Share on other sites

PHP doesn't communicate with javascript directly at all; however, assigning a value to a javascript variable based on a PHP query or value is definitely possible. Just remember this rule of thumb: javascript is entirely client based, so by the time the browser begins reading your javascript, your PHP is nowhere to be seen. So, if you're wanting to assign a PHP variable to a javascript variable when the page loads, try this:

<script type="text/javascript">
var num = '<?php echo $value; ?>';
</script>

I have a file I will read in via PHP when I load the page and based on what is read in from the file I want to store the different string variables into javascript variables. These variables will not change once a page is loaded won't change very often anyways, but reading from the file allows me to have flexibility to my page. So do I need to echo the value to store in a javascript variable? Thanks!

I am having problems with trying to integrate PHP and Javascript. I cannot see what I am doing wrong however. If you could please help me it would be great. Here is what my code looks like:

 

<html>
   <head>
      <title>Submit Top Ten</title>
   </head>

   <?php
      // Read in a file for the names of the teams
      $teams;
      $totalTeams;
      function getTeams( )
      {
         global $teams;
         global $totalTeams;
         $fp = fopen( 'leagueTeams.txt', 'r' );
         if( $fp )
         {
            for( $totalTeams = 0 ; !feof( $fp ) ; $totalTeams++ )
            {
               $team[$totalTeams] = fgets( $fp, 128 );
echo $totalTeams . "  " . $team[$totalTeams] . "<br>";
            }
         }
         fclose( $fp );
      }
   ?>

   <body bgcolor="#000000" text="#FFFF00" link="#FF9900" vlink="#CC9900">
      <font face="Arial">

         <script language="javascript">

            // variable to set the number of participants $totalTeams
            var par_num = '<?php global $totalTeams; echo $totalTeams; ?>';

            // variable to set the number of radio buttons per row
            var rad_num = 10;

            function reset_radio(row)
            {
               // loop through the radio buttons on the row to see which 
               // radio button is selected

               for( i = 1 ; i <= rad_num ; i++ )
               {
                  // if the radio button is selected,
                  // uncheck it and enable the other radio buttons
                  if( document.getElementById( 'par_' + row + '_' + i ).checked )
                  {
                     // unchecking the button
                     document.getElementById( 'par_' + row + '_' + i ).checked = false;
                     // turning the other radion buttons on
                     change_disable_status( i, false );
                     return;
                  }
               }
            }


            // this function will change the status of the radio buttons
            function change_disable_status(ra, st)
            {
               for( i = 1 ; i <= par_num ; i++ )
               {
                  if( !document.getElementById( 'par_' + i + '_' + ra ).checked )
                  {
                     document.getElementById( 'par_' + i + '_' + ra ).disabled = st;
                  }
               }
            }
         </script>
         <table>
            <tr>
               <td align="center"> </td>
               <td align="center">1</td>
               <td align="center">2</td>
               <td align="center">3</td>
               <td align="center">4</td>
               <td align="center">5</td>
               <td align="center">6</td>
               <td align="center">7</td>
               <td align="center">8</td>
               <td align="center">9</td>
               <td align="center">10</td>
            </tr>
            <?php
               getTeams( );
               global $totalTeams;
               global $teams;
echo count( $teams ) . "<br>";
               for( $i = 0 ; $i < $totalTeams ; $i++ )
               {
                  echo "<tr>";
                  echo "<td align=\"center\">" . $teams[$i] . "</td>";
                  for( $j = 0 ; $j < 10 ; $j++ )
                  {
                     echo "<td align=\"center\"><input type=\"radio\" name=\"par_" 
                          . $i . "\" id=\"par_" . $i . "_" . $j . 
                          "\" onmousedown=\"reset_radio(\'" . $i . 
                          "\');\" onclick=\"change_disable_status(\'" . $j . 
                          "\', true);\" /></td>";
                  }
                  echo "<td align=\"center\"><a href=\"#\" onclick=\"reset_radio(\'" . $i . 
                       "\'); return false;\">Reset</a></td>";
                  echo "</tr>";
               }
            ?>
         </table>
      </font>
   </body>
</html>

 

Whenever I call the function getTeams( ) and I have those echos I get:

 

n  name

n+1  name1

n+2  name2

...

 

It prints out exactly what I would think. But then when I print out the size of the array with the count function, it prints 0. Then when I print my table of labels and radio buttons I get the line of

 

  1  2  3  4  ...  10

  o  o  o  o  ...  o    Reset

  ...

 

No team names appear to the left of the radio buttons. I cannot click multiple radio buttons in one row so that is behaving correctly. The javascript functionality is supposed to only allow me to choose one radio button per column. My final product should be to allow only one chosen radio button per row and per column. The rest of the radio buttons in the column are supposed to grey out but unfortunately they do not. Also the Reset link/button is supposed to clear any radio buttons selected in that row. But none of the javascript functionality seems to not work. What am I doing wrong in trying to combine javascript and PHP? Thanks!

I've changed it so I call getTeams( ) before writing the <script> portion that way the teams are read in. The variable $totalTeams holds the correct value but the array $teams is not holding anything for once I leave the getTeams( ) function. I still cannot get the javascript functions to be called either. Any ideas? Thanks!

I have gotten my javascript and PHP to communicate. I had some \' and \; inside my big long echo and realized I didn't need those like when I need \" and once I took those extra \ out and the javascript functionality all works. The only thing that is not working is trying to access my array later. If you have any advice on that I would appreciate it. Thanks!

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.