Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo a php variable into innerhtml

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?

like image 790
fahimeh Avatar asked Apr 10 '26 12:04

fahimeh


2 Answers

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?>.

like image 179
Mithun Satheesh Avatar answered Apr 13 '26 02:04

Mithun Satheesh


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

  }
  ?>
like image 30
Amir Md Amiruzzaman Avatar answered Apr 13 '26 01:04

Amir Md Amiruzzaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!