2015-12-17 33 views
0

因此,首先,我將列出我參考的3段代碼。不從數據庫插入的javascript輸入字段中檢索值

HTML:

<div id='newuserpage'> 

    <h1><font color='white'>Create New User</font></h1> 

    <div id='userbody'> 
     <div id='userfields'> 
      <div id='usernamelabel'> 
       <font color='white'>Enter Username:</font> 
       <input id='createfield1' type='text'> 
      </div> 

      <div id='passwordlabel'> 
       <font color='white'>Enter Password:</font> 
       <input id='createfield2' type='password'> 
      </div> 

      <div id='usertype'> 
       <br> 
       <font color='white'>User Type:</font> 
       <select name="typeofuser" id='type'> 
        <option value="2">Student</option> 
        <option value="1">Teacher</option> 
       </select> 
       <br><br> 
      </div> 
     <button id = 'newuserbutton' onclick='createuser()'> 
      Submit 
     </button> 

     <button id = 'backtomenu' onclick = 'backtomenu()'> 
      Back to Teacher Menu 
     </button> 

JavaScript函數

function createuser() 
{ 
var newuser; 
var newpass; 

// check browser type 
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp = new XMLHttpRequest(); 
} else { // code for IE6, IE5 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 


newuser = document.getElementById("createfield1").value; 
newpass = document.getElementById("createfield2").value; 

xmlhttp.onreadystatechange = function() { 
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { 

     if (xmlhttp.responseText == '1') { //Connected properly 
      var createdmessage= document.getElementById('createdmessage'); 
      createdmessage.style.visibility= 'visible'; 
     } 
     if (xmlhttp.responseText == '2') { 
      var loginpage = document.getElementById('loginbox'); 
      loginpage.style.display = 'block'; 
      } 
     else { 
      // show error message 
      loginlabelerror.style.visibility = 'visible'; 
     } 
    } 
} 

// call php function 
xmlhttp.open("GET","createuser.php"); 
xmlhttp.send("username=" + encodeURIComponent(newuser) + "&password=" + encodeURIComponent(newpass)); 

//var createdmessage= document.getElementById('createdmessage'); 
//createdmessage.style.visibility= 'visible'; 

} 

PHP頁面來處理插入

<?php 

$q=$_GET["username"]; 
$p=$_GET["password"]; 
$t=$_GET["type"]; 

$host="xxx"; // Host name 
$username="xxx"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="xxx"; // Database name 

// Connect to server and select databse. 
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name",$link)or die("cannot select DB"); 


$sql="INSERT INTO users (username, password, type) VALUES('".$q."', '".$p."', 2)"; 

$retval = mysql_query($sql); 
if(! $retval) 
{ 
    echo 2; 
} 

    echo 1; 

mysql_close($link); 
?> 

我遇到的問題是,它從來沒有真正拉動值了createfield1和createfield2輸入區域。它只是將空字符串引入數據庫。

(忽略下拉菜單,這就是我們用身邊玩弄的東西,但並不真正需要的。)

至於我可以告訴大家,並可以在網上找到,我做了什麼應該工作?除非我完全誤解了某些東西,否則我很困惑,爲什麼它實際上沒有檢索輸入到輸入字段中的值。

爲了演示外部資源,我希望允許空白字段作爲實質上沒有任何操作的「直通」。眼下的問題是,即使有一個例子:

ASDF

進入兩個輸入字段,當按鈕調用函數來送過來的信息被插入,它不不要拉這些值,而是隻使用空白字符串。我不希望值檢查是需要的,因爲它不會使用空字符串,而是會認爲它應該在字段中提取值。

回答

0

稍微偏離主題,創建事件應該始終發佈,而不是獲取。 http://www.w3schools.com/tags/ref_httpmethods.asp(有更多有信譽的來源在那裏,但即使他們同意)

更重要的是...修改您發送調用..

xmlhttp.open("GET","createuser.php"); 

xmlhttp.open("GET","createuser.php?username=" + encodeURIComponent(newuser) + "&password=" + encodeURIComponent(newpass)); 

你仍然需要調用發送方法。

考慮做一個POST請求,而不是:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

頭號原因後/獲取,至少對我來說,是獲得能夠重新運行,沒有提示。 Post至少會提醒用戶,並且在創建帳戶時,會發出警告。

+0

嗯我理解使用Post vs. Get的重點,這只是我正在爲之前的學生撰寫的內容而努力的一個延續,我試圖擴展。以可讀性爲依據,我將它保持不變,但我可能會在某些時候改變它。 –

相關問題