I have come to an understanding that the problem is that I am trying to pass a string array to a JavaScript.
I do not know how to proceed.
FORM
<input type="hidden" name="id">
<input type="hidden" name="adProd">
DB Connection
$stmt = $conn->prepare("SELECT product_name, product_quantity, product_id FROM product ");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $v) {
echo "<td style='width:150px;border:1px solid black;'>";
echo "</td>";
echo '<tr>';
echo '<td>' .$v->product_name. "</td>";
echo '<td>' .$v->product_quantity. "</td>";
echo '<td>' .$v->product_id. "</td>";
product_id below works exactly as it should.
echo '<td> <button type="submit" onclick="askForSell('.$v->product_id.')"> Sell </button> </td>';
product_name on the other hand does not work.
echo '<td> <button type="submit" onclick="askForBuy('.$v->product_name.')"> Buy </button> </td>';
JavaScript
form=document.getElementById("sellAndBuy");
Works fine:
function askForSell(id) {
form.action="sellProducts.php";
form['id'].value = id;
form.submit();
}
Does not work:
function askForBuy(adProd) {
form.action="buyProducts.php";
form['adProd'].value = adProd;
form.submit();
}