how to write price from stock_id to modal field
how to write price from stock_id to modal field

Hi
I need to pass the stock price back into the modal price field, having selected the stock_id from the dropdown list, I may need to display the price as a message below the stock dropdown list also.
I am succeeding in passing the stock_id to the server and returning JSON, but the format and how I extract the price value back into the modal field, is where I am struggling.
In this example
symbol is AVON.LSE
price is 1744
stock_id is 290
So I need to display 1744 on the price field in the modal and display text
yesterday close price: 1744 under the symbol modal field box
Currently I am writing stock_id to both positions described above.
Extract of client javascript
**This code writes the stock_id to the modal price field box
**
editor.dependent( 'dm_transactions.stock_id', function ( val, data, callback ) {
$.ajax( {
url: '../../Editor-PHP-1.9.0/controllers/ajax_stock_transactions.php',
dataType: 'json',
// pass stock_id value to server php script
data: { "stock_id": val },
} );
editor.field("dm_transactions.price").set(editor.field('dm_transactions.stock_id').val());
}),{
event: 'keyup change'
};
**This code writes the stock_id as a message below the modal symbol field box
**
editor.dependent('dm_transactions.stock_id', function ( val, data, callback ){
return { messages: { 'dm_transactions.stock_id': 'yesterday close price: ' + editor.field('dm_transactions.stock_id').val() }};
},{
event: 'keyup change'
});
This is the modal extract shown below
extract of server PHP script
$stock_id = $_GET['stock_id']; // manually setting variable for testing
$stock_array = array();
// check if variable is NOT blank pass JSON back to client
if ($stock_id <> "") {
echo "the value of stock_id is :" . $stock_id . ":" . "\n";
try {
$pdo = new PDO(strtolower($sql_details['type']) . ":host=" . $sql_details['host'] . ";dbname=" . $sql_details['db'], $sql_details['user'], $sql_details['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully" . "\n\n";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$result = $pdo->query("SELECT id, symbol, name, price FROM dm_stocks WHERE id = $stock_id");
foreach ($result as $row) {
echo "" . $row['id'] . "";
echo " : " . $row['symbol'] . "";
echo " : " . $row['name'] . "";
echo " : " . $row['price'] . "\n";
array_push( $stock_array, array('symbol'=>$row['symbol'], 'price'=>$row['price'] ) );
}
echo json_encode($stock_array);
}
<?php
>
```
?>
the value of stock_id is :290:
Connected successfully
290 : AVON.LSE : Avon Rubber p.l.c : 1774
[{"symbol":"AVON.LSE","price":"1774"}]
```
[{"symbol":"AVON.LSE","price":"1774"}]
Please let me know if you need more code snippets.
Many Thanks
Colin
This question has an accepted answers - jump to answer
Answers
Hi
Apologies for duplicate thread, I think the answer is to use values, I will check tomorrow as I am offline for the rest of the day travelling.
how to display the price on the modal in either
1/ the field given by dm_transactions.price
response from Allan on a previous call
Similar to the options property in the returned JSON you are currently using there is a values option for the JSON returned to dependent(). That can be used to set the value of the fields - e.g.:
Likewise you can specify any other fields in the values object that you want.
Allan
Thanks
Colin
Hi
I should be grateful for some help on the client side to extract the value of dm_transactions.price using the "values" option for one sample stock. I have setup a JSON file on the server with the content as per above for a one stock
The client file is as follows
any help on the correct syntax of the client file to
1/write the price (from the JSON file 7324 in this case) under the stock_id field
and
2/write the price into the price field of the modal (editable by the user, i.e. not fixed)
Many Thanks
Colin
I'm not quite getting something here - sorry. This line:
is redundant. Its just writing its own value to itself. At its doing it before the Ajax action is completed as well.
If
../../Editor-PHP-1.9.0/controllers/ajax_stock_transactions_json.txt
contains your JSON from above, thendm_transactions.price
should contain{value}
whendm_transactions.stock_id
is changed.If that isn't happening, can you give me a link to a page showing the issue so I can trace it through please.
Allan
Hi Allan
Thanks for your response, I have sent you a private message.
Best regards
Colin