简体   繁体   English

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

[英]echoing a jquery alert popup in php

I want to display a pop-up alert box upon some condition in PHP being satisfied. 我希望在满足PHP的某些条件下显示弹出警告框。 Something like: 就像是:

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

except using a custom jquery alert box. 除了使用自定义jquery警报框。 Is this possible?? 这可能吗??

I've tried something like: 我尝试过类似的东西:

  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>"; 

but it gives me a weird effect. 但它给了我一个奇怪的效果。 Not pop up. 没弹出来。

Thanks for your interest. 谢谢你的关注。

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;

Try something like this. 尝试这样的事情。 Where $bleh is the PHP variable with the message to show. $bleh是带有要显示的消息的PHP变量。

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

PHP is expecting " (double quotes) in any lines like rel="stylesheet" to be php code. You have to either use ' (single quotes) or escape them with a function around the echo like add_slashes. To get around this, I generally use single quotes around echo contents, so all the double quotes are not bothered. PHP期望“rel =”stylesheet“等任何行中的”(双引号)都是php代码。你必须使用'(单引号)或者使用add_slashes这样的函数转义它们。为了解决这个问题,我通常在回显内容周围使用单引号,因此所有双引号都不会被打扰。

echo 'all the "quoted" stuff here'; 在这里回复“所有”引用的“东西”;

Yes its possible.. 是的可能..

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

but i am not sure if its a good way to write whole function in echo, Instead Define Your Function in HTML some where in your Page and echo the function name here like i did above.. 但我不确定它是否是一个在echo中编写整个函数的好方法,而是在HTML中定义你的函数,在页面中的某些位置,并像我上面那样回显函数名称。

as noted before you need to escape your character or use it like this so no need to escape characters in here 如前所述,你需要逃避你的角色或像这样使用它,所以不需要在这里转义字符

<?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; ?>

Sure thing. 当然可以。 you probably want something around this: 你可能想要这个:

  <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 } ?>

basically, you don't actually need to 'echo' the script, as long as you're outputting it from a .php file and not an external .js file. 基本上,只要您从.php文件而不是外部.js文件输出脚本,就不需要“回显”脚本。 the script will show up as long as the condition passes. 只要条件通过,脚本就会显示出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM