drzfz9995
drzfz9995
2014-12-03 16:45

如何通过函数将MySQL数据从PHP传递给Javascript

I'm making a program that shows several descriptions taken from a database (MySQL):

echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
                                            . " onclick=cambiarBoton(" . $i . ", \"" . $row["descripcion"] . "\") />";

echo "<div class=\"entrada-descripcion\" id=\"Descripcion" . $i . "\">  </div>";

where $row["descripcion"] is the access to the description and $i is an identifier because i'm using loops. That code generates a button that i must press to show the description. The javascript function "cambiarBoton" is the one that do that changes:

function cambiarBoton(i, d){

                    if (document.getElementById("Boton"+i).value == "Mostrar Descripcion"){
                        document.getElementById("Boton"+i).value = "Ocultar Descripcion";
                        document.getElementById("Descripcion"+i).innerHTML = d;
                    }else if(document.getElementById("Boton"+i).value == "Ocultar Descripcion"){
                        document.getElementById("Boton"+i).value = "Mostrar Descripcion";
                        document.getElementById("Descripcion"+i).innerHTML = "";
                    }

                }

Ok, but there is a problem in the program and this is that i can't pass the description to the function in javascript and this doesn't works. How can i do this? (I must do this without using AJAX)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanguochi6194 duanguochi6194 7年前

    The problem was in the call of the function "cambiarBoton" in the code PHP, it should have been this:

    echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
                                            . " onclick=\"cambiarBoton(" . $i . ", '" . $row["descripcion"] . "')\" />";
    

    And then it works.

    点赞 评论 复制链接分享
  • duannaoben8011 duannaoben8011 7年前

    I answer to the question I think you're asking is:

    <?php echo "<script>var data = ".$row["description"].";</script>"; ?>
    
    点赞 评论 复制链接分享

相关推荐