簡體   English   中英

將帶有JSON的Jquery數組傳遞給PHP

[英]Pass Array FROM Jquery with JSON to PHP

嘿伙計們,我讀了一些其他帖子並嘗試了很多,但它仍然不適合我。 當我提醒數組我在第一個網站上得到所有結果但是在將數據發送到php后我得到一個空的結果。 有任何想法嗎?

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });

    var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
   window.location = "foo.php";     
   }); 
});

$data = json_decode($_POST['data']);

謝謝你

當我提醒房屋/公寓,花園/自然,運動/愛好時,我的陣列看起來像這樣,這是用戶可能選擇的幾個結果(來自復選框)。 但當我把它發布到PHP我什么都沒得到。 當我使用請求標記(鉻擴展)時,它向我顯示了一些內容:原始數據貓=%5B%22house +主題%22%2C%22平面+項目%22%5D

我也試過這種方式 - 仍然沒有結果

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
        alert(cats);

    $.ajax({

    type: 'POST',
    url: "foo.php",
    data: {cats:  JSON.stringify(cats)},
    success: function(data){
     alert(data);
     }
   });
  }); 
   window.location = "foo.php";
  }); 
}); 

PHP:

$json = $_POST['cats'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);
echo "<pre>";
print_r($data);

它讓我瘋狂

拿這個腳本: https//github.com/douglascrockford/JSON-js/blob/master/json2.js

並致電:

var myJsonString = JSON.stringify(yourArray);

所以現在你的代碼是

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });
   var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
 //  window.location = "foo.php";    // comment this by this page redirect to this foo.php
   });
   }); 

//如果你想要重定向,那么使用下面的代碼

-------------------------------------------------
     $.post('foo.php',{data:st},function(data){
       window.location = "foo.php"; 
     });
---------------------------------------------------

$data = json_decode($_POST['data']);
var ItemGroupMappingData = []

Or 

var ItemGroupMappingData =
{
"id" : 1,
"name" : "harsh jhaveri",
"email" : "test@test.com"
}

 $.ajax({
            url: 'url link',
            type: 'POST',
            dataType: "json",
            data: ItemGroupMappingData,
            success: function (e) {
// When server send response then it will be comes in as object in e. you can find data //with e.field name or table name
            },
            error: function (response) {
                //alert(' error come here ' + response);
                ExceptionHandler(response);
            }
        });

嘗試這個 :-

    $data = json_decode($_POST['data'], TRUE);

我認為你應該將“window.location =”移動到post回調,這意味着它應該等到帖子finshed然后重定向頁面。

$.post('foo.php', {
  data : st
}, function(data) {

   window.location = "foo.php";
});

暫無
暫無

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

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