0

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>   
2
  • no need to close double quote before dynamic parameter {$row["symbol"]} and open it again after that? Commented Mar 29, 2015 at 21:44
  • First print $rows array and then check the index ! Commented Mar 30, 2015 at 13:24

4 Answers 4

1

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

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

5 Comments

Thanks for that comment. But it does sadly not answer my question as to why the key in $_POST["stock"] returns an undefined inside the PHP code.
What if you change your <option> elements to not have the attribute name and instead a value attribute? E.g. <option value="foo">bar</option>
Thanks a lot! It worked by removing the name attribute from the option elements and adding it to select :) Thinking of the problem now, it makes a lot of sense. Thanks for your help!
The pleasure was mine.
copied from my answer ... thats not fair @FredrikBorggren
0

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> 

2 Comments

Thanks for your comment, but that sadly did not solve the problem as I'm still getting the same error.
this may happen when you choose no option (or first one with empty value). this code generates the form without error.
0

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.

Comments

0

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.

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.