www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] PHP-javascript interacion problem

  1. #1
    Join Date
    Dec 2011
    Posts
    3

    resolved [RESOLVED] PHP-javascript interacion problem

    Hi, i'm trying to develop this page but it seems to be a problem in the interaction between PHP and javascript. The deal is that the HTML and javascript codes are created dynamically by the PHP code and it is created properly as I've seem in the source code generated by de navigator (IE, chrome and Mozilla)... but they keep not working...

    the PHP code tha generates de 'function call' in javascript is:

    PHP Code:
    //Faz a busca pelos dados na tabela do ano selecionado
    $result mysql_query("SELECT * FROM Ano" $ano_tabela['Ano'] . " ORDER BY Placas ASC");
                
    for (
    $i=1$row mysql_fetch_array($result); $i++)
    {
         echo 
    '<tr><td class="border3">' $row['Placas'] . '</td><td class="border3">' $row['Status'] . '</td>';
                                    
         
    //Estrutura de repetição para escrever as semanas
         
    for($i=1$i<($num_semanas+1); $i++)
         {
              
    //Define a cor do background da celula de acordo com o conteúdo
              
    if($row['Week' $i] == "P")
              {
                   
    $color "#FFFF00";
              }
              elseif(
    $row['Week' $i] == "1" || $row['Week' $i] == "2" || $row['Week' $i] == "3")
              {
                   
    $color "#4D70B8";
              }
              elseif(
    $row['Week' $i] == "S")
              {
                   
    $color "#99FF66";                            
              }
              else
              {
                   
    //Se for a semana atual fica preenchida de laranja
               
    if($i==date("W") && $ano_tabela['Ano']==date("Y"))
               {
               
    $color "#FFB280";
               }
               else
               {
               
    $color "#ffffff";
               }
              }
                        
                        
                        
    $comment $i;
                        
                     
    //This generates a link tha calls the function updateDiv(a,b,c,d)
                        
    if($row['Week' $i] == "" || $row['Week' $i] == NULL)
                        {
                            echo 
    '<td id="link_td" style="background-color:' $color ';"><a href="#" onclick="updateDiv(' $i ',' $num_semanas ',' $row['Week' $i] . ',' $comment ')">-</a></td>';
                        }
                        else
                        {
                            echo 
    '<td id="link_td" style="background-color:' $color ';"><a href="#" onclick="updateDiv(' $i ',' $num_semanas ',' $row['Week' $i] . ',' $comment ')">' $row['Week' $i] . '</a></td>';
                        }
                    }
                    
                    echo 
    '</tr>';
                } 
    the problem is not html or javascript because it does't even call the function... the error happens before that...

    the thing is the '$row['Week' . $i]' part, because if I specify just the the parameter a and b of the function it works, like this: updateDiv(a,b).

    Even if in the place of '$row['Week' . $i]' I put a string it wont work, it just worked when used a random variable in there (like $i)...

    please, someone know what it could possibly be?

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    One thing. You have have a nested for loop both using the variable $i. So as soon as the second loops begins the first $i is reset to 1. The limit for the loop is a variable which will also not work. It good programming practice to use different variable names for counters in nested loops.

    Typical practice for a database query is to use a while loop. Mainly because unless you use function mysql_num_rows you won't know where to set the limit for the for loop.

  3. #3
    Join Date
    Dec 2011
    Posts
    3

    Unhappy not solved yet..

    Dasher, you are right about using the same variable, it was working because I was using $row = mysql_fetch_array($result) inside the first for loop... i've changed it alredy, but whatever, that wasn't the problem... testing the code again i've noticed that the function only works if I send numeric values to the javascript. For example, the function updateDiv(a,b,c,d) only would work if the values for a,b,c and d were numbers... I think that must be something related to variable type, but I can't imagine what it is...

    please.. I really need this to work, because it's indispensable to send strings to the javascript function...

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    You're right, it's because you're sending non-numeric data into your functions without quoting them properly in the echo section:

    Code:
    if ($row['Week' . $i] == "" || $row['Week' . $i] == NULL)
    {
    	echo '<td id="link_td" style="background-color:' . $color . ';"><a href="#" onclick="updateDiv(' . $i . ',' . $num_semanas . ',\'' . $row['Week' . $i] . '\',\'' . $comment . '\')">-</a></td>';
    }
    else
    {
    	echo '<td id="link_td" style="background-color:' . $color . ';"><a href="#" onclick="updateDiv(' . $i . ',' . $num_semanas . ',\'' . $row['Week' . $i] . '\',\'' . $comment . '\')">' . $row['Week' . $i] . '</a></td>';
    }
    I didn't quote $i and $num_semanas because they look like numbers.

  5. #5
    Join Date
    Dec 2011
    Posts
    3

    Talking solved

    you're right bionoid, after putting the \' around the variable it worked fine...

    thank you so much.. you kind saved me from beign fired.. hehe

    if same other problem apears then I'll post it here, but I don't think it will.. thanks again...

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    glad to hear

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles