2013-10-04 35 views
0

我在使用AJAX向PHP文件發送關聯數組時遇到困難。有些事情我不清楚。這裏是我的代碼,從一個輸入標籤的形式,使數組,但我不知道如何發送它並解釋它在PHP中。帶有JSON響應並帶有對象的AJAX示例

<script type="text/javascript"> 
$(document).ready(function(){ 
    $(':submit').on('click', function() { // This event fires when a button is clicked 
     var theData = {}; 
     $(":input:not(:button)").each(
     function(index){ 
      var input = $(this); 
      theData[input.attr('name')] = input.val(); 
     } 
    ); 
     $.ajax({ // ajax call starts 
      url: "http://www.aberlechiropractic.com/meningealrelease/modifydoctors/modifydoctors3.php", 
      data: theData, 
      dataType: 'json', 
      success: function(data) 
      { 
       $('#wines').html(''); // Clear #wines div 
       $('#wines').append('Data Received: ' + data.name+' '+data.address + '<br/>'); 
      } 
     }); 
     return false; // keeps the page from not refreshing 
    }); 
}); 
</script> 

<body> 
    <form> 
    <input type="text" name="name" id="name" value="Jeff Aberle"/> 
    <input type="text" name="address1" id="address1" value="4710 East Broadway"/> 
    <button type="submit" name="updatedoctor" id="updatedoctor" value="all">All</button> 
    </form> 
</body> 

這裏是我的PHP代碼:

<?php 
$name = $_GET['name']; 
$address1 = $_GET['address1']; 
$array = array($button, $address1); 
print json_encode($array); 
?> 

啊,現在一切正常。我編輯了所有的代碼來完成這個工作。

<?php 
// Get value of clicked button 
$name = $_GET['name']; 
$address1 = $_GET['address1']; 
$array = array(
    "name" => $name, 
    "address" => $address1, 
); 
print json_encode($array); 
?> 

我也有ID =葡萄酒的股利。這是我忘了顯示的另一件事。然而,這是數據返回並顯示時沒有名稱的地方。

+0

實際上是回聲,不是打印。 –

+0

兩者都有效。儘管如此,Echo的系統資源較少。 – Christian

+2

'.live()'已棄用,並已在jQuery 1.9中刪除。改用'.on'。 – Barmar

回答

1

jQuery代碼收集的價值觀是正確的,雖然.serialize()將簡化它。

要檢索PHP中的值,就像表單正常提交一樣。他們在$_GET['name']$_GET['address1']theData只是包含該對象的Javascript變量的名稱,它不是發送給PHP的屬性名稱。

+0

沒有使用.serialize() – docaberle

+0

你的其他建議是一個很大的幫助。 – docaberle

0

對不起,我我的手機上,以便其短的答案,但使用序列

http://api.jquery.com/serialize/

$('form').on('submit', function(){ 
     $data = $(this).serialize(); 
     //send via ajax 
     return false; 
    }) 
0

要發送的數據:我假設你要發送的結果你的表單?要做到這一點,首先你需要在你的頁面上添加一個提交按鈕。這應該放在您的表單中以提交代碼。

其次,它看起來像你錯過了你在AJAX成功響應中引用的<div id="wine">,所以你需要添加它。

0

試試這個你要添加按鈕,您的形式來觸發動作:

<script type="text/javascript"> 

$(document).ready(function(){ 
    $('#submit').live('click', function() { 
     var theData = {}; 
     $(":input:not(:button)").each(
     function(index){ 
      var input = $(this); 
      theData[input.attr('name')] = input.val(); 
     } 
    ); 
     $.ajax({ 
      url: "http://www.aberlechiropractic.com/modifydoctors3.php", 
      data: theData, 
      dataType: 'json', 
      success: function(data) 
      { 
       $('#wines').html(''); // Clear #wines div 
       $('#wines').append('Data Received: ' + data + '<br/>'); 
      } 
     }); 
     return false; // keeps the page from not refreshing 
    }); 
}); 
</script> 

<body> 
    <form> 
    <input type="text" name="name" id="name" value="Jeff Aberle"/> 
    <input type="text" name="address1" id="address1" value="4710 East Broadway"/> 
    <input type="button" id="submit" value ="send"/> 
    </form> 
</body> 




<?php 
$button = $_GET['theData']; 
$array = array($button.name, $button.address1, $button.state); 
print json_encode($array); 
?> 
相關問題