2013-06-24 32 views
0

我有一個名爲form.php的文件,當我上傳到我的服務器,它支持PHP,表單不斷重複,並從不執行PHP代碼。我上傳了http://davidjahn.info/DavidJahnNet/form.php爲什麼我的表單不斷重複?它似乎是不運行的PHP

<?php 
$age=$_POST["age"]; 
echo $age; 
?> 

<html> 
    <head> 
     <title>Survey</title> 
     <style type="text/css"> 
      .welcome { 
       color: #FFFFFF; 
       text-align: center; 
       background-color:red; 
       font-weight:bold; 
       height: 50px; 
      } 
     </style> 
    </head> 
    <body> 
     <table style="height: 232px; width: 500px"> 
      <form method="post" id="survey" action="form.php"> 
       <tr> 
        <td> 
         <div class="welcome"><br>Welcome!</div> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <ul> 
          <br> 
          <br> 
          <div id="question1" style="display: inline;"> 
           <h3> 
            Are You 30+ Years Old? 
           </h3> 
           <div style="height: 10px"></div> 
           <input type="button" name="age" value="Yes" onclick="document.getElementById('question1').style.display='none';document.getElementById('question2').style.display='inline';"> 
           <input type="button" name="age" value="No" onclick="document.getElementById('question1').style.display='none';document.getElementById('question2').style.display='inline';"> 
          </div> 
          <div id="question2" style="display: none;"> 
           <h3> 
            Are You Looking for a Date? 
           </h3> 
           <div style="height: 10px"></div> 
           <input type="button" name="date" value="Yes" onclick="document.getElementById('question2').style.display='none';document.getElementById('question3').style.display='inline';"> 
           <input type="button" name="date" value="No" onclick="document.getElementById('question2').style.display='none';document.getElementById('question3').style.display='inline';"> 
          </div> 
          <div id="question3" style="display: none;"> 
           <h3> 
            Which Girl Is Your Type? 
           </h3> 
           <div style="height: 10px"></div> 
           <input type="image" src="http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834" alt="Submit" width="100px" value="type1" /> 
           <input type="image" src="http://jenntgrace.com/wp-content/uploads/2012/12/2.png" alt="Submit" width="100px" value="type2" /> 
           <input type="image" src="http://3.bp.blogspot.com/-Q_owQUxNjdQ/Te3ALCmT3oI/AAAAAAAAAnk/wv9r0b0MT-g/s1600/600px-MA_Route_3.svg_.jpg" alt="Submit" width="100px" value="type3" /> 
           <input type="image" src="http://4.bp.blogspot.com/-wY_qFr2pcAs/UCxhAayJ6oI/AAAAAAAAC6w/PgtLs2O_4g8/s1600/4.png" alt="Submit" width="100px" value="type4" /> 
          </div> 
         </ul> 
        </td> 
       </tr> 
      </form> 
     </table> 
    </body> 
</html> 
+3

有沒有PHP更換<input type="button" name="age" ...
。 –

+0

php的前四行是: –

+1

@TomvanderWoerdt:有,代碼格式不正確。 –

回答

7

PHP是一種服務器端語言。您需要運行PHP的服務器來編寫PHP頁面。你不能簡單地保存它們並在瀏覽器中打開它們。

看看像XAMPP這樣的東西,它可以讓您快速開發環境。

+0

我對這個問題的第一眼看法有相同的看法,但實際上這個問題更多地與HTML/JavaScript元素的結構有關。 –

+0

當我看到源代碼時,我拋棄了這個假設。找不到PHP標籤:) –

1

你在localhost上測試了這個嗎?我想你沒有安裝WAMP,並且您正在嘗試查看.php文件而無需使用php服務器&您必須知道php是服務器端語言。安裝的EasyPHP,然後你就可以.PHP

http://www.easyphp.org/

+0

我對這個問題的第一眼看法有相同的看法,但實際上這個問題更多地與HTML/JavaScript元素的結構有關。 –

-3

跑我不知道你想要說什麼。你能再詳細一點嗎?

另一方面,通過粗略猜測,它可能是您的瀏覽器。嘗試添加

<!DOCTYPE html> 

上述

<html> 

編輯:如果你談論的是PHP打印,而不是做什麼,它的意思做。那麼只是爲了讓你知道,PHP是一種服務器端腳本語言。它需要通過PHP引擎進行處理。你在共享的網絡主機上,或者你是否設置了自己的服務器?

0

請確保您在localhost服務器上運行您的代碼,因爲PHP只適用於服務器端而非客戶端。
如果你正在運行它正確地使用這個更好的做法:

if(isset($_POST['age'])){ 
$age=$_POST["age"]; 
echo $age; 
} 
+1

更好的做法是在輸出前逃避用戶提供的內容。而且,首先,不要拼錯$ _POST –

+0

感謝「拼寫正確」達米安! –

1

您應該使用按鈕提交。我已編輯你的代碼執行以下操作:有四種類型

  • 隱藏值來表示年齡的封面圖片

    • 按鈕,日期

    代碼:

    <?php 
        if (isset($_POST['age'])) { 
         $age=$_POST["age"]; 
         echo $age; 
        } 
    ?> 
    <html> 
        <head> 
         <title>Survey</title> 
         <style type="text/css"> 
         .welcome { 
          color: #FFFFFF; 
          text-align: center; 
          background-color:red; 
          font-weight:bold; 
          height: 50px; 
         } 
         .type1 { 
          background-image: url(http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834); 
         } 
         .type2 { 
          background-image: url(http://jenntgrace.com/wp-content/uploads/2012/12/2.png); 
         } 
         .type3 { 
          background-image: url(http://3.bp.blogspot.com/-Q_owQUxNjdQ/Te3ALCmT3oI/AAAAAAAAAnk/wv9r0b0MT-g/s1600/600px-MA_Route_3.svg_.jpg); 
         } 
         .type4 { 
          background-image: url(http://4.bp.blogspot.com/-wY_qFr2pcAs/UCxhAayJ6oI/AAAAAAAAC6w/PgtLs2O_4g8/s1600/4.png); 
         } 
         input[type=submit]{ 
          border: none; 
          width: 100px; 
          height: 100px; 
          background-size:cover; 
         } 
         </style> 
        </head> 
        <body> 
         <table style="height: 232px; width: 500px"> 
         <form method="post" id="survey" action="form.php"> 
          <input type="hidden" name="age" id="age"> 
          <input type="hidden" name="date" id="date"> 
          <tr> 
           <td> 
            <div class="welcome"><br>Welcome!</div> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <ul> 
            <br> 
            <br> 
            <div id="question1" style="display: inline;"> 
             <h3> 
              Are You 30+ Years Old? 
             </h3> 
             <div style="height: 10px"></div> 
             <input type="button" value="Yes" onclick="document.getElementById('question1').style.display='none';document.getElementById('question2').style.display='inline';document.getElementById('age').value='Yes'"> 
             <input type="button" value="No" onclick="document.getElementById('question1').style.display='none';document.getElementById('question2').style.display='inline';document.getElementById('age').value='No'"> 
            </div> 
            <div id="question2" style="display: none;"> 
             <h3> 
              Are You Looking for a Date? 
             </h3> 
             <div style="height: 10px"></div> 
             <input type="button" value="Yes" onclick="document.getElementById('question2').style.display='none';document.getElementById('question3').style.display='inline';document.getElementById('date').value='Yes'"> 
             <input type="button" name="date" value="No" onclick="document.getElementById('question2').style.display='none';document.getElementById('question3').style.display='inline';document.getElementById('date').value='No'"> 
            </div> 
            <div id="question3" style="display: none;"> 
             <h3> 
              Which Girl Is Your Type? 
             </h3> 
             <div style="height: 10px"></div> 
             <input type="submit" value="1" class="type1" name="type" /> 
             <input type="submit" value="2" class="type2" name="type" /> 
             <input type="submit" value="3" class="type3" name="type" /> 
             <input type="submit" value="4" class="type4" name="type" /> 
            </div> 
            </ul> 
           </td> 
          </tr> 
         </form> 
         </table> 
        </body> 
    </html> 
    
  • +0

    那爲什麼不張貼年齡? –

    +1

    @DavidJahn因爲按鈕不會發布值。您的「年齡」元素是按鈕。看到我的答案。 –

    +0

    @SimonAndréForsberg:的確如此。我已經在我的**更新**答案:)覆蓋了這個。 –

    0

    如果你正在尋找年齡段,你的Javascript和HTML沒什麼意義。

    您所擁有的age元素是兩個按鈕。

    你可以通過使用<input type="hidden" name="age" value="">然後使用Javascript修改這個值,當用戶點擊你的按鈕(如果你這樣做,改變你的按鈕的名稱屬性)來解決這個問題。

    或者你可以通過用單選按鈕替換按鈕來解決它。

    只需使用<input type="radio" name="age" ...>

    相關問題