2014-11-24 74 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