1

I want to echo the following html code on my page. But the onclick event doesn't work when I do this. the $senduser has the correct value

$table1="<div id='sendmsg' class='row'>
<div class='col-lg-6'>
<textarea id='msg' cols='40' placeholder='Write your message here...'></textarea>
</div><div class='col-lg-2' >
<button type='button' onClick='sendmsg(".$senduser.")' class='btn btn-success' value='send' id='msgbtn'>Send to</button></div></div>";

echo $table1;

my javascript function:

function sendmsg(senduser){
  alert(senduser);
  .
  .
  .
}

Alert doesn't show anything.

1
  • Corrected some syntax, added block ticks around what was needed, and removed unnecessary "please" and "thank" from the question :). Commented Mar 5, 2015 at 17:55

3 Answers 3

1

$senduser is probably a string and as you don't quote it, it is interpreted as a (undefined...) variable in javascript.

You need to quote it:

...
<button type='button' onClick='sendmsg(\"".$senduser."\")' class='btn btn-success' ...
 //                                    ^^ here        ^^ and here
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure you're wrapped the javaScript in a script tag

<script>
   function sendmsg(senduser){
          alert(senduser);
          .
          .
          .
    }
</script>

Comments

0

Try this

<?php 
$senduser = '1';
$table1="<div id='sendmsg' class='row'>
<div class='col-lg-6'>
<textarea id='msg' cols='40' placeholder='Write your message here...'></textarea>
</div><div class='col-lg-2' >
<button type='button' onClick='sendmsg(".$senduser.")' class='btn btn-success' value='send' id='msgbtn'>Send to</button></div></div>";

echo $table1; ?>

<script>
function sendmsg(senduser){
  alert(senduser); 
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.