When building prototypes, I’m using html to build my forms. My little knowledge of CakePHP and html is just enough to get things done the way I want it to behave.
Bit by bit I’m trying make more use of the FormHelper, but in the case of the select-element, I’m getting stuck. Reading the cookbook over and over, just can’t figure out how to convert from htm-code to the FormHelper way of building the wanted form.
Took the code from the prototype to my testbed (fresh baked) to do things step-by-step. Maybe by writing things down I can find out what’s lacking.
Here’s the html-code which behaves like I want to:
<div>
<form>
<label for="amount">Amounts:</label>
<select id="amount" name="amount">
<?php foreach ($individual->amounts as $amount) : ?>
<option value="<?= $amount->quantity ?>"> <?= $amount->amount ?> (<?= $amount->quantity ?> g)</option>
<?php endforeach; ?>
</select>
<input type="submit" value="Submit">
</form>
</div>
<?php $quantity = $this->request->getQuery('amount'); ?>
The page and form look like:
After trying a lot of things with the related records, I restarted with just a list of options, but the
$quantity doesn’t get a value:
<div>
<?= $this->Form->create(); ?>
<?= $this->Form->select('amount', [1, 2, 3, 4, 5]); ?>
<?= $this->Form->button('Submit'); ?>
<?= $this->Form->end(); ?>
</div>
<?php $quantity = $this->request->getQuery('amount'); ?>
Looking to the generated html-code, the name stays empty, I thought that has to have value in order to work?
<select name=""><option value="0">1</option><option value="1">2</option><option value="2">3</option><option value="3">4</option>
So there’s something wrong with referring to ‘amount’, because of being a related record?
<?= $this->Form->select('amount', [1, 2, 3, 4, 5]); ?>