2011-05-09 5 views
0

FORM에서 입력을 읽고, 쿼리 문자열을 작성하여 PHP 파일로 보내고, 데이터를 데이터베이스에 삽입하고 성공 또는 실패를 나타내는 피드백을 보내도록 Ajax 함수를 작성했습니다 프로세스의Ajax XMLHttpRequest.responseText 조건부로 작업하기

문제는 Ajax XMLHttpRequest.responseText가 작동하지 않는다는 것입니다. 최종 사용자가 프로세스가 성공했는지 실패했는지 PHP 파일에서 피드백을 얻지는 못합니다.

이상하게도 Ajax 함수의 끝 부분에 alert() 함수를 넣으면 XMLHttpRequest.responseText가 마술처럼 작동하기 시작한다는 것을 알게되었습니다. 상상할 수 있듯이이 프로세스가 실행될 때마다 팝업을 생성하므로 성가신 편입니다.

누군가 이런 일이 일어나는 이유를 이해할 수 있도록 도와 줄 수 있습니까?

// FORM

class Teacher { 

    function AddATeacher() 
    { 


     ## Template for form with image ## 

     echo "<img src='../images/addaTeacher.png' alt='add A teacher icon' align='middle' width='90' height='90' />"; 
     echo " <b>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Add a Teacher </b> <br> <br>"; 



     ## Template for form with image ## 


     echo "<form name ='login' onsubmit='AddATeachers()' method='post'>";   

     echo "First Name: &emsp&emsp&emsp&emsp&emsp <input type='text' size='15' maxlength='15' 
       onclick=this.value='' id='teachers_first_name' 
       value='Teachers First Name' /> <br>"; 

     echo "Surname: &emsp&emsp&emsp&emsp&emsp&emsp<input type='text' size='15' maxlength='15' 
       onclick=this.value='' id='teachers_surname' 
       value='Teachers surname' /> <br>"; 

     echo "Number: &emsp&emsp&emsp&emsp&emsp &emsp <input type='text' size='15' maxlength='15' 
       onclick=this.value='' id='teachers_number' 
       value='Teachers number' /> <br>"; 

     echo "Email address: &emsp&emsp&emsp&emsp<input type='text' size='15' 
       maxlength='25' onclick=this.value='' id='teachers_email' 
       value='Email address' /> <br>"; 

     echo "Password: &emsp &emsp &emsp&emsp&emsp <input type='password' size='15'maxlength='25' 
       id='password' value='' /> <br>"; 

     echo "<hr/>"; 

     echo " 
      Sex: 
      <select value= 'Male' id='sex'> 
      <option> </option> 
      <option>Male</option> 
      <option>Female</option> 
      </select> "; 

     echo " 
     Assigned Class: 
      <select value= 'Choose a class' id='classes'> 
      <option> </option>   
      <option>P1 East</option> 
      <option>P1 West</option> 
      <option>P2 East</option> 
      <option>P2 West</option> 
      <option>p3 East</option> 
      <option>p3 West</option> 
      <option>p4 East</option> 
      <option>p4 West</option> 
      <option>p5 East</option> 
      <option>p5 West</option> 
      <option>p6 East</option> 
      <option>p6 West</option> 
      <option>p7 East</option> 
      <option>p7 West</option> 
      </select>"; 

     echo" 
     <hr/> 
     <input type='submit' value='Submit' /><br />"; 

     echo "</form>"; 

    } 
} 

// AJAX 기능 클래스 방법/인스턴스

<?php 
require ("database.php"); 
require ("teacher.class"); 

mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die(mysql_error()); 
//echo "Connected to MySQL<br /> <br />"; 
mysql_select_db(DATABASE) or die(mysql_error()); 
//echo "Connected to Database: " . DATABASE . " <br />"; 

$sql = ("SELECT * FROM Teachers_Table WHERE TFirstName='$_GET[tFirstName]' AND TSurName= '$_GET[tSurname]' "); 
$query = mysql_query($sql); 
$Sample = new Teacher; 
$Sample->AddATeacherInsert(); 

?> 

// 방법

를 호출

function AddATeachers(){ 
    var ajaxRequest; // The variable that makes Ajax possible! 

    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
    } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    // Create a function that will receive data sent from the server 
    ajaxRequest.onreadystatechange = function(){ 
     if(ajaxRequest.readyState == 4){ 
      document.getElementById("Ajax_response").innerHTML = ajaxRequest.responseText; 
     } 
    } 

    //alert ("Above ur variables "); 
    var teachers_first_name = document.getElementById("teachers_first_name").value; 
    //alert ("within ur variables 1111111111 "); 
    var teachers_surname = document.getElementById('teachers_surname').value; 
    var teachers_number = document.getElementById('teachers_number').value; 
    //alert ("within ur variables 222222222 "); 
    var teachers_email = document.getElementById('teachers_email').value; 
    var password = document.getElementById('password').value; 
    //alert ("Email retrieved: " + password + ""); 
    var tClass = document.getElementById('classes').value; 
    var gender = document.getElementById('sex').value;  
    //alert ("Class value: " + tClass + "");  
    //alert ("Exited ur variables "); 

    //alert ("Just before Query String..."); 
    var queryString = "?tFirstName=" + teachers_first_name + "&tSurname=" + teachers_surname + "&tNumber=" + teachers_number + "&tEmail=" + teachers_email + "&tPwd=" + password + "&tSex=" + gender + "&tClass=" + tClass ; 
    //alert ("After Query String..."); 

    ajaxRequest.open("GET","addATeacherInsert.php" + queryString, true); 
    ajaxRequest.send(null); 
    alert ("Process Complete"); 
} 

// addATeacherInsert.php

function AddATeacherInsert() 
    { 


     mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die(mysql_error()); 
     //echo "Connected to MySQL<br /> <br />"; 
     mysql_select_db(DATABASE) or die(mysql_error()); 
     //echo "Connected to Database: " . DATABASE . " <br />"; 

     ##### Insert into Teachers_Table ###### 
     mysql_query("INSERT INTO Teachers_Table 
     (TFirstName, TSurName, TNumber, TAddress, TPwd, TClassAssig, TSex) VALUES 
     ('$_GET[tFirstName]', '$_GET[tSurname]', '$_GET[tNumber]', '$_GET[tEmail]', 
     '$_GET[tPwd]', '$_GET[tClass]', '$_GET[tSex]') ") 

     or die(mysql_error()); 

     ##### Insert into login_details ###### 
     mysql_query("INSERT INTO login_details 
     (email_address, password) VALUES 
     ('$_GET[tEmail]', '$_GET[tPwd]') ") 

     or die(mysql_error()); 

     echo "<br>"; 
     echo "<center> <img src='../images/approved.png' alt='Enroll student icon' align='bottom' width='90' height='90' /> </center>"; 

     echo "<br><center> <b> <font color='blue'>" . $_GET[tFirstName] . " " . $_GET[tSurname] . "</b> has been successfully enrolled...</font></center><br />";  
} 
,

내가 MySQL과 많은 일을하지 않은 사전

답변

0

에 감사하지만, MySQL의 명령에 의해 발생 오류가 PHP 오류의 범위에 포함되지 않는 한, 당신은 단지 ajaxRequest.status를 해석 할 수있다. 다른 상태 코드 목록은 here입니다.