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

Pooja
  • 170
  • 1
  • 1
  • 10

5 Answers5

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

Sergio
  • 28,539
  • 11
  • 85
  • 132
  • Hey thanks for the answer totally missed using echo before $b. BTW need to put double quotes too alert (""); – Pooja Sep 07 '13 at 10:05
  • I would love to but i cant do that due to very less reputation...!! If you don't mind please rate my question i need to have 15points..Thanks :) – Pooja Sep 07 '13 at 10:19
  • 1
    You don't need low reputation to accept answer, just to upvote. You get also +2 when you accept. :) – Sergio Sep 07 '13 at 10:21
  • oh is it...!! please let me know from where can i accept the answer...as i am a noob here ;) Thanks for an upvote..appreciate it :) – Pooja Sep 07 '13 at 10:25
  • 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 07 '13 at 10:28
  • 1
    @Pooja, exactly! if you do that on your other questions you will get more reputation also – Sergio Sep 07 '13 at 10:30
  • 1
    oh yes i found it! Thanks for informing about that...accepted your answer ^_^ – Pooja Sep 07 '13 at 10:31
3

Have you tried this?

alert ('<?php echo $b ?>');
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
mkas
  • 351
  • 2
  • 8
2

I use it like this

$text="Example PHP Variable Message";
echo '<script type="text/javascript">alert("'.$text.'")</script>';
Danh
  • 5,916
  • 7
  • 30
  • 45
1

1.

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

you have to script off on when php start

TLama
  • 75,147
  • 17
  • 214
  • 392
jignesh
  • 11
  • 6
0

This worked for me :

alert ("<?php echo $b; ?>");
Praveesh P
  • 1,399
  • 2
  • 25
  • 42