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
In case someone searches this out, I found an answer to my own question. Two warnings: 1) this answer looks messy to me. There is probably an easier way. 2) @elclanrs is correct in his comment; I plan to clean this code up into separate files. If you look at the above question, I have modified the bottom script, and modified the variables in the top script. Here is the bottom script (I used a javascript foreach array, and placed all possible results in the array - there is probably a way to do that without listing them all?). For brevity, I only included the first two variable arrays.
<script>
$( ".submit" ).click(function(){
//get the id number, which is indexed for each result, of the button the user selects
//the variable B is decremented 1 from id because javascript arrays begin with [0]
if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
B = id - 1; });
//create an array for all 10 results. There is probably a cleaner way to do this
var b_cover = [<?php echo json_encode($b_cover[1]); ?>,<?php echo json_encode($b_cover[2]); ?>,<?php echo json_encode($b_cover[3]); ?>,<?php echo json_encode($b_cover[4]); ?>,
<?php echo json_encode($b_cover[5]); ?>,<?php echo json_encode($b_cover[6]); ?>,<?php echo json_encode($b_cover[7]); ?>,<?php echo json_encode($b_cover[8]); ?>,
<?php echo json_encode($b_cover[9]); ?>,<?php echo json_encode($b_cover[10]); ?>];
//this is the javascript foreach
b_cover.forEach(function(entry){console.log(entry);});
var b_title = [<?php echo json_encode($b_title[1]); ?>,<?php echo json_encode($b_title[2]); ?>,<?php echo json_encode($b_title[3]); ?>,
<?php echo json_encode($b_title[4]); ?>,<?php echo json_encode($b_title[5]); ?>,<?php echo json_encode($b_title[6]); ?>,
<?php echo json_encode($b_title[7]); ?>,<?php echo json_encode($b_title[8]); ?>,<?php echo json_encode($b_title[9]); ?>,<?php echo json_encode($b_title[10]); ?>];
b_title.forEach(function(entry){console.log(entry);});
And here is how the variables are assigned in the top script:
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
//assign values to the variables to be passed to the server via data, according to their B position
//within the foreach array.
data: { B : B, cover : b_cover[B], title : b_title[B], author : b_author[B], published : b_published[B],
ISBN : b_ISBN[B], description : b_description[B], pages : b_pages[B], publisher : b_publisher[B]},
success: function(data)