将 php 变量回显到 innerhtml 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19883228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:10:16  来源:igfitidea点击:

echo a php variable into innerhtml

javascriptphpjquerymysqlinnerhtml

提问by fahimeh

I have a problem in my code, I want to put $meaning variable into innerhtml of my di,here is my code: searchinput.php:

我的代码有问题,我想将 $meaning 变量放入我的 di 的 innerhtml 中,这是我的代码:searchinput.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>

search.php:

搜索.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'];
?>

now I want to have this:

现在我想要这个:

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

how can I impelemt this?

我怎样才能做到这一点?

回答by mithunsatheesh

Yes. Start a script tag after the definition of $meaningand put the below code in it. Like.

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

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

Also Can't you think of directly echo ing the value of $meaninginside the divwithout even using javascript? like

难道你不能想到甚至不使用javascript就直接echo ing$meaning里面的值div吗?喜欢

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

You can also use the short form <?=$meaning?>.

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

回答by Amir Md Amiruzzaman

 <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>';

  }
  ?>