我可以用xmlhttprequest获取什么?

时间:2010-01-27 05:59:06

标签: ajax

我很难得到答案......

我能用ajax从php文件中获取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>

并且包含/ forgot-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 <admin@mbira.me>' . "\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 joel@rattletree.com.</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>
?>

1 个答案:

答案 0 :(得分: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 <admin@mbira.me>' . "\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 joel@rattletree.com.</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有点奇怪 - 表单永远不会关闭,并且不清楚在首次显示表单时应显示哪些部分以及表单在表单之后应显示哪些部分提交。