0

Without needing to give too much info on the API in use, I'm trying to get the option value to match what's being wrapped inside the option tag.

Something like this:

<option value="Foo">Bar</option>

Here's the PHP:

<?php
    $counter = 0;
    $numbers = $client->account->available_phone_numbers->getList('US', 'Local', array(
    "AreaCode" => $_POST["areacode"]));
    echo "<select>";
    foreach($numbers->available_phone_numbers as $number) {
        echo "<option value=''>";
        echo $number->phone_number;
        $counter++;
        echo "</option>";
        echo "<br>";
    }
    echo "</select>";
    echo "<br>";
    echo $counter;
?>

With this form:

<form action="index.php" method="post">
    Area Code:<br>
    <input type="text" name="areacode" value=""><br>
    <input type="submit" value="Submit">
</form>
1
  • 2
    You don't put <br> inside a select, and you need to add value to each option: echo "<option value='" . htmlspecialchars($number->phone_number) . "'>"; Commented Mar 30, 2016 at 20:19

4 Answers 4

2

Your syntax is incorrect.

echo '<select>';
foreach($numbers->available_phone_numbers as $number) {

    echo '<option value="' .$number->phone_number .'">';
    echo $number->phone_number;
    echo '</option>';
}
echo '</select>'; 

Also, you shouldn't have a br tag in your select element.

Sign up to request clarification or add additional context in comments.

1 Comment

You should always wrap values that go into an attribute with htmlentities - In case the value contains characters such as " or > or others that might break the html structure - But the answer is correct in the overall
1

Change echo "<option value=''>"; to echo "<option value='{$number->phone_number}'>";

Comments

0

I think you need to move the select inside the form tag in order to submit the selected data also.

<form action="index.php" method="post">
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {

echo "<option value='".$number->phone_number."'>";
echo $number->phone_number;
$counter++;
echo "</option>";
echo "<br>";
}
echo "</select>";
echo "<br>";
echo $counter;
?>Area Code:<br>
<input type="text" name="areacode" value=""><br>
<input type="submit" value="Submit">
</form>

Hope this help

Comments

0
<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local',        
array(
"AreaCode" => $_POST["areacode"]
));
?>
<form action="index.php" method="post">
<?php
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option>" . $number->phone_number . "</option>";
$counter++;
}
echo "</select>";

?>
<input type="submit" value="Submit">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.