[英]How to pass a MySQL data from PHP to Javascript through a function
I'm making a program that shows several descriptions taken from a database (MySQL): 我正在制作一个程序,其中显示了从数据库(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. 其中$ row [“ descripcion”]是对描述的访问,而$ i是标识符,因为我正在使用循环。 That code generates a button that i must press to show the description.
该代码生成一个按钮,我必须按下该按钮才能显示说明。 The javascript function "cambiarBoton" is the one that do that changes:
javascript函数“ cambiarBoton”是可以更改的函数:
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. 好的,但是程序中有一个问题,这是我无法将说明传递给javascript中的函数,因此不起作用。 How can i do this?
我怎样才能做到这一点? (I must do this without using AJAX)
(我必须在不使用AJAX的情况下执行此操作)
我回答我想你问的是这个问题:
<?php echo "<script>var data = ".$row["description"].";</script>"; ?>
The problem was in the call of the function "cambiarBoton" in the code PHP, it should have been this: 问题出在代码PHP中的函数“ cambiarBoton”的调用中,应该是这样的:
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=\"cambiarBoton(" . $i . ", '" . $row["descripcion"] . "')\" />";
And then it works. 然后工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.