最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to add php content or variable inside javascript alert box?! javascript, php, alertbox - Stack Overflow

matteradmin15PV0评论

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

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 asked Sep 7, 2013 at 9:57 PoojaPooja 1701 gold badge1 silver badge10 bronze badges 6
  • 1 alert(<?php print($b); ?>); will do – Shankar Narayana Damodaran Commented Sep 7, 2013 at 10:00
  • Thanks shankar..it works very well :) – Pooja Commented Sep 7, 2013 at 10:11
  • Cool.Yw. Happy Coding ! – Shankar Narayana Damodaran Commented Sep 7, 2013 at 10:12
  • possible duplicate of Passing a PHP variable to JavaScript – M Khalid Junaid 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!! – Pooja Commented Sep 7, 2013 at 10:22
 |  Show 1 more comment

5 Answers 5

Reset to default 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

Have you tried this?

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

I use it like this

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

1.

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

you have to script off on when php start

This worked for me :

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far