Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to pass $post; a variable created in a mysql query, to javascript function showDiv. Currently this doesnt work.

$post = $row['id'];
?>


<script type="text/javascript">
  function showDiv() {  
    var note = "<?php echo $post ?>";
    document.getElementById("<?php echo $post; ?>").style.display = "inline";      
  }
</script>

<?php 
  $addnote = '<input type="button" value="addnote" onclick="showDiv()"><div id="'.$postid.'"  style="display:none;" class="'.$postid.'"> WELCOME</div>';

But if I change $post to have a html value e.g

$post = '11';

then this code works.

I am novice in javascript so please be gentle, Any help is greatly appreciated. Thank you.

share|improve this question
3  
What does $post equal when you use $post = $row['id'];? Could it be that it can't find the row id from the dB? – Albzi Aug 20 '13 at 15:49
3  
What does var_dump($row['id']) show? – Mike Brant Aug 20 '13 at 15:50
1  
If you're outputting Javascript, the best thing to do is to look at the actual javascript your script prints. What BeatAlex and Mike Brant are hinting at, is that your variable is either empty or contains some value that "breaks" your javascript string. – Jeroen van den Broek Aug 20 '13 at 15:55
1  
There is only one chance to load PHP variables into JS, and that is at page-load when PHP is run. If this value is changing after that, then you'll need to use AJAX. – tymeJV Aug 20 '13 at 15:56
    
What is $postid? Is there any difference between $post and $postid? Also, avoid numeric-only ids if possible. – Petr R. Aug 20 '13 at 15:59

If you are in a loop, I think JS doesn't like redeclare your function "showDiv()". Try this :

$post = $row['id'];
$addnote = '<input type="button" value="addnote" onclick="showDiv('.$post.')"><div id="'.$post.'"  style="display:none;" class="'.$post.'"> WELCOME</div>';

And the javascript NOT in the loop :

<script type="text/javascript">
  function showDiv(note) {  
    document.getElementById(note).style.display = "inline";      
  }
</script>
share|improve this answer

check your $row['id'] if it's returning something.

<?php echo $row['id']; ?>

or check your source code. your code might look something like

<script type="text/javascript">
  function showDiv() {
    var note = "";
    document.getElementById("").style.display = "inline";
  }
</script>
share|improve this answer
1  
A similar comment has already been made. – Ja͢ck Aug 20 '13 at 16:09

Assuming that: $post = $row['id'] has a value. You want var note to have a value of a string. JS wraps strings in single or double quotes. so wrap <?php echo $post ?> in a string like this:

var note = "'"+<?php echo $post ?>"'";

This will prepend and append the quotes around the $post value so that JS can recognize it as a string.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.