user3588132
user3588132

Reputation: 107

HTML Option Tags inside PHP Code

I'm rather new to PHP (just picked it up for a course on CS) and I'm right now trying to display some values from a Database, namely stocks. Right now I'm running into some issue though as I'm trying to display the stocks symbol from the database in a nice option menu. But when I try to retrieve the value via $_POST["stock"] ("stock" is the name of the option) it displays me an error of "Undefinex index: stock".

Now if I choose the above option (the option before the php code) it actually works perfectly and "stock" is retrievable (and displays nothing, as anticipated).

Now my question is: What did I do wrong and how can I make the name "stock" show the value of $_POST["stock"]

    <select class="form-group">
    <option class='form-control' type='text' name='stock'></option>
    <?php
        $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

        foreach ($rows as $row)
        {
            print("<option class='form-control' name='stock'>{$row["symbol"]}</option>");

        }
    ?> 
    </select>   

Upvotes: 0

Views: 2205

Answers (4)

Fredrik Borggren
Fredrik Borggren

Reputation: 107

You should generally always check that your key in $_POST exists to avoid the undefined index issue, and you can also check that it's not empty if defined as examples shows below.

E.g.

$stock = isset( $_POST["stock"] ) && !empty( $_POST["stock"] ) ? $_POST["stock"] : '';

Not shorthand:

$stock = '';
if ( isset( $_POST["stock"] ) && !empty( $_POST["stock"] ) ) {
    $stock = $_POST["stock"];
}

EDIT

The reason why $_POST['stock'] is undefined is because your <select> element is missing a defined name, you have the name defined on the childs instead.

Change:

<select class="form-group">

To:

<select class="form-group" name="stock">

Also, make sure to remove the name attribute from all your <option> elements.

Best of luck,
Fredrik

Upvotes: 1

Martin
Martin

Reputation: 22760

You have several issues, the serious ones are mostly HTML based.

1) Inorder to use the POST variable you need to set the FORM to send the data via post, this is done with the method tag you may or may not already have but :

<form method="post"> 

2) With HTML, the VALUE of the option selected needs to be defined, for example you have

print("<option class='form-control' name='stock'>{$row["symbol"]}</option>");

But what this does is send a value of "" (if anything) because no value has been defined. instead write it as:

 print("<option class='form-control' name='stock' value='stockvalue'>{$row["symbol"]}</option>");

As an additional - the "name" attribute needs to appear in the <select> element, and only the value attribute is needed in the <option> element.

so:

print("<option class='form-control' value='stockvalue'>{$row["symbol"]}</option>");

3) query is not a recognised function, unless you made your own function? You may want mysqli_query() instead.

4) minor reformatting: Do not use double quotes for array keys, your array should be: $rows['symbol']. Also when printing out variables to a print command, get into the habit of shaping it thus:

  print "<option class='form-control' name='stock' value='stockvalue'>".$row['symbol']."</option>";

This stops printing with the . character, and appends the raw PHP variable value $row and then appends onto this with another . the rest of the string to print. Print also does not need to be in brackets.

Upvotes: 0

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

You need to add attribute name to select not in options.use like this

<select class="form-group" name="stock">
    <option class='form-control' type='text'></option>
    <?php
        $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

        foreach ($rows as $row)
        {
print("<option value='".$row["symbol"]."' class='form-control'>".$row["symbol"]."</option>");

        }
    ?> 
    </select>

then with php when you will try to get value with $_POST['stock'] it will return you the option value which is selected . i.e $row["symbol"] value in that option.

Upvotes: 0

Ali Sheikhpour
Ali Sheikhpour

Reputation: 11055

Bring dynamic parameteres out of double quote and spearate them by dot while printing. Also use php isset before consuming posted data (specially while first option has null value):

     <select class="form-group">
       <option class='form-control' type='text' name='stock'></option>
        <?php
            $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

            foreach ($rows as $row)
            {
                print "<option class='form-control' name='stock'>{".$row["symbol"]."}</option>";

            }
        ?> 

        </select> 

Upvotes: 0

Related Questions