最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - echo a php variable into innerhtml - Stack Overflow

programmeradmin3浏览0评论

I have a problem in my code, I want to put $meaning variable into innerhtml of my di,here is my code: 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
    $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?

I have a problem in my code, I want to put $meaning variable into innerhtml of my di,here is my code: 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
    $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?

Share Improve this question asked Nov 9, 2013 at 21:19 fahimehfahimeh 511 gold badge2 silver badges5 bronze badges 1
  • Possible duplicate of: "How to write a hello world in PHP?". – dbanet Commented Nov 9, 2013 at 21:51
Add a ment  | 

2 Answers 2

Reset to default 8

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

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

Also Can't you think of directly echo ing the value of $meaning inside the div without even using javascript? like

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

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

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

  }
  ?>
发布评论

评论列表(0)

  1. 暂无评论