2013-03-26 67 views
0

我要顯示警報消息fails.so我的條件下使用下面的代碼來顯示信息警報消息將顯示在新窗口中

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

它工作正常,但問題是,它顯示在消息以空白頁作爲背景的新窗口。我需要在同一窗口中顯示此消息。請幫助找出一種方法來執行此操作。

在此先感謝。

+2

您需要在Javascript中的原始頁面中進行驗證檢查,而不是在提交頁面中。 – Barmar 2013-03-26 05:30:41

+0

你試過使用「回聲」? – 2013-03-26 05:30:49

+1

@HirenPandya爲什麼會有所作爲? – Barmar 2013-03-26 05:31:09

回答

1

顯示它的負載

print '<script type="text/javascript">'; 
    print 'window.onload = function(){' 
    print 'alert("You cannot leave field empty")'; 
    print '};' 
print '</script>'; 
+0

謝謝you.this解決了我的問題。 – 2013-03-26 06:54:11

0

結束代碼裏面的條件。然後把它放在哪裏,你要顯示的警報相同的頁面..

if (condition) { 
    print '<script type="text/javascript">'; 
    print 'alert("You cannot leave field empty")'; 
    print '</script>'; 
} else { 
    //Normal flow 
} 
0

我認爲你需要做這樣的事情在你的表格的同一頁也不要你的表單數據重定向到另一頁...(我以爲你是試圖通過該警報消息驗證表單):

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

這樣的警報會在同一頁面顯示被你的表格...如果驗證的子mit是錯誤的....