$stock) {
if ($stock['stock'] === $symbol) {
$stockIndex = $index;
break;
}
}
if ($stockIndex === null && !empty($symbol)) {
$message = "Stock '$symbol' not found in your portfolio.";
$messageType = 'error';
}
$currentIsin = '';
$currentNrbght = '';
$currentDepot = 'scalablecapital'; // Default value
$currentPricetarget = '';
if ($stockIndex !== null) {
$currentIsin = $stocks[$stockIndex]['isin'] ?? '';
$currentNrbght = $stocks[$stockIndex]['nrbght'] ?? '';
$currentDepot = $stocks[$stockIndex]['depot'] ?? 'scalablecapital';
$currentPricetarget = $stocks[$stockIndex]['pricetarget'] ?? '';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $stockIndex !== null) {
$isin = isset($_POST['isin']) ? strtoupper(trim($_POST['isin'])) : '';
$nrbght = isset($_POST['nrbght']) ? trim($_POST['nrbght']) : '';
$depot = isset($_POST['depot']) ? trim($_POST['depot']) : 'scalablecapital';
$pricetarget = isset($_POST['pricetarget']) ? trim($_POST['pricetarget']) : '';
$errors = [];
if (empty($isin)) {
$errors[] = 'ISIN code is required.';
} elseif (!preg_match('/^[A-Z]{2}[A-Z0-9]{9}[0-9]$/', $isin)) {
$errors[] = 'Invalid ISIN format. ISIN must be 12 characters: 2 letters, 9 alphanumeric, 1 check digit.';
}
if (empty($nrbght)) {
$errors[] = 'Number bought is required.';
} elseif (!is_numeric($nrbght) || $nrbght <= 0 || floor($nrbght) != $nrbght) {
$errors[] = 'Number bought must be a positive whole number.';
}
if (!in_array($depot, ['scalablecapital', 'ingdiba', 'comdirect'])) {
$errors[] = 'Invalid depot selection.';
}
// pricetarget is optional - only validate if the user actually filled it in
if ($pricetarget !== '' && !is_numeric($pricetarget)) {
$errors[] = 'Price target must be a number.';
}
if (empty($errors)) {
$stocks[$stockIndex]['isin'] = $isin;
$stocks[$stockIndex]['nrbght'] = (int)$nrbght;
$stocks[$stockIndex]['depot'] = $depot;
// Only add/update "pricetarget" in the JSON if the user filled it in.
// If left empty, behaviour stays exactly as it is now (field untouched).
if ($pricetarget !== '') {
$stocks[$stockIndex]['pricetarget'] = (float)$pricetarget;
}
saveStocks($stocks);
$message = "Successfully added ISIN: $isin, Quantity: $nrbght, and Depot: $depot for $symbol.";
if ($pricetarget !== '') {
$message .= " Price target: $pricetarget.";
}
$messageType = 'success';
$currentIsin = $isin;
$currentNrbght = $nrbght;
$currentDepot = $depot;
if ($pricetarget !== '') {
$currentPricetarget = $pricetarget;
}
} else {
$message = implode('
', $errors);
$messageType = 'error';
}
}
?>
Enter ISIN, quantity purchased & depot
buy.php?go=AAPL
stocks.json under the entry for
. These fields are stored as "isin", "nrbght", and "depot".
If you fill in a price target, it will also be saved as "pricetarget".