2012-05-03 27 views
0

好:我有HTML代碼,如:的JavaScript的getElementById()問題得到數據

<form method="post"> 
    <ul> 
    <li> 
     <label for="username">Username</label> 
     <input type="text" id="username" size="30" onblur="checkUser()" /> 
     <div id=user></div> 
    </li> 
    <li> 
     <label for="email">Email</label> 
     <input type="text" name="email" id="email" size="30" /> 
    </li> 
    <li> 
     <label for="first_name">First Name</label> 
     <input type="text" name="first_name" id="first_name" size="30" /> 
    </li> 
    <li> 
     <label for="last_name">Last Name</label> 
     <input type="text" name="last_name" id="last_name" size="30" /> 
    </li> 
    <li> 
     <label for="cell">Cell</label> 
     <input type="text" name="cell" id="cell" size="30" /> 
    </li> 
    <li> 
     <label for="street">Street</label> 
     <input type="text" name="street" id="street" size="30" /> 
    </li> 
    <li> 
     <label for="city">City</label> 
     <input type="text" name="city" id="city" size="30" /> 
    </li> 
    <li> 
      <label for="zip">Zip</label> 
     <input type="text" name="zip" id="zip" size="30" /> 
    </li> 
    <li> 
      <label for="password">Password</label> 
     <input type="password" name="password" id="password" size="30" /> 
    </li> 
    <li><label for="password2">Repeat Password</label> 
     <input type="password" name="password2" id="password2" size="30" /> 
    </li> 
    <li> 
      <label for="submit"></label> 
     <button type="submit" name="submit" id="submit">Register</button> 
    </li> 
    <ul> 
</form> 

與JS功能

function checkUser() 
{ 
    var userID=document.getElementById('username').value; 
    alert(userID); 
    createRequest(); 
    var url= "userCheck.php?username="+userID; 
    //alert(url); 
    request.open("GET",url,true); 
    request.onreadystatechange=updatePage; 
    request.send(null); 
} 

我卡在document.getElementById('username').value;。警報顯示值爲username

我試圖用這個函數從文本框中取值並將它傳遞給userCheck.php查詢用戶名是否已被使用。

任何幫助?

更新 - 代碼

<? include "heading.php" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<?php 
function mailer($to,$username,$hash){ 

     $subject = "Test mail"; 
     $message = "http://www.me.com/verify.php?username=$username&hash=$hash"; 
     $from = "[email protected]"; 
     $headers = "From:" . $from; 
     mail($to,$subject,$message,$headers); 
     echo "Mail Sent."; 

    } 

if(isset($_POST['submit'])){ 
     if($_POST['password']==$_POST['password2'] && !empty($_POST['password']) && !empty($_POST['username']) && !empty($_POST['email']) && !empty($_POST['last_name'])){ 
      if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])){ 
       $msg = 'The email you have entered is invalid, please try again.'; 
       }else{ 
        require_once "UserManagement.php"; 
        $hash = md5(rand(0,1000)); 
        $usman=new UserManagement(); 
        $usman->createUser($_POST['username'],$_POST['email'],$_POST['first_name'],$_POST['last_name'],$_POST['cell'],$_POST['street'],$_POST['city'],$_POST['zip'],md5($_POST['password']),$hash); 
        mailer($_POST['email'],$_POST['username'],$hash); 
        $msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has been send to your email.'; 
        header('Location: http://www.me.com/me'); 
        } 
        }else{ 
         $msg = 'Form Data Incorrect'; 
         sleep(10); 
         header('Location:http://www.me.com/me'); 

        } 
} 

?> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Registration</title> 
<meta name="keywords" content="" /> 
<meta name="description" content="" /> 
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" /> 




<script language="javascript" type="text/javascript"> 
function clearText(field) 
{ 
    if (field.defaultValue == field.value) field.value = ''; 
    else if (field.value == '') field.value = field.defaultValue; 
} 
</script> 

<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" /> 

<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript" src="js/ddsmoothmenu.js"> 

/*********************************************** 
* Smooth Navigational Menu- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com) 
* This notice MUST stay intact for legal use 
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code 
***********************************************/ 

</script> 

<script type="text/javascript"> 

ddsmoothmenu.init({ 
    mainmenuid: "templatemo_menu", //menu DIV id 
    orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v" 
    classname: 'ddsmoothmenu', //class added to menu's outer DIV 
    //customtheme: ["#1c5a80", "#18374a"], 
    contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"] 
}) 

</script> 

</head> 
<body id="sub_page"> 
<div id="templatemo_wrapper"> 
    <div id="templatemo_top"> 
     <div id="templatemo_login"> 
     <?include "loginbox.php"?> 
     </div> 
    </div> <!-- end of top --> 

    <div id="templatemo_header"> 
     <div id="site_title"><h1><a href="http://www.spaladonna.com">SPA LaDonna</a></h1></div> 
     <div id="templatemo_menu" class="ddsmoothmenu"> 
      <? include "mainmenu.php" ?> 
     </div> <!-- end of templatemo_menu --> 
    </div> <!-- end of header --> 
    <? include "checkuser.php"?> 
    <div id="templatemo_main"> 
     <div id="templatemo_content"> 
      <div class="col_fw"> 
       <h1>Register User</h1> 

       <fieldset> 
        <legend>New User Signup</legend> 
        <form method="post"> 
         <ul> 

           <li><label for="username">Username</label> 
           <input type="text" id="username" size="30" onblur="checkUser()" /> 
           <div id=user > </div> 
           </li> 

           <li><label for="email">Email</label> 
           <input type="text" name="email" id="email" size="30" /> 
           </li> 

           <li><label for="first_name">First Name</label> 
           <input type="text" name="first_name" id="first_name" size="30" /> 
           </li> 

           <li><label for="last_name">Last Name</label> 
           <input type="text" name="last_name" id="last_name" size="30" /> 
           </li> 

           <li><label for="cell">Cell</label> 
           <input type="text" name="cell" id="cell" size="30" /> 
           </li> 

           <li><label for="street">Street</label> 
           <input type="text" name="street" id="street" size="30" /> 
           </li> 

           <li><label for=city">City</label> 
           <input type="text" name="city" id="city" size="30" /> 
           </li> 

           <li><label for="zip">Zip</label> 
           <input type="text" name="zip" id="zip" size="30" /> 
           </li> 

           <li><label for="password">Password</label> 
           <input type="password" name="password" id="password" size="30" /> 
           </li> 

           <li><label for="password2">Repeat Password</label> 
           <input type="password" name="password2" id="password2" size="30" /> 
           </li> 

           <li><label for="submit"></label> 
           <button type="submit" name="submit" id="submit">Register</button> 
           </li> 

         <ul> 
        </form> 
       </fieldset> 
<script type="text/javascript"> 
var request=null; 
function createRequest() 
{ 
    try{ 

     request= new XMLHttpRequest(); 

     }catch(tryMicrosft){ 
      try{ 
       request= new ActiveXObject("Msxml2.XMLHTTP"); 

      }catch(otherMicrosoft){ 
       try{ 

         request=new ActiveXObject("Microsoft.XMLHTTP"); 
       }catch(failed){ 

        request=null; 
       } 
      } 
     } 
     if(request==null) 
     alert("Error creating request object!"); 
//else alert("Successfully created"); 
} 

function checkUser() 
{ 
    var userID=document.getElementById('username').value; 
    alert(userID); 
    createRequest(); 
    var url= "userCheck.php?username="+userID; 
    //alert(url); 
    request.open("GET",url,true); 
    request.onreadystatechange=updatePage; 
    request.send(null); 
} 
function updatePage() 
{ 
    if(request.readyState==4) 
    replaceText('user',request.responseText); 
} 

function clearText(el) { 
    if (el != null) { 
    if (el.childNodes) { 
     for (var i = 0; i < el.childNodes.length; i++) { 
     var childNode = el.childNodes[i]; 
     el.removeChild(childNode); 
     } 
    } 
    } 
} 


function replaceText(el, text) { 
    if (el != null) { 
    el=document.getElementById(el); 
    clearText(el); 
    //setTimeout("alert("HELLO")", 4000); 


    var newNode = document.createTextNode(text); 

    el.appendChild(newNode); 




    } 
} 

</script> 
       </div> 
<div class="col_fw_last">     

       <div class="col_w300 float_l"> 

       </div> 

       <div class="col_w300 float_r"> 

       </div> 

       <div class="cleaner"></div> 
      </div> 
     </div> <!-- end of content --> 

     <div id="templatemo_sidebar"> 
     <h4>Categories</h4> 
      <div class="sidebar_box"> 
       <ul class="templatemo_list"> 
        <li><a href="#">Aliquam pretium porta odio</a></li> 
        <li><a href="#">Integer sit amet erat sit</a></li> 
        <li><a href="#">Nunc elementum mollis</a></li> 
        <li><a href="#">Quisque dictum pharetra</a></li> 
       </ul> 
      </div> 

      <div class="cleaner h40"></div> 

      <h4>Cool Links</h4> 
      <div class="sidebar_box"> 
       <ul class="templatemo_list"> 
        <li><a href="http://www.templatemo.com" target="_parent">Free CSS Templates</a></li> 
        <li><a href="http://www.flashmo.com/page/1" target="_parent">Flash XML Galleries</a></li> 
        <li><a href="http://www.flashmo.com" target="_parent">Flash Templates</a></li> 
        <li><a href="http://www.webdesignmo.com/blog" target="_parent">Web Design Resources</a></li> 
       </ul> 
      </div> 
      <div class="cleaner h40"></div> 
       <h4>Latest Updates</h4> 
       <div class="news_box"> 
        <a href="#">Donec eu orci dolor</a> 
        <p>Pellentesque in nunc quam. Sed molestie ultricies ante, vel scelerisque diam varius sed. </p> 
       </div> 
       <div class="news_box"> 
        <a href="#">Aliquam bibendum vulputate</a> 
        <p>Auris nisl mi, aliquet ac lobortis ut, tincidunt in tortor nisl vitae lectus dapibus pellentesque.</p> 
       </div> 
       <div class="news_box"> 
        <a href="#">Nunc viverra vestibulum </a> 
        <p>Nunc viverra vestibulum magna, nec dignissim turpis rhoncus tincidunt. Donec ac nibh arcu. </p> 
       </div> 
       <div class="news_box"> 
        <a href="#">Fusce placerat ultrices</a> 
        <p>Integer ac ultricies nisi. Curabitur velit ante, porttitor ac feugiat a, porta ut lectus.</p> 
       </div> 
     </div> <!-- end of sidebar --> 

     <div class="cleaner"></div> 
    </div> <!-- end of main --> 
</div> <!-- end of wrapper --> 

<div id="templatemo_footer_wrapper"> 
    <div id="templatemo_footer"> 
     Copyright © 2012 <a href="#"></a> 
     <div class="cleaner"></div> 
    </div> 
</div> 

</body> 
</html> 
<? ob_flush(); ?> 

和header.php中的其餘部分

<? ob_start(); ?> 
<?php 
session_start(); 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 
if(isset($_POST['login'])){ 
     require_once "UserManagement.php"; 
     $usman=new UserManagement(); 
     if($usman->authenticate($_POST['username'],md5($_POST['password']))){ 
       header('Location: http://www.me.com') 
     }else { 
       echo "Authentication error"; 
       echo '<a href="index.php">Home</a>';    
     } 
     die(); 
} 
?> 

我可以張貼所有的菜單和東西太多,如果需要的話

+0

什麼不起作用?你爲什麼期望警報值在引號中? –

+0

我wouldnt - 我只是想解釋,那些報價並不真的存在 - 即時通訊嘗試從用戶輸入字段中獲取數據,它一直說輸入的數據是用戶名 –

+2

這個詞我把這個小提琴:http: //jsfiddle.net/jLtSv/。我沒有看到任何奇怪的 - '價值'是一個空字符串,因爲它應該是。值爲''用戶名'的唯一原因是,如果你真的在輸入框中輸入了''用戶名''。 – Imp

回答

0

是 - 所以這是肯定我的一個ID 10噸錯誤 - 我打電話loginbox.php,並沒有認識到,它被作爲佔位符

的值設定爲ID =「用戶名」作爲實際單詞「用戶名」將表單上的一個改爲id ='newuser'等等 - 繁榮都很好

1

嘗試用這種

//replace 

var userID=document.getElementById('username').value; 

//for this 

var userID = document.forms[0]['username'].value; 
+0

與形式[0]我根本得不到數據 –

+0

Nevermind - 仍然得到用戶名作爲結果 –

+0

它爲我工作,把ID放在您的表單喜歡這個。