2014-08-30 52 views
0

我想要它,以便當所有輸入字段填充時,按鈕會自動再次啓用,但我發現很難這樣做。我使用PHP來檢查字段和HTML以啓用/禁用按鈕Div浮動不正確或與標題內聯

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" href="main.css"> 
</head> 
<body> 
    <?php 
     $required = array('FName', 'LName', 'Email', 'Subject', 'Comments'); 

     $error = false; 
     foreach($required as $field) { 
      if (empty($_POST[$field])) { 
       $error = true; 
      } 
      else{ 
       $error = false; 
      } 
     } 

     if ($error) { 
      } else {  
     } 

    ?> 
    <div class="ContentBox" style="width:auto;"> 
    <h1 class="Title">Contact Us</h1> 
     <div class="contact_form"> 
      <form method="post" action="ThankYou.php"> 

       <div id="contact_left"> 
      <p>First Name: </p> 
        <input class="TextBox" type="text" name="FName"> 
      <p>Last Name: </p> 
        <input class="TextBox" type="text" name="LName"> 
      <p id="HText">Email: (?)<span id="HSpan">Only For Verification Purposes</span></p> 
       <p><input class="TextBox" type="email" name="Email"></p> 


      <p style="text-align:center;"><input class="Button_s" name="submit" type="submit value="Submit" id="submit" disabled="disabled"></p> 

        </div> 
       <div id="contact_right"> 
      <p>Subject: </p> 
        <input class="TextBox" type="text" name="Subject"> 
      <p>Comments: </p> 
        <textarea id="content" rows="10" cols="40" name="Comments"></textarea> 
        </div> 

      </form> 
     </div> 
    </div> 
    <?php 
     function check_input($data) 
      { 
       $data = trim($data); 
       $data = stripslashes($data); 
       $data = htmlspecialchars($data); 
       return $data; 
      } 
    ?> 
</body> 
</html> 
+0

PHP只運行在服務器上,而不是瀏覽器上 - 你需要做的JavaScript做你想要做的事。 – Blazemonger 2014-08-30 14:21:31

+0

運行時檢查您需要jQuery/javascript來啓用/禁用按鈕。使用if(空(字段))來檢查這些字段是否爲空或填充。 – 2014-08-30 14:24:29

回答

1

PHP代碼將只在服務器端進行處理。只有在用戶點擊提交按鈕後,PHP才能處理輸入。

您應該在這種情況下使用JavaScript。

1

當所有字段填充時,只能使用HMTL和CSS來啓用「提交」按鈕:http://jsfiddle.net/du97k26u/

HTML:

<form method = "post"> 
    <input type = "text" placeholder = "First Name" required /> 
    <input type = "text" placeholder = "Last Name" required /> 
    <input type = "submit" value = "Process" /> 
</form> 

CSS:

form > input { 
    display: block; 
} 

form > input:invalid ~ input[type = "submit"] { 
    display: none; 
}