I am creating a currency calculator using a Google API but don't seem to be setting the variable selected from the drop-down box.
I create an array to hold 3 currencies:
<?php
$options = array(
'usd' => 'USD',
'eur' => 'EUR',
'gbp' => 'GBP'
);
?>
The user selects one of the above values which are displayed in the form.
<form action="" method="post ">
<p>
Amount:<br />
<input type="text" name="amount" />
</p>
<p>
From:<br />
<input type="text" name="from" />
</p>
<p>
To:<br />
<select name="position">
<?php foreach ($options as $value => $text): ?>
<option value="<?php echo $value ?>"
<?php echo ($v['position'] == $value) ? 'selected="selected"' : '' ?> >
<?php echo $text ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>
The following function is used to determine whether variables have been set and I echo out of results to ensure my findings.
if (isset($_POST['amount'], $_POST['from'], $_POST[$value])) {
$amount = (int) $_POST['amount'];
$from = $_POST['from'];
$value = $_POST['value'];
echo $amount;
echo $from;
echo $value;
// echo $text;
// echo $selected;
// echo $options;
// echo $v;
exit();
I can successfully echo out the $amount and $from variables but don't understand why I can get the $value variable from the drop-down box.
I've tried echoing out all the variables to no avail which makes me believe that the problem is with my coding in the form itself.
Any obvious issues spring to mind?
Thanks in advance.
Where is $v defined? Also, I don't understand why you're checking if $_POST[$value] exists then setting $value = $_POST['value']... and what is $value before it gets changed? What element has the name value? Shouldn't you just be grabbing $_POST['position']?
$_POST['position'] is the variable that will always contain which value of the select menu was selected, but I don't see you actually using it anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With