1

How can i add php content or php variable inside Java-script alert box?! I tried to make it work few ways but it is only popping up a blank box rather than the contents of php variable.

Here is the code:

<script language="javascript">
    $(document).ready(function() {
        $("#a").blur(function() {           
            <?php $b = $_POST['a'];

            if(isset($_POST['update'])) {
            mysql_query("UPDATE tbl_travel set fld_a='".$_POST[$b]."' where fld_id = '".$_POST["id"]."' ") or die(mysql_error());
            } ?>

                alert (<?php $b ?>);
           });
    });
</script> 

Thank You for your Help :)

6
  • 1
    alert(<?php print($b); ?>); will do Commented Sep 7, 2013 at 10:00
  • Thanks shankar..it works very well :) Commented Sep 7, 2013 at 10:11
  • Cool.Yw. Happy Coding ! Commented Sep 7, 2013 at 10:12
  • possible duplicate of Passing a PHP variable to JavaScript Commented Sep 7, 2013 at 10:21
  • @dianuj: well i guess it is, but i tried to search the question for duplicate entry before posting the question!! Commented Sep 7, 2013 at 10:22

5 Answers 5

8

Change this

alert (<?php $b ?>);

to this

alert ('<?php echo $b; ?>');

You need to output the value of $b and add quotes inside the alert.

About PHP - echo

Sign up to request clarification or add additional context in comments.

7 Comments

Hey thanks for the answer totally missed using echo before $b. BTW need to put double quotes too alert ("<?php echo $b; ?>");
You don't need low reputation to accept answer, just to upvote. You get also +2 when you accept. :)
@Pooja, under the score of the answer were you can upvote and downvote there is a check mark, press there in the answer you want to accept and you get +2 reputation. You have some other old questions you asked were you can do that also :)
@Pooja, exactly! if you do that on your other questions you will get more reputation also
oh yes i found it! Thanks for informing about that...accepted your answer ^_^
|
3

Have you tried this?

alert ('<?php echo $b ?>');

Comments

2

I use it like this

$text="Example PHP Variable Message";
echo '<script type="text/javascript">alert("'.$text.'")</script>';

Comments

1

1.

<script>
  alert("</script><?php $r=5;echo $r;?> <script>")
</script>

you have to script off on when php start

Comments

0

This worked for me :

alert ("<?php echo $b; ?>");

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.