사용자가 ISBN 번호를 입력 할 수있는 검색 창이 있습니다. 제출시 div은 Google 도서 API의 정보를 표시합니다. 이 정보는 표준 형식의 JSON 파일에서 검색됩니다. 문제없이 div 내에서 제목, 부제목, 작성자 및 설명을 표시 할 수 있습니다.JSON Google 도서 및 Ajax
나는 다음 데이터베이스에 모든 정보를 전송해야 버튼 '라이브러리에 추가'는, 내 문제는 모든 필드 저자에서 떨어져를 보낼 것입니다있다. 작성자 이름을 보내는 대신 '배열'이라는 단어가 데이터베이스로 전송됩니다.
작성자 이름을 보낼 수있는 유일한 방법은 내 Ajax 데이터 객체의 끝에 [0]을 추가하는 것입니다. 그러나이 방법은 첫 번째 작성자의 이름 만 전송합니다 (3 명의 작성자가있는 경우 2 개가 남게됩니다. 아웃).
업데이트 - Ajax 데이터를 "authors", "industryIdentifiers"또는 "categories"이외의 것으로 변경하면 JSON과 관련이있는 것으로 보입니다. 목록을 포함하고 단일 문자열이 아니기 때문입니까?
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에 새로 온 사람이 개인 개발 단지와 같이 매우 아마추어의 경우 코드를 용서하십시오.
'authors '는 어떨까요 : JSON.stringify (entry.volumeInfo.authors),'? 그런 다음 서버 측에서 구문 분석하고 계속 진행 하시겠습니까? –
FYI : http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php –
고마워요 @Rory 이제 다음 형식으로 값을 보냅니다. [ "Chris Cleave", " Philippa Gregory ","Sarah Pekkanen - JSON 서버 측을 파싱하는 방법에 대해 살펴 보겠습니다. 곧 업데이트하겠습니다. –