2015-11-07 22 views
4

這是我的整個頁面。我不知道什麼是錯的。這太令人沮喪了,因爲它非常簡單。爲什麼我的javascript DOM不起作用?

當我試圖走出價值,如果我的輸入字段它什麼都不回報。即使我在AJAX之前發出警報,它也不會寫出任何內容。我抄錄了我的輸入文件,它完全一樣。

怎麼了?

和registration.php:

<?php 
    if(isset($_POST["php_username"]) && isset($_POST["php_password"]) && isset($_POST["php_password2"]) && isset($_POST["php_email"])){ 
     $error = ""; 
     if(strlen($_POST["php_username"]) < 8){ 
      $error = $error."Username has to be at least 8 characters.<br>"; 
     } 
     if(strlen($_POST["php_password"]) < 8){ 
      $error = $error."password has to be at least 8 characters.<br>"; 
     } 
     if(strlen($_POST["php_username"]) > 16){ 
      $error = $error."Username can't be more than 16 characters.<br>"; 
     } 
     if(strlen($_POST["php_password"]) < 16){ 
      $error = $error."Password can't be more than 16 characters.<br>"; 
     } 
     if(strcmp($_POST["php_password"],$_POST["php_password2"])){ 
      $error = $error."Password confirmation falied.<br>"; 
     } 
     echo($error); 

    }else{ 
     echo(' 
      <div id="register-form"> 
       <form> 
        <h2 style="text-decoration: underline; margin-bottom: 10px;">Registration</h2><br><br> 
        Username:<br> 
        <input type="text" maxlength="16" id="username" value="asd" /><br> 
        Password:<br> 
        <input type="password" maxlength="16" id="password" /><br> 
        Confirm password:<br> 
        <input type="password" maxlength="16" id="password2" /><br> 
        E-mail address:<br> 
        <input type="email" id="email" /><br><br> 
        <input type="button" id="registration-submit" value="Signup"><br> 
        <div id="registration-response">       
        </div> 
       </form> 
      </div> 
      <script> 
       $("#registration-submit").click(function(){ 
        var username = document.getElementById("username").value; 
        var password = document.getElementById("password").value; 
        var password2 = document.getElementById("password2").value; 
        var email = document.getElementById("email").value; 

        $.post("registration.php",{ 
         "php_username": username, 
         "php_password": password, 
         "php_password2": password2, 
         "php_email": email 
        }, 
        function(data, status){ 
         $("#registration-response").html(data); 
         $("#registration-response").fadeIn(750); 
        }); 

       }); 
      </script> 
     '); 
    } 
?> 
+2

?因爲我希望它會給你$未定義的錯誤。 –

+0

是的,我做了index.php。這個頁面已被加載到與jQuery的div。該頁面沒有給出javascript錯誤。 –

+0

所以要麼包括jQuery文件或使用文件無處不在$ –

回答

0

只是把你的javascript echo語句之外,您是否包括jQuery的也把它包裏面$(document).ready();

 <?php 
      if(isset($_POST["php_username"]) && isset($_POST["php_password"]) && isset($_POST["php_password2"]) && isset($_POST["php_email"])){ 
       $error = ""; 
       if(strlen($_POST["php_username"]) < 8){ 
        $error = $error."Username has to be at least 8 characters.<br>"; 
       } 
       if(strlen($_POST["php_password"]) < 8){ 
        $error = $error."password has to be at least 8 characters.<br>"; 
       } 
       if(strlen($_POST["php_username"]) > 16){ 
        $error = $error."Username can't be more than 16 characters.<br>"; 
       } 
       if(strlen($_POST["php_password"]) < 16){ 
        $error = $error."Password can't be more than 16 characters.<br>"; 
       } 
       if(strcmp($_POST["php_password"],$_POST["php_password2"])){ 
        $error = $error."Password confirmation falied.<br>"; 
       } 
       echo($error); 

      }else{ 
       echo(' 
        <div id="register-form"> 
         <form> 
          <h2 style="text-decoration: underline; margin-bottom: 10px;">Registration</h2><br><br> 
          Username:<br> 
          <input type="text" maxlength="16" id="username" value="asd" /><br> 
          Password:<br> 
          <input type="password" maxlength="16" id="password" /><br> 
          Confirm password:<br> 
          <input type="password" maxlength="16" id="password2" /><br> 
          E-mail address:<br> 
          <input type="email" id="email" /><br><br> 
          <input type="button" id="registration-submit" value="Signup"><br> 
          <div id="registration-response">       
          </div> 
         </form> 
        </div>'); 


    <script> 

    $(document).ready(function(){ 
    $("#registration-submit").click(function(){ 
     var username = document.getElementById("username").value; 
     var password = document.getElementById("password").value; 
     var password2 = document.getElementById("password2").value; 
     var email = document.getElementById("email").value; 

     $.post("registration.php",{ 
      "php_username": username, 
      "php_password": password, 
      "php_password2": password2, 
      "php_email": email 
      }, 
     function(data, status){ 
      $("#registration-response").html(data); 
      $("#registration-response").fadeIn(750); 
      }); 

      }); 
     });      

    </script> 

      } 
     ?> 
+0

我不知道爲什麼應該這樣做。 $(document).redy()不需要,因爲我的腳本在html之後。外部或insed php相同,爲什麼它很重要? –