从我通过ajax发出请求的文件中获取DOM

时间:2011-12-14 15:39:24

标签: php ajax dom post request

我用这个简化的例子来更好地解释这个问题。

在ajax下给出以下帖子请求:

$(document).ready(function()
{
   $(#submit).click(function()
   {
    $var = $("#result");
    $code = $("#code"); 
    $.post("ajax.php", {option : $var, code: $code}, function()
   {
     //Getting through the DOM could be useful if you want to analyse the answer coming from the ajax.php file
      var $DOMresponse = getElementByTagName("div")[0].firstChild.data; // I would want the correct code here because this is incorrect... this is ti give you an idea
      if($DOMresponse == "your code is correct")
      {
        $("#container1").fadeOut(400, function(){ $("#container1").html(result); });
        $("#container1").fadeIn();
      }
      elseif($DOMresponse == "your code is incorrect. Go again trough the procedure")
      {
        $("#container2").fadeOut(400, function(){ $("#container2").html(result); });
        $("#container2").fadeIn();
      }
       // In this second case I could fill the second container id="container2"
    });
  });
});

ajax.php示例:

<?php
 if($_POST['request']==1)
 { 
     if($_POST['code']==$user['code'])
     {
       ?><img src="...">
         <div>tomatoes</div>
         <div>potatoes</div>
         <div id="answer">your code is correct</div> <?php 
     } 
     else
     {
       ?><img src="...">
         <div>apples</div>
         <div>oranges</div>
         <div>your code is incorrect. Go again trough the procedure</div> <?php
      }
 }

我想知道如何通过ajax.php文件的DOM。

我该怎么做?感谢

2 个答案:

答案 0 :(得分:0)

在将结果插入页面之前,您是否需要这样做?如果是,则创建新元素并将结果插入其中。例如

var div = document.createElement('div');
var obj = $(div).html(response);

现在你有了一个带有dom元素的标准jQuery对象。

回应评论:

我很困惑。你想验证php或js中的代码吗?看起来您检查通过post发送的内容是否与$ user变量中定义的相同。因此验证代码是在php中完成的。在这种情况下,使用json作为响应并不简单。在php脚本中创建结果数组,其键状态设置为1或0.在post resposne中,您可以检查response.status == 0.

另外,看起来很奇怪你在php中进行一次验证,在响应后进行两次js验证。此外,如果您将响应设置为标准文本,那么您必须创建dom元素并将响应放在内部以便能够搜索它。

答案 1 :(得分:0)

你问的是如何在ajax.php文件中获得$('#code')的值。

以下是您正在做的事情:

  var $code = $('#code'); // jQuery object
  $.post('ajax.php', { code: $code });

这个问题是你将整个jQuery对象传递给ajax.php。您可能想要做的是传递$('#code')对象的值或html,如下所示:

  var code = $('#code').html(); // if it's a non-input element
  var code = $('#code').val(); // if it's an input
  $.post('ajax.php', { code: code });

然后在ajax.php文件中,你的$ _POST ['code']将等于代码的值(例如“ABC123”),你可以用它来与$ user ['code']或者你的任何东西进行比较想。

我希望我能正确理解这个问题。祝你好运。

编辑:我想我明白你现在所得到的。你想要做的是:

HTML:         

使用Javascript:

    var $option = 'request';
    var $code = $('#code').val();
    $.post('ajax.php', { option: $option, code: $code }, function(data) {
        if (data == 'valid') {
             // show valid code result here
        } else {
             // show invalid code result here
        }
    });

和ajax.php

    <? if ($_POST['option'] == 'request') {
          if ($_POST['code'] == '123ABC') {
              echo 'valid';
          } else {
              echo 'invalid';
          }
       }
    ?>

请注意,变量 data 来自$ .post参数中的函数(数据)部分。 data 变量包含来自ajax.php的响应(在我的示例中,它将是'有效'。)