javascript - 将 php 变量回显到 innerhtml

标签 javascript php jquery mysql innerhtml

我的代码有问题,我想将 $meaning 变量放入我的 di 的 innerhtml 中,这是我的代码: 搜索输入.php:

<form action = "search.php" method = "post">
    <input name="word" id="word" type="text" maxlength="255" />
    <label for="word">word:</label></br></br>
    <input type="submit" value="search" />

</form>
<div id = "meaning"> </div>

搜索.php:

<?php
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db("project",$db);
    $word = isset($_POST["word"]) ? $_POST["word"] : "";
    $result = mysql_query("select meaning from vocabulary where word = '$word'");
    $fetchmeaning = mysql_fetch_array($result); 
    $meaning = $fetchmeaning['meaning'];
?>

现在我想要这个:

document.getElementById('meaning').innerhtml = $meaning; !!!!

我该如何实现?

最佳答案

是的。在 $meaning 的定义之后开始一个脚本标签并将以下代码放入其中。 喜欢。

document.getElementById('meaning').innerHTML = "<?PHP echo $meaning; ?>" ;

难道你不能直接回显 $meaning 的值吗?在div里面甚至不使用 javascript?喜欢

<div id = "meaning"><?PHP echo $meaning; ?></div>

您也可以使用缩写形式 <?=$meaning?> .

关于javascript - 将 php 变量回显到 innerhtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883228/

相关文章:

javascript - 同位素过滤器和动画不起作用

php - 在 WordPress 中使用 WP_Query 进行自定义类别查询后丢失分页

php - 在这种情况下是使用接口(interface)还是抽象类更好?

javascript - 为什么我的 Ajax 对象返回时未定义?

jquery - 如何让我的按钮随着我的 .animate 宽度扩展而移动?

javascript - If/else if/else 错误 - 意外的标识符

javascript - 从另一个 Controller 调用 Angular Controller

php - jQuery 单击事件更改 php session 变量

javascript - 使用 Window.print 打印 HTML 页面

javascript - react 如何在按钮单击时重新呈现占位符