【问题标题】:echo a php variable into innerhtml将 php 变量回显到 innerhtml
【发布时间】:2013-11-21 21:21:11
【问题描述】:

我在我的代码有一个问题,我想把$意味着可变进我的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; !!!!

我怎样才能实现这个?

【问题讨论】:

  • 可能重复:“如何用 PHP 编写一个 hello world?”。

标签: javascript php jquery mysql innerhtml


【解决方案1】:

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

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

你难道不可以不使用javascript就直接在div中回显$meaning的值吗?喜欢

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

您也可以使用简写形式&lt;?=$meaning?&gt;

【讨论】:

    【解决方案2】:
     <div id="errorMessage"></div>
     <?php
      if (isset($_GET['q'])) {
      echo '<script type="text/javascript">
      document.getElementById("errorMessage").innerHTML = "User name or Password is incorrect";
      </script>';
    
      }
      ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多