簡體   English   中英

從jQuery向PHP文件發送數據時的未定義索引

[英]Undefined index when sending data to PHP file from jQuery

我已經研究了這個問題(我讀了很多StackOverflow問題),但仍然找不到解決方案。

當我單擊鏈接時,我使用jQuery獲得了id HTML'a'屬性值,然后需要將其發送到php文件中,該文件將進行數據庫查詢。 我嘗試使用$.ajax$.post jQuery函數,但遇到相同的錯誤。

$.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");         
  });
});

$.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

我究竟做錯了什么?

編輯:我用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"); 
      });

您忘記在“數據”參數中添加密鑰。 而且我認為您可能僅在使用$ .post時才會看到此信息,因為$ .ajax是正確的。 評論$ .ajax,然后嘗試我建議的內容,或者評論$ .post,然后嘗試$ .ajax。

在這里,您有一個示例http://www.tutorialspoint.com/jquery/ajax-jquery-post.htm

PS:對不起,我還不能發表評論。

POST方法可以像

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

我不認為你做錯了什么。 與其嘗試使用某些key來獲取數據,不如,

嘗試var_dump($_POST)來查看是否獲取了任何POST數據

注意:也嘗試在ajax請求中設置datatype

編輯 :在您的請求中包括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應該可以。

嗯,正如我在評論中所說,我遇到了一個不使用JavaScript的解決方案。

我正在使用Twig制作模板,而我所做的就是為此而忘記了jQuery,並在showElement.php使用$_GET而不是$_POST ,因為我沒有發送敏感數據(密碼或用戶名),而只是追加了

?idElement=anyElementId

到showElement.php的路徑,例如,獲取以下URI:

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

在我的showElement.php中:

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

為了顯示信息,我顯示了showElement.twig模板:

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

在我的Twig模板(showElement.twig)中

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

這只是一種解決方案。 我不知道這是否是一個好方法,但是它有效。 我想我可以將對象直接保存在數組中,從而避免使用$ _SESSION變量,但是,如果需要在其他PHP文件中使用檢索到的元素,我可以這樣做。

如果有人找到其他解決方案,可以在此處發布。

謝謝大家的幫助:)

編輯鏈接到可以幫助我的線程: Ajax發送數據,但是php無法接收 (標題僅說明了我使用Firebug:P發現的內容)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM