I am trying to update a PHP Session array 'shopping cart' when 'add to cart' button is clicked without leaving the page.
I have php written to get several variables from a MySQL database, from which I create an HTML element with php echo. Within that HTML, I am trying to add one of those variables to the PHP Session Array when "Add to Cart" button is clicked. My issue is I can't get the variable passed. I can send it to a JavaScript function, but can't use the JavaScript to add it to the Session array.
Any tips on how I can add $isbn to the session array? Is there a different way of doing this that would be better? I would prefer to stay on the same page & not change anything else except updating the Session 'cart'.
`
//create section for each book result
if($result->num_rows>0){
//get variables from db
while($row=$result->fetch_assoc()){
$title = $row['title'];
$author = $row['author'];
$publisher = $row['publisher'];
$isbn = $row['isbn'];
$price = $row['price'];
echo "
<tr>
<td align='left'>
<!-- on button click add to cart session array -->
<button name='btnCart'
id='btnCart'
type='submit'
onClick='cart(\"{$isbn}\")'>Add to Cart
</button>
</td>
<td rowspan='2' align='left'>{$title}</br>
By {$author}</br>
<b>Publisher:</b> {$publisher}</br>
<b>ISBN:</b> {$isbn} <br>
<b>Price: $</b> {$price}
</td>
</tr>
<tr>
<td align='left'>
<button name='review'
id='review'
onClick='review(\"{$isbn}\", \"{$title}\")'>Reviews
</button>
</td>
</tr>
<tr>
<td colspan='2'>
<p>_______________________________________________</p>
</td>
</tr>
";
}
}
`
Thanks!