drzfz9995 2014-12-03 16:45
浏览 82
已采纳

如何通过函数将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 2014-12-06 19:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?