I have search results sent to a jquery window - 10 to be exact. If a user selects the 4th result, I would like that specific result assigned to a javascript variable. Toward the bottom of the below code is where I think I need to index the javascript variables somehow? Then at the top of this code I am thinking I can assign the number the user selected. I have already figured out how to get the number the user selects. Also at the bottom of the below code you will see where I commented out my attempt at indexing javascript variables to match the same php variables.
<script>
$(function() {
$( "#dialog" ).dialog({height: 550, width: 450});
$( ".submit" ).click(function(){
//if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
//bookSelect = id;
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
//assign values to the variables to be passed to the server via data
data: { B : B, cover : cover, title : title, author : author, published : published, ISBN : ISBN,
description : description, pages : pages, publisher : publisher},
success: function(data)
{
//identify the variables for unique handing on the server side, this is
//how the book data gets to the input fields for bbpress forums
$("input[name='bookCover']").val(data.cover);
$("input[name='bbp_topic_title']").val(data.title);
$("input[name='bookAuthor']").val(data.author);
$("input[name='bookPublished']").val(data.published);
$("input[name='bookDescription']").val(data.description);
$("input[name='bookPages']").val(data.pages);
$("input[name='bookPublisher']").val(data.publisher);
$("input[name='bookISBN']").val(data.ISBN);
//$("input[name='selection']").val(data.bookSelect);
//alert(B);
},
error: function(errorThrown){
alert('error');
},
});
$( "#dialog" ).dialog( "close" );
}); });
</script>
<strong><p style="font-size: 16px; text-align: center";>Top 10 Results for "<?php echo @$_POST['q']; ?>"</p></strong>
<strong><p style="font-size: 14px; text-align: center";>choose a book to select as your topic</p></strong>
<table style="width:400px">
<col width="325">
<col width="75">
<?php $i=0; foreach ($data['items'] as $item) { $i++; ?>
<tr>
<td>
<strong><u><div style="font-size: 14px";><?php printf($item['volumeInfo']['title']);
$b_title[$i] = $item['volumeInfo']['title']?></u></div></strong>
<strong>Author: </strong><?php printf( $item['volumeInfo']['authors'][0]);
$b_author[$i] = $item['volumeInfo']['authors'][0]?><br />
<strong>Published: </strong><?php printf( $item['volumeInfo']['publishedDate']);
$b_published[$i] = $item['volumeInfo']['publishedDate'] ?><br />
<strong>Page(s): </strong><?php printf( $item['volumeInfo']['pageCount']);
$b_pages[$i] = $item['volumeInfo']['pageCount'] ?><br />
<strong>Publisher: </strong><?php printf( $item['volumeInfo']['publisher']);
$b_publisher[$i] = $item['volumeInfo']['publisher'] ?><br />
<strong>Category: </strong><?php printf( strtolower($item['volumeInfo']['printType']).', '.strtolower($item['volumeInfo']['categories'][0])); ?>
<strong>ISBN: </strong><?php printf( $item['volumeInfo']['industryIdentifiers'][0]['identifier']);
$b_ISBN[$i] = $item['volumeInfo']['industryIdentifiers'][0]['identifier'] ?></td>
<td><p><?php echo "<input type='submit' method='post' name='submit' value='Select' class='submit' id='select $i' />"; ?></p>
<img src="<?php printf( rawurldecode($item['volumeInfo']['imageLinks']['smallThumbnail']));
$b_cover[$i] = $item['volumeInfo']['imageLinks']['smallThumbnail'] ?>" />
</td>
<tr><td style="width:420px"><p><strong>Description: </strong><?php printf( $item['volumeInfo']['description']);
$b_description[$i] = $item['volumeInfo']['description'] ?><br /></p></td>
</tr>
</tr>
<script>
$( ".submit" ).click(function(){
if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
B = id; });
//for(var i = 0, len = 11; i < len; i++)
//var title = [];
var cover = <?php echo json_encode($b_cover[3]); ?>;
//var title[i] = <?php echo json_encode($b_title[$i]); ?>;
var title = <?php echo json_encode($b_title[3]); ?>;
var author = <?php echo json_encode($b_author[3]); ?>;
var published = <?php echo json_encode($b_published[3]); ?>;
var description = <?php echo json_encode($b_description[3]); ?>;
var pages = <?php echo json_encode($b_pages[3]); ?>;
var publisher = <?php echo json_encode($b_publisher[3]); ?>;
var ISBN = <?php echo json_encode($b_ISBN[3]); ?>;
</script>
wp_enqueue_script
andwp_enqueue_style
as well as this tutorial on how to use AJAX in WP. – elclanrs May 26 at 2:10