Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :)

share|improve this question
1  
alert(<?php print($b); ?>); will do –  Shankar Damodaran Sep 7 at 10:00
 
Thanks shankar..it works very well :) –  Pooja Sep 7 at 10:11
 
Cool.Yw. Happy Coding ! –  Shankar Damodaran Sep 7 at 10:12
1  
Yeah Done. ^_^. –  Shankar Damodaran Sep 7 at 10:18
 
possible duplicate of Passing a PHP variable to JavaScript –  M Khalid Junaid Sep 7 at 10:21
show 2 more comments

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
 
Hey thanks for the answer totally missed using echo before $b. BTW need to put double quotes too alert ("<?php echo $b; ?>"); –  Pooja Sep 7 at 10:05
1  
You don't need low reputation to accept answer, just to upvote. You get also +2 when you accept. :) –  Sergio Sep 7 at 10:21
1  
@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 :) –  Sergio Sep 7 at 10:28
1  
@Pooja, exactly! if you do that on your other questions you will get more reputation also –  Sergio Sep 7 at 10:30
1  
oh yes i found it! Thanks for informing about that...accepted your answer ^_^ –  Pooja Sep 7 at 10:31
show 4 more comments

Have you tried this?

alert ('<?php echo $b ?>');
share|improve this answer
 
yes it works very well thank you :) –  Pooja Sep 7 at 10:29
add comment

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.