Jump to content

Ajax to send JSON data to DB


johnnys

Recommended Posts

I am retrieving Google Books info in JSON format and displaying it inside a div. I would like to send the contents of this div (name, title, description) to my database using Ajax.

 

Currently only the ISBN field sends because I have it declared as a variable. However my question is, how do I send the other fields (name, title, author). How do I declare these also, I'm not sure what format they need to be in etc.

 

My JS

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

My Ajax;

        $.ajax({
             type: 'POST',
             url: 'addIsbnScript.php',
             data: {
                 'isbn' : isbn,
                 'title' : title
                 'subtitle' : subtitle,
                 'authors' : authors,
                 'description' : description
             },
             success: function () { 
            $.growl({
            message: " Record added"
});      
}
});

Note, if i manually set the vars like below they all do successfully send to my database, so I know my query is working ok

var title = "some text",
var author = "some text",
Var description = "some text"

Thanks in advance for any help, newbie here (incase it wasn't obvious!).

 

J

 

 

Link to comment
https://forums.phpfreaks.com/topic/292615-ajax-to-send-json-data-to-db/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.