I'm struggling trying to create the form for this part of the pset. I have a controller (sell.php) and a template form (sell_form.php).

Sell.php receives submission from sell_form.php, delete user 's stock from portfolio table and update user's cash in users table.

The thing is that I don't know how to create a form where the user can select any of the stocks he has. This is what I wrote, which doesn't work. The idea is to iterate through an array containing every stock the user has and display that option in the form, but I don't understand how I can combine php with html tags select and option.

<form action="sell.php" method="post">
        <div class="form-group">
            <select name = "stock">
            // stocks is an array passed through render from the controler with the symbol of every stock              
            <?php foreach($stocks as $stock):?>
                    <option value = <?= $stock ?>> </option>            
        <?php endforeach ?>

Any help would be really appreciated.

1 Answer 1


First, for closed tags, if you open a tag, you should close it. I see you're not closing the <select> tag. More information about that tag maybe found here.

Also, you have </div> within the body of your for each loop which means </div> will be there n times where n is the number of iterations that this loop will iterate.

Next, you probably shouldn't be mixing HTML and PHP that way

<option value = <?= $stock ?>> </option>

because, you know, angle brackets within angel brackets is a little bit confusing. Rather, you should probably have something like

print("<option value=\"{$s}\">{$s}</option>");

where $s contains the stock symbol of the current company. Recall that you can access the symbol using something like $row["symbol"].

Hope that helps!

  • Thank you for your help. I tried tidying the syntax but I still can't get the form to work. $stocks is an array generated in sell.php and passed by render to sell_form.php. Looping through every element of that array and displaying a form option for each one of them should work, so I wrote as you suggested: print, but it seems like the connection between php and html it is not working, it doesn't see the variable in $stock, it just displays the text. pastebin.com/uqJTJ4JJ
    – FranGoitia
    Commented Aug 23, 2014 at 22:06
  • 1
    @FranciscoGoitia once you type ?>, you're out of PHP mode. The call to print() in your code happens to be out of PHP mode.
    – kzidane
    Commented Aug 23, 2014 at 23:48
  • Solved. Thank you a lot !
    – FranGoitia
    Commented Aug 24, 2014 at 1:57
  • @FranciscoGoitia feel free to upvote the answer and accept it if it was helpful! Thank you!
    – kzidane
    Commented Aug 24, 2014 at 22:00
  • Similar problem. I used very similar syntax but I cant seem to get it to work. pastebin.com/rNQgvb4g .
    – ascar
    Commented Dec 16, 2014 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .