2010-01-27 83 views
0

我很難得到這個答案...我可以用xmlhttprequest獲取什麼?

我能夠從一個PHP文件與Ajax的HTML元素?我有一種感覺,我試圖做一些我不應該做的事情。

我使用:

. 
. 
. 
<tr> 
     <td colspan="3" style="font-size:11px;"><a href="javascript:void(0)" onclick="getData('includes/forgot-passwordajax.php', 'targetDiv')">Forgot Password?</a></td> 
    </tr> 


    </table></td> 
    </tr> 
</table> 
</form> 
    <div id="targetDiv"> 
    </div> 

,包括/忘記-passwordajax.php與表單數據的表格,等難道這工作?

編輯:這是腳本:

<script language = "javascript"> 
     var XMLHttpRequestObject = false; 

     if (window.XMLHttpRequest) { 
     XMLHttpRequestObject = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { 
     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     function getData(dataSource, divID) 
     { 
     if(XMLHttpRequestObject) { 
      var obj = document.getElementById(divID); 
      XMLHttpRequestObject.open("GET", dataSource); 

      XMLHttpRequestObject.onreadystatechange = function() 
      { 
      if (XMLHttpRequestObject.readyState == 4 && 
       XMLHttpRequestObject.status == 200) { 
       obj.innerHTML = XMLHttpRequestObject.responseText; 
      } 
      } 

      XMLHttpRequestObject.send(null); 
     } 
     } 
    </script> 

PHP:

<?php   
<div class='a_login'> 
<table class="tabbody"> 
    <tr class="tprowbgcolor"> 
    <td class="title">Retrieve Password</td></tr> 
</table> 
<center><BR><BR> 
    <table cellpadding="0" cellspacing="0"> 
    <form name='loginForm' action="forgot-password.php" method="POST"> 
    <tr><td><img src="<?=SITE_URL?>/images/top-bg.jpg"></td></tr> 
    <tr> 
    <td class="a_td_color" align="center" valign="top"> 


    if(isset($_POST['formSubmitted'])) 
    { 
    $email = addslashes(trim($_POST['e_address'])); 

    if($email=="") { 
     echo '<p>Just kidding? Ehh! <a href="forgot-password.php">try again</a></p>'; 
    } else { 
     $query = "SELECT `Password`, `UserName` FROM users WHERE `Email`='$email'"; 

     $result = mysql_query($query); 

     if(mysql_num_rows($result)>0) { 
     $row = mysql_fetch_assoc($result); 

     $password=$row['Password']; 
     $username=$row['UserName']; 
     $to = $email; 
     $headers = 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
     $headers .= 'To: '.$username.' <'.$email.'>' . "\r\n"; 
     $headers .= 'From: Joel Laviolette <[email protected]>' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
     $subject = "Here is your password"; 
     $message = '<p>Hello '.$username.',</p><p>Your password is: '; 
     $message .= '<strong>'.$password.'</strong><br><br>'; 
     $message .= 'You can login here: <a href="'.SITE_URL.'/login2.php">'.SITE_URL.'/login2.php</a></p>'; 
     $message .= '<p>With Love and gratitude,<br>Joel Laviolette</p>'; 
     if(mail($to, $subject, $message, $headers)) { 
      echo '<p>Your password has been sent. Please check your email.</p>'; 
     } else { 
      echo '<p>Email could not be sent! Please contact me directly at [email protected]</p>'; 
     } 
     } else { 
     echo '<p>Email not found! <a class="external" href="forgot-password.php">try again</a></p>'; 
     } 
    } 
    } else { 

    <table border='0'> 
    <tr> 
     <td colspan="3"> 
    <p>Email Address<br /> 
    <input type="text" name="e_address" value="" size="20"></p></td> 
    </tr> 
    <tr> 
     <td colspan="3" align="center"><input type="submit" value="Submit" name="submit"><input type="hidden" name="formSubmitted" value='1'></td> 
    </tr> 
    <tr> 
     <td colspan="3">&nbsp;</td> 
    </tr> 
    </table> 
?> 
+0

您應該能夠獲取數據並根據它更改HTML - 這就是AJAX的用途。但是你可以發佈'getData()'的定義嗎?因爲它不清楚在這裏做了什麼? – 2010-01-27 06:01:45

+0

完成。我仍然不相信我的php文件有任何形式的正確格式,但我不願意發佈它 - 哈哈 - 我沒有寫它。 – Joel 2010-01-27 06:04:43

+0

您發佈的所有代碼_looks_正確,發佈PHP。可能有個問題, – 2010-01-27 06:08:31

回答

1

從您發佈的PHP,它看起來就像你在<?php ... ?>標記之間的HTML代碼。這是不正確的。您應該將PHP代碼放在標籤中,並將HTML留在外面。最後你還有一個未封閉的支撐。在你的情況,這應該是(未經測試):

<div class='a_login'> 
<table class="tabbody"> 
    <tr class="tprowbgcolor"> 
    <td class="title">Retrieve Password</td></tr> 
</table> 
<center><BR><BR> 
    <table cellpadding="0" cellspacing="0"> 
    <form name='loginForm' action="forgot-password.php" method="POST"> 
    <tr><td><img src="<?=SITE_URL?>/images/top-bg.jpg"></td></tr> 
    <tr> 
    <td class="a_td_color" align="center" valign="top"> 

<?php   
    if(isset($_POST['formSubmitted'])) { 
     $email = addslashes(trim($_POST['e_address'])); 

     if($email=="") { 
     echo '<p>Just kidding? Ehh! <a href="forgot-password.php">try again</a></p>'; 
     } else { 
     $query = "SELECT `Password`, `UserName` FROM users WHERE `Email`='$email'"; 

     $result = mysql_query($query); 

     if(mysql_num_rows($result)>0) { 
      $row = mysql_fetch_assoc($result); 

      $password=$row['Password']; 
      $username=$row['UserName']; 
      $to = $email; 
      $headers = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
      $headers .= 'To: '.$username.' <'.$email.'>' . "\r\n"; 
      $headers .= 'From: Joel Laviolette <[email protected]>' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 
      $subject = "Here is your password"; 
      $message = '<p>Hello '.$username.',</p><p>Your password is: '; 
      $message .= '<strong>'.$password.'</strong><br><br>'; 
      $message .= 'You can login here: <a href="'.SITE_URL.'/login2.php">'.SITE_URL.'/login2.php</a></p>'; 
      $message .= '<p>With Love and gratitude,<br>Joel Laviolette</p>'; 
      if(mail($to, $subject, $message, $headers)) { 
      echo '<p>Your password has been sent. Please check your email.</p>'; 
      } else { 
      echo '<p>Email could not be sent! Please contact me directly at [email protected]</p>'; 
      } 
     } else { 
      echo '<p>Email not found! <a class="external" href="forgot-password.php">try again</a></p>'; 
     } 
     } 
    } else { 
?> 

    <table border='0'> 
    <tr> 
     <td colspan="3"> 
    <p>Email Address<br /> 
    <input type="text" name="e_address" value="" size="20"></p></td> 
    </tr> 
    <tr> 
     <td colspan="3" align="center"><input type="submit" value="Submit" name="submit"><input type="hidden" name="formSubmitted" value='1'></td> 
    </tr> 
    <tr> 
     <td colspan="3">&nbsp;</td> 
    </tr> 
    </table> 

<? } ?> 

順便說一句,你可以去到頁面(includes/forgot-passwordajax.php)在瀏覽器中看到錯誤消息PHP給出。

編輯:此外,HTML是有點古怪 - 形式永遠不會關閉,而且目前還不清楚哪些部分應該顯示當第一次顯示的形態和形式已提交後應該顯示。

+0

好的。謝謝。我確實設法至少在我的主頁上看到ajax輸出。現在我可以開始調整這些其他表單元素,而不是將我重定向到其他頁面等。再次感謝! – Joel 2010-01-27 07:09:10

相關問題