2014-11-24 66 views
-1

我有一個搜索框,用戶輸入一個ISBN號碼,當這是一個div顯示來自Google Books API的信息。此信息是以標準格式從JSON文件中檢索的。我能夠在div中顯示標題,副標題,作者和描述,而不會有任何問題。JSON谷歌圖書和Ajax

然後我有一個「添加到資料庫」按鈕,應該把所有這些信息的數據庫,我的問題是,所有的字段從作者發送除了。發送作者姓名的單詞'數組'被髮送到數據庫。

我只能通過將[0]添加到我的Ajax數據對象的末尾來獲得作者姓名的唯一方式,但是這隻會發送第一個作者的名字(如果有3個作者,則會留下兩個出)。

更新 - 這似乎是JSON相關的,如果我改變我的Ajax數據不是「作者」其他任何東西,「industryIdentifiers」或「類別」它的工作原理。那是因爲它們包含一個列表並且不是單個字符串?

JS

$(document).ready(function() { 

    $('#submit').click(function(ev) { 
     ev.preventDefault(); 
     var isbn = $('#isbn_search').val(); //get isbn direct from input, no need for php 
     var url='https://www.googleapis.com/books/v1/volumes?q='+isbn; 
     $.getJSON(url,function(data){ 
      $('.result').empty(); 
      $.each(data.items, function(entryIndex, entry){ 
       var html = '<div class="results well">';      
       html += '<h3>' + entry.volumeInfo.title + '</h3>';     
       html += '<h5>' + entry.volumeInfo.subtitle + '</h5>'; 
       html += '<p>' + entry.volumeInfo.authors + '</p>';    
       html += '<p>' + entry.volumeInfo.description + '</p>'; 
       $('.result').append(html); 
      });       
     }); 
    }); 
}); 

AJAX

$.ajax({ 
    type: 'POST', 
    url: 'addIsbnScript.php', 
    data: { 
     'isbn' : isbn, 
     'title' : entry.volumeInfo.title, 
     'subtitle' : entry.volumeInfo.subtitle, 
     'authors' : JSON.stringify(entry.volumeInfo.authors), 
     'description' : entry.volumeInfo.description 
    }, 
    success: function() { 
    $("#add").prop('disabled', true); 
    } 
}); 

PHP

$isbn = $_POST['isbn']; 
$title = $_POST['title']; 
$subtitle = $_POST['subtitle']; 
$authors = $_POST['authors']; 
$decoded_authors = json_decode($authors); 
print_r($decoded_authors); 
$description = $_POST['description']; 

$query = $conn->prepare("INSERT INTO `isbn` (isbn_num,title,subtitle,authors,description) VALUES (?,?,?,?,?)"); 

$query->bind_param('issss', 

$isbn, 
$title, 
$subtitle,  
$decoded_authors, 
$description   
     ); 

JSON

"volumeInfo":{ 
"title":string, 
"subtitle":string, 
"authors":[ 
string 
], 
"publisher":string, 
"publishedDate":string, 
"description":string, 
"industryIdentifiers":[ 
{ 
"type":string, 
"identifier":string 
} 
], 
"pageCount":integer, 
"dimensions":{ 
"height":string, 
"width":string, 
"thickness":string 
}, 
"printType":string, 
"mainCategory":string, 
"categories":[ 
string 
], 
"averageRating":double, 
"ratingsCount":integer, 
"contentVersion":string, 
"imageLinks":{ 
"smallThumbnail":string, 
"thumbnail":string, 
"small":string, 
"medium":string, 
"large":string, 
"extraLarge":string 
}, 
"language":string, 
"previewLink":string, 
"infoLink":string, 
"canonicalVolumeLink":string 
}, 

請原諒的代碼,如果這是非常業餘的,因爲我是新來的JS,這僅僅是爲了個人發展。

+1

「作者」如何:JSON.stringify(entry.volumeInfo.authors),'?然後在服務器端解析並繼續? – 2014-11-24 10:33:30

+0

僅供參考:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – 2014-11-24 10:34:29

+1

感謝@Rory它現在發送以下格式的值[「Chris Cleave」,「 Philippa Gregory「,」Sarah Pekkanen - 所以我會看看解析JSON服務器端,我會盡快更新。「 – 2014-11-24 11:03:13

回答

1

您需要在客戶端將其解碼以將該陣列傳遞給服務器端。喜歡的東西:

$.ajax({ 
    type: 'POST', 
    url: 'addIsbnScript.php', 
    data: { 
     'isbn' : isbn, 
     'title' : entry.volumeInfo.title, 
     'subtitle' : entry.volumeInfo.subtitle, 
     'authors' : JSON.stringify(entry.volumeInfo.authors), 
     'description' : entry.volumeInfo.description 
    }, 
    success: function() { 
    $("#add").prop('disabled', true); 
    } 
}); 

然後在你的PHP文件(addIsbnScript.php)

<?php 
    $authors = $_POST['authors'];//'["Chris Cleave","Philippa Gregory","Sarah Pekkanen"]' 
    $decoded_authors = json_decode($authors); 
    print_r($decoded_authors);// Array ([0] => Chris Cleave [1] => Philippa Gregory [2] => Sarah Pekkanen) 
?> 

希望這有助於。乾杯

+1

感謝@Rory,我現在已經理解了我應該做的事情,儘管當我遵循你的代碼時,它仍然以[Chris Cleave],「Philippa Gregory」,「 Sarah Pekkanen - 我快到了! – 2014-11-24 12:21:03

+0

是的,在服務器端,您可以使用'json_decode'來獲取數組並將其用於您的需要。 – 2014-11-24 12:32:02

+0

感謝您的建議,但是當我使用更新的代碼(請參閱原始問題)時,「數組」這個詞會發送到數據庫?這是我的查詢問題嗎? – 2014-11-24 13:08:38