2

index.php

<?php
$loginmessage = "this is message";
?>

    $(document).ready(function(){
        $("#sb-btn").click(function() {
          alert("<?=$loginmessage?>");
          return false;
        });

    });

i have a button i wanted to when the button is clicked , it alert the $loginmessage by java script , but when i clicked it noting happen and no error as well. what i have to do to make the php variable pass to javascript and alert it when user clicked.

5
  • any error in console ? Commented Oct 5, 2016 at 10:31
  • 1
    Is this code in a script tag ? Commented Oct 5, 2016 at 10:33
  • The php is fine. I'd say it's an error console issue as a guess.. Commented Oct 5, 2016 at 10:33
  • @shubam already told that answer,You may also check here. Commented Oct 5, 2016 at 10:38
  • Do you have a button with the id of sb_btn? Can you show us your HTML? Commented Oct 5, 2016 at 10:58

4 Answers 4

3

Don't forget to call jquery library. then try this-

<?php
$loginmessage = "this is message";
?>

    <script>
        $(document).ready(function(){
            $("#sb-btn").on("click", function() {
            var getvalue = '<?php echo $loginmessage; ?>';
            alert(getvalue);
          });
         });

    </script>
Sign up to request clarification or add additional context in comments.

7 Comments

noting happen as well , and also i did call jquery.
replace alert(getvalue); with alert("hello"); and check jquery working or not.
simple try: <script>alert("<?=$loginmessage?>");</script> than you can chk either jquery included or not. @babayaga
@devpro short tags are no guaranteed to be set to on, best to suggest <script>alert("<?php echo $loginmessage?>");</script>
@shubham715: your answer's first line is very important :)
|
0

Depending upon your PHP version and host, the short tags could be disabled. They recently came back into use with PHP7, which is still in Release Candidate state.
What this means is that you might have to replace the short tags with the full <?php echo use. Look in the generated HTML code on the client, to find out if this is the case.

One small thing I'd like to nitpick on, is your terminology. :P
You're not actually passing a variable to JavaScript from PHP, as much as you're using PHP to generate parts of said JS. It might seem trivial, but the difference is a crucial one. Passing the variable implies something being handled in the same program, which isn't the case at all here. The PHP code gets executed on the server side, which generates plain text output to the client. The client (web browsers) in turn parses said content, and figures out what kind of content the different part of this text actually is.

Having this clear mental separation in mind when developing web applications/sites will make it a lot easier for you to understand the details of how things work, and in turn make it easier for you to come up with clean and simple solutions that works as intended. :)

Comments

0

Try to write in console. Maybe your browser blocks alert messages. And-> are you see JavaScript errors in your console?

<script>
    $(document).ready(function(){
        $("#sb-btn").on("click", function() {
        var getvalue = '<?php echo $loginmessage; ?>';
        console.log(getvalue);
      });
     });

</script>

Comments

0

Create a function that is called when a button is clicked. Pass the message that you want to alert to that function. Hope this helps. Checkout the code in snippet.

function alertSomething(test){
  alert(test);
}
<button name="samplebtn" onclick="alertSomething('test alert')">Test Alert</button>

2 Comments

than y need for this https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js?
you dont need to include jquery since no jquery syntax is used. but if you want to do anything else using jquery in that function it will be more accurate.

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.