2012-08-27 43 views
-2

我有一些PHP邏輯呈現的以下表單。表單渲染正常;你可以看到文本輸入和提交按鈕等。爲什麼這些<form>不被FF和Chrome識別?

在IE中,表單按預期工作。第一種形式爲'index.php?subscribe = go',第二種形式爲'index.php?unsub = go',但在FF和Chrome中,單擊提交按鈕將重新加載頁面(不會進入表單操作)。我沒有檢查其他瀏覽器。

我在Firebug中發現<form>標記甚至沒有存在在Firefox的頁面上。這很奇怪,檢查出來:

else 
    { 
     echo '<div class="subs_main">'; 
     if (isset($_GET['subscribe'])) 
     { 
      if ($_GET['subscribe'] != 'go') 
      {?> 
       Subscribe to <b>Bella Blog</b> for specials, sales, news and more! 
       <br /> 
       <form action="index.php?subscribe=go" method="post" name="subscribe_form" onsubmit="return checkForm();"> 
       Name: <input type="text" name="name" size="15" /> 
       <br /> 
       Email: <input type="email" name="email" size="20" /> 
       <br /> 
       <input type="submit" value="subscribe!" name="submit" /> 
       </form> 
       <p class="unsub">You can <a href="index.php?unsub">unsubscribe</a> at any time</p> 
      <?php 
      } 
      else 
      { 
       // subscribe user 

      } 
     } 
     elseif (isset($_GET['unsub'])) 
     { 
      if ($_GET['unsub'] != 'go') 
      {?> 
       Sorry to see you go! You can <a href="index.php?subscribe">re-subscribe</a> at any time! 
       <br /> 
       <form onsubmit="return checkForm2()" name="unsub_form" method="post" action="index.php?unsub=go"> 
       Email: <input type="email" name="email" size="20" /> 
       <br /> 
       <input type="submit" value="unsubscribe" name="submit" /> 
       </form> 
      <?php 
      } 
      else 
      { 
       // process unsubscription HERE 

      } 
     } 
     echo '</div>'; 
    } 

這是JS的表單驗證(可忽略我想是因爲它在IE和評論這個劇本的時候了,我得到了相同的結果):

<script type="text/javascript" language="javascript"> 
function checkForm() 
{ 
    var regex = /^[a-zA-Z0-9._-][email protected][a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; 
    var form = document.forms.subscribe_form; 
    var name = form.name.value; 
    var email = form.email.value; 

    if (name == '' || email == '') 
    { 
     alert('You must enter both your name and email address!'); 
     return false; 
    } 
    else if (!email.match(regex)) 
    { 
     alert('You must enter a valid email!'); 
     return false; 
    } 
    return true; 
} 
function checkForm2() 
{ 
    var form = document.forms.unsub_form; 
    var email = form.email.value; 

    if (email == '') 
    { 
     alert('You must enter an email address!'); 
     return false; 
    } 
    return true; 
} 
</script> 
+0

我們可以看到上面的網頁的網址。 – 2012-08-27 00:22:35

+0

它在我的本地機器上,一個WAMP服務器。再次,它在IE中運行良好,但有一些錯誤或者東西,使FF和Chrome不呈現窗體標籤。 – khaverim

+0

無法調試我無法看到,上傳或? – 2012-08-27 00:29:06

回答

0

<form>標籤不存在?除非您有將輸出定製到USER_AGENT的代碼,否則任何將給定的一組GET/POST輸入傳遞給頁面的瀏覽器應該會得到相同的輸出。當然,他們可能會渲染頁面並以(潛在地)不同的方式響應事件,但源代碼應該是相同的。

發佈頁面源代碼,我們可以看看問題可能是什麼。

+0

我查看了源代碼並找到了問題併發布了答案;感謝Adam。 – khaverim

1

如果您在表單中使用POST方法,所有參數都應該通過INPUT html元素(即action =「index.php?subscribe = go」和action =「index.php?unsub = go」是錯誤的)。

+0

發佈數據與表單提交中URL傳遞的GET數據分開處理(即它們不是'錯誤')。我現在就開始工作併發布了答案。 – khaverim

+0

使用HTTP「post」方法,表單數據集包含在表單主體中併發送給處理代理。恕我直言,你不應該混合得到和發佈方法。 – freedev

+0

我同意不使用兩者的'一般做法',但您可以通過HTTP傳遞GET和POST數據,沒有任何問題。它們是獨立的實體,可以這樣訪問。在我的情況下,它可以非常有用,同時使用POST和GET數據。 – khaverim

-1

這是一個奇怪的WAMP問題。我在文件中產生了一些其他的PHP代碼,這些代碼產生了WAMP錯誤(但是在現場沒有錯誤),我一直忽略它,因爲它沒有意義。 '未定義索引'就是它的名稱,當你使用$ _POST [例子]而不是$ _POST ['example']調用一個PHP變量時會出現錯誤。最荒謬的。

因此,WAMP吐出一堆HTML(錯誤<table> s),它與我的頁面上的其他表單混淆在一起。 IE可以處理那裏搞砸的形式,我的表單(顯示問題)正常工作,而FF/Chrome不能。

希望這可以幫助別人。

+0

它不會幫助任何人,除非你發佈生成的html。 – Alohci

+0

不,這個概念很簡單,並且可以理解爲沒有WAMP的HTML吐出亂七八糟:頁面上的其他形式+ WAMP未定義的索引錯誤(在活動站點上不會發生這種錯誤)=表單呈現正確的問題。 – khaverim

相關問題