从jQuery向PHP文件发送数据时未定义索引


Undefined index when sending data to PHP file from jQuery

我已经研究了这个问题(我读了很多StackOverflow问题),但我仍然找不到解决方案。

我得到id HTML 'a'属性值与jQuery当我点击链接,然后我需要把它发送到一个php文件,将做一个数据库查询。我尝试使用$.ajax$.post jQuery函数,我得到同样的错误。

With $.post

var idElement;
$(document).ready(function(){
  $('.linkElement').click(function(){
      idElement= $(this).attr('id');
      console.log(idElement); // Shows id value on console
      $.post("showElement.php", idElement).done(function (){
         alert("All went good"); 
      });
      $(document.body).load("showElement.php");         
  });
});

With $.ajax

var idElement;
    $(document).ready(function(){
        $('.linkElement').click(function(){
          idElement= $(this).attr('id');
          console.log(idElement); // Shows id value on console
          $.post("showElement.php", idElement).done(function (){
             alert("All went good"); 
          });
            $.ajax({
              url: "showElement.php",  
              type: "POST",
              data: {'idElement':idElement}
            });
        });
});

当我在showElement.php文件中使用$_POST['idElement']时,我得到以下内容:

Notice: Undefined index: idElement in C:'xampp'htdocs'path'to'showElement.php on line 28

我做错了什么?

EDIT:我用Firebug测试了这个,它说没问题。Firebug控制台显示的数据显示正确,但是PHP文件没有显示在浏览器中(var_dump返回它应该有的值)。

有什么想法吗?

修改

      $.post("showElement.php", idElement).done(function (){
         alert("All went good"); 
      });

         $.post("showElement.php", {'idElement': idElement}).done(function (){
         alert("All went good"); 
      });

您忘记在'data'参数中添加键。我想你可能只有在使用$时才会看到这个。Post是因为$。Ajax是正确的。注释$。ajax的一个,并尝试我建议你或注释$。发布一个并尝试$.ajax.

这里有一个例子http://www.tutorialspoint.com/jquery/ajax-jquery-post.htm

附言:对不起,我还不能作为评论来回答。

我认为你没有做错任何事。而不是试图获取数据使用一些key

试试var_dump($_POST),看看你是否得到任何POST的数据

注意:尝试在ajax请求中也设置datatype

EDIT:在您的请求中包含contentType。检查下面的

  $.ajax({
          url: "showElement.php",  
          type: "POST",
          contentType:"application/x-www-form-urlencoded",
          dataType: 'html', //or json whatever data that you return from server side
          data: {'idElement':idElement}
        });

我认为设置contentType应该工作

POST方法可以像

$.post("showElement.php", 
{idElement: idElement},
 function (result) {
   alert("All went good"); 
});

嗯,正如我在评论中所说,我遇到了一个不使用JavaScript的解决方案。

我正在使用Twig模板,我所做的是忘记jQuery为此使用$_GET而不是$_POSTshowElement.php,因为我没有发送敏感数据(密码或用户名),我只是附加了

?idElement=anyElementId

到showelment .php的路径,例如,获取以下URI:

http://localhost/Project/showElement.php?idElement=1

In my showelment .php:

$anElement= ElementQuery::create()->findPk($_GET['idElement']); // Retrieve and element with Propel
$_SESSION['element'] = $anElement; // Save the element in $_SESSION['element'] variable

为了显示信息,我显示了我的showElement。树枝模板:

$twig->display("showElement.twig", array('element') => $_SESSION['element']); // Display Twig template and pass arguments in an array

在我的Twig模板(showelment . Twig)

<!-- Link to be clicked -->
    <a id="{{element.getId}}" class="linkElement" href="./../Project/showElement.php?idElement={{element.getId}}">{{element.getTitle}}</a>

这只是一个解决方案。我不知道这是不是一个好方法,但它确实有效。我认为我可以直接将对象保存在数组中,避免使用$_SESSION变量,但是,我这样做是为了防止我需要在其他PHP文件中使用检索到的元素。

如果有人找到另一个解决方案,可以在这里发布。

谢谢大家的帮助:)

EDIT链接到线程,帮助我:Ajax发送数据,但php不接收(标题描述了我发现的Firebug: p)