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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题