在php中回显jquery警报弹出窗口

时间:2013-05-13 22:41:28

标签: php jquery

我想在满足PHP的某些条件下显示一个弹出警告框。类似的东西:

  echo "<script type="text/javascript"> alert('bleh'); </script>";

除了使用自定义jquery警报框。这可能吗?

我尝试过类似的事情:

  echo "<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
});
</script>"; 

但它给了我一个奇怪的效果。没弹出。

感谢您的关注。

6 个答案:

答案 0 :(得分:3)

echo <<<EOD
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
       $("#dialog-message").dialog({
           modal: true,
           buttons: {
               Ok: function() {
                  $( this ).dialog( "close" );
               }
           }
       });
    </script>
EOD;

答案 1 :(得分:0)

尝试这样的事情。其中$bleh是带有要显示的消息的PHP变量。

<script type="text/javascript">
$(document).ready(function(){
   var dlg = $('<div>').text(<?php echo $bleh ?>);
   $(body).append(dlg);
   dlg.dialog(
      modal: true
   );
});
<script>

答案 2 :(得分:0)

PHP期望“rel =”stylesheet“这样的任何行中的”(双引号)都是php代码。你必须使用'(单引号)或者使用像echo_slashes这样的回显函数来转义它们。这个,我通常在echo内容周围使用单引号,因此所有的双引号都没有被打扰。

回复'所有“引用的”这里的东西';

答案 3 :(得分:0)

是的,可能......

echo "<script type=\"text/javascript\">alert('bleh');</script>";

但是我不确定它是否是一个在echo中编写整个函数的好方法,而是在HTML中定义你的函数,在页面中的某些位置,并像在上面那样回显函数名称。

答案 4 :(得分:0)

如前所述,你需要逃避角色或像这样使用它,所以不需要在这里转义字符

<?php if (condition == true) : ?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
 });
 </script>
<?php endif; ?>

答案 5 :(得分:0)

当然可以。你可能想要一些东西:

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   //check for PHP condition. Script will only be output on condition pass.
   <?php if(condition == true){ ?>
   <script>
   $(function() {
    $("#dialog-message").dialog({
     modal: true,
     buttons: {
     Ok: function() {
        $(this).dialog("close");
      }
    }
   });
 });
 </script>
 <?php } ?>

基本上,只要您从.php文件而不是外部.js文件输出脚本,就不需要'回显'脚本。只要条件通过,脚本就会显示出来。