Php 警报消息显示在新窗口中

Php 警报消息显示在新窗口中,php,javascript,alert,Php,Javascript,Alert,我想在条件失败时显示警报消息 print '<script type="text/javascript">'; print 'alert("You cannot leave field empty")'; print '</script>'; 打印“”; 打印“警报(“您不能将字段留空”); 打印“”; 它工作正常,但问题是,它是显示在新窗口中的空白页为背景的消息。我需要在同一窗口中显示此消息。请帮助找出一种方法来做到这一点 提前感谢。将代码包装在if条件中。然后将

我想在条件失败时显示警报消息

print '<script type="text/javascript">';
print 'alert("You cannot leave field empty")';
print '</script>'; 
打印“”;
打印“警报(“您不能将字段留空”);
打印“”;
它工作正常,但问题是,它是显示在新窗口中的空白页为背景的消息。我需要在同一窗口中显示此消息。请帮助找出一种方法来做到这一点


提前感谢。

将代码包装在if条件中。然后将其放在要显示警报的同一页面中

if (condition) {
  print '<script type="text/javascript">';
  print 'alert("You cannot leave field empty")';
  print '</script>'; 
} else {
   //Normal flow
}
if(条件){
打印“”;
打印“警报(“您不能将字段留空”);
打印“”;
}否则{
//正常流量
}
加载时显示

print '<script type="text/javascript">';
    print 'window.onload = function(){'
    print 'alert("You cannot leave field empty")';
    print '};'
print '</script>'; 
打印“”;
打印“window.onload=function(){”
打印“警报(“您不能将字段留空”);
打印'};'
打印“”;

我认为您需要在表单的同一页中执行类似操作,并且不要将表单数据重定向到另一页…(我认为您正在尝试通过警报消息验证表单):


您需要在原始页面而不是提交页面中进行Javascript验证检查。您是否尝试过使用“echo”?@HirenPandya为什么会有不同?此代码非常完美,可能是其他代码issues@Svetlio:
<?php
    if(isset($_POST) && count($_POST)){
        // do some validation for data...

        if(!valid){
               print '<script type="text/javascript">';
               print 'alert("You cannot leave field empty")';
               print '</script>';
        }

    }

    // as it's the same page of your form...all the code of form and other stuff goes           here....
    ?>


    <form action="" type="POST">
             <!-- all inputs and elements of form
    </form